1 | <?php |
||
15 | class WebhookRequestHandler |
||
16 | { |
||
17 | /** |
||
18 | * @var ServerRequestInterface |
||
19 | */ |
||
20 | private $request; |
||
21 | |||
22 | /** |
||
23 | * App secret used to verify the request sha1 |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | private $secret; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $decodedBody; |
||
33 | |||
34 | /** |
||
35 | * @var Entry[] |
||
36 | */ |
||
37 | private $hydratedEntries; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $body; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $verifyToken; |
||
48 | |||
49 | /** |
||
50 | * @var EventDispatcherInterface |
||
51 | */ |
||
52 | private $dispatcher; |
||
53 | |||
54 | /** |
||
55 | * @param string $secret |
||
56 | * @param string $verifyToken |
||
57 | * @param ServerRequestInterface|null $request |
||
58 | */ |
||
59 | 12 | public function __construct($secret, $verifyToken, ServerRequestInterface $request = null, EventDispatcherInterface $dispatcher = null) |
|
60 | { |
||
61 | 12 | $this->secret = $secret; |
|
62 | 12 | $this->verifyToken = $verifyToken; |
|
63 | 12 | $this->request = $request ?: ServerRequest::fromGlobals(); |
|
64 | 12 | $this->dispatcher = $dispatcher ?: new EventDispatcher(); |
|
65 | 12 | } |
|
66 | |||
67 | /** |
||
68 | * Check if the token match with the given verify token. |
||
69 | * This is useful in the webhook setup process. |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 1 | public function isValidVerifyTokenRequest() |
|
74 | { |
||
75 | 1 | if ($this->request->getMethod() !== 'GET') { |
|
76 | return false; |
||
77 | } |
||
78 | |||
79 | 1 | $params = $this->request->getQueryParams(); |
|
80 | |||
81 | 1 | if (!isset($params['hub_verify_token'])) { |
|
82 | return false; |
||
83 | } |
||
84 | |||
85 | 1 | return $params['hub_verify_token'] === $this->verifyToken; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return null|string |
||
90 | */ |
||
91 | 1 | public function getChallenge() |
|
97 | |||
98 | /** |
||
99 | * Check if the request is a valid webhook request |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | 3 | public function isValidCallbackRequest() |
|
116 | |||
117 | /** |
||
118 | * Check if the request is a valid webhook request |
||
119 | * |
||
120 | * @deprecated use WebhookRequestHandler::isValidCallbackRequest() instead |
||
121 | * |
||
122 | * @return bool |
||
123 | */ |
||
124 | public function isValid() |
||
128 | |||
129 | /** |
||
130 | * @return CallbackEvent[] |
||
131 | */ |
||
132 | 2 | public function getAllCallbackEvents() |
|
142 | |||
143 | /** |
||
144 | * @return Entry[] |
||
145 | */ |
||
146 | 1 | public function getEntries() |
|
150 | |||
151 | /** |
||
152 | * @return ServerRequestInterface |
||
153 | */ |
||
154 | 1 | public function getRequest() |
|
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | 5 | public function getDecodedBody() |
|
172 | |||
173 | /** |
||
174 | * Dispatch events to listeners |
||
175 | */ |
||
176 | 1 | public function dispatchCallbackEvents() |
|
192 | |||
193 | /** |
||
194 | * @param EventSubscriberInterface $subscriber |
||
195 | */ |
||
196 | 1 | public function addEventSubscriber(EventSubscriberInterface $subscriber) |
|
200 | |||
201 | /** |
||
202 | * @return Entry[] |
||
203 | */ |
||
204 | 3 | private function getHydratedEntries() |
|
221 | |||
222 | /** |
||
223 | * @return string |
||
224 | */ |
||
225 | 7 | private function getBody() |
|
235 | |||
236 | /** |
||
237 | * @return bool |
||
238 | */ |
||
239 | 3 | private function isValidHubSignature() |
|
251 | } |
||
252 |