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: | private $signatureValidator; |
26: | |
27: | |
28: | private $responseFactory = null; |
29: | |
30: | |
31: | |
32: | |
33: | public function __construct(SecretKeyStore $secretKeyStore) |
34: | { |
35: | $this->signatureValidator = new SignatureValidator($secretKeyStore); |
36: | } |
37: | |
38: | |
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: | |
49: | |
50: | |
51: | |
52: | |
53: | |
54: | |
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: | |