1: <?php
2: /*
3: * This class was auto-generated from the API references found at
4: * https://apireference.connect.worldline-solutions.com/
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: * Class WebhooksHelper
19: *
20: * @package Worldline\Connect\Sdk\V1\Webhooks
21: */
22: class WebhooksHelper
23: {
24: /** @var SignatureValidator */
25: private $signatureValidator;
26:
27: /** @var ResponseFactory|null */
28: private $responseFactory = null;
29:
30: /**
31: * @param SecretKeyStore $secretKeyStore
32: */
33: public function __construct(SecretKeyStore $secretKeyStore)
34: {
35: $this->signatureValidator = new SignatureValidator($secretKeyStore);
36: }
37:
38: /** @return ResponseFactory */
39: protected function getResponseFactory()
40: {
41: if (is_null($this->responseFactory)) {
42: $this->responseFactory = new ResponseFactory();
43: }
44: return $this->responseFactory;
45: }
46:
47: /**
48: * Unmarshals the given input stream that contains the body,
49: * while also validating its contents using the given request headers.
50: * @param string $body
51: * @param array $requestHeaders
52: * @return WebhooksEvent
53: * @throws SignatureValidationException
54: * @throws ApiVersionMismatchException
55: */
56: public function unmarshal($body, $requestHeaders)
57: {
58: $this->signatureValidator->validate($body, $requestHeaders);
59:
60: $response = new ConnectionResponse(200, array('Content-Type' => 'application/json'), $body);
61: $responseClassMap = new ResponseClassMap();
62: $responseClassMap->addResponseClassName(200, '\Worldline\Connect\Sdk\V1\Domain\WebhooksEvent');
63:
64: $event = $this->getResponseFactory()->createResponse($response, $responseClassMap);
65: $this->validateApiVersion($event);
66: return $event;
67: }
68:
69: private function validateApiVersion($event)
70: {
71: if ('v1' !== $event->apiVersion) {
72: throw new ApiVersionMismatchException($event->apiVersion, 'v1');
73: }
74: }
75: }
76: