| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | namespace Worldline\Connect\Sdk\V1\Webhooks; |
| 7: | |
| 8: | use Worldline\Connect\Sdk\Communication\ConnectionResponse; |
| 9: | use Worldline\Connect\Sdk\Communication\ResponseClassMap; |
| 10: | use Worldline\Connect\Sdk\Communication\ResponseFactory; |
| 11: | use Worldline\Connect\Sdk\V1\Domain\WebhooksEvent; |
| 12: | use Worldline\Connect\Sdk\Webhooks\ApiVersionMismatchException; |
| 13: | use Worldline\Connect\Sdk\Webhooks\SecretKeyStore; |
| 14: | use Worldline\Connect\Sdk\Webhooks\SignatureValidationException; |
| 15: | use Worldline\Connect\Sdk\Webhooks\SignatureValidator; |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | class WebhooksHelper |
| 23: | { |
| 24: | |
| 25: | |
| 26: | |
| 27: | private SignatureValidator $signatureValidator; |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | private ?ResponseFactory $responseFactory = null; |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | public function __construct(SecretKeyStore $secretKeyStore) |
| 38: | { |
| 39: | $this->signatureValidator = new SignatureValidator($secretKeyStore); |
| 40: | } |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | protected function getResponseFactory(): ResponseFactory |
| 46: | { |
| 47: | if (is_null($this->responseFactory)) { |
| 48: | $this->responseFactory = new ResponseFactory(); |
| 49: | } |
| 50: | return $this->responseFactory; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | public function unmarshal(string $body, array $requestHeaders): WebhooksEvent |
| 65: | { |
| 66: | $this->signatureValidator->validate($body, $requestHeaders); |
| 67: | |
| 68: | $response = new ConnectionResponse(200, array('Content-Type' => 'application/json'), $body); |
| 69: | $responseClassMap = new ResponseClassMap(); |
| 70: | $responseClassMap->addResponseClassName(200, '\Worldline\Connect\Sdk\V1\Domain\WebhooksEvent'); |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | $event = $this->getResponseFactory()->createResponse($response, $responseClassMap); |
| 76: | $this->validateApiVersion($event); |
| 77: | return $event; |
| 78: | } |
| 79: | |
| 80: | private function validateApiVersion(WebhooksEvent $event): void |
| 81: | { |
| 82: | if ('v1' !== $event->apiVersion) { |
| 83: | throw new ApiVersionMismatchException($event->apiVersion, 'v1'); |
| 84: | } |
| 85: | } |
| 86: | } |
| 87: | |