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 $secret |
||
56 | * @param $verifyToken |
||
57 | * @param EventDispatcherInterface|null $dispatcher |
||
58 | */ |
||
59 | 12 | public function __construct($secret, $verifyToken, EventDispatcherInterface $dispatcher = null) |
|
60 | { |
||
61 | 12 | $this->secret = $secret; |
|
62 | 12 | $this->verifyToken = $verifyToken; |
|
63 | 12 | $this->dispatcher = $dispatcher ?: new EventDispatcher(); |
|
64 | 12 | } |
|
65 | |||
66 | /** |
||
67 | * @param ServerRequestInterface $request |
||
68 | */ |
||
69 | 12 | public function handleRequest(ServerRequestInterface $request) |
|
73 | |||
74 | /** |
||
75 | * Check if the token match with the given verify token. |
||
76 | * This is useful in the webhook setup process. |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 1 | public function isValidVerifyTokenRequest() |
|
81 | { |
||
82 | 1 | if ($this->getRequest()->getMethod() !== 'GET') { |
|
83 | return false; |
||
84 | } |
||
85 | |||
86 | 1 | $params = $this->getRequest()->getQueryParams(); |
|
87 | |||
88 | 1 | if (!isset($params['hub_verify_token'])) { |
|
89 | return false; |
||
90 | } |
||
91 | |||
92 | 1 | return $params['hub_verify_token'] === $this->verifyToken; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @return null|string |
||
97 | */ |
||
98 | 1 | public function getChallenge() |
|
99 | { |
||
100 | 1 | $params = $this->getRequest()->getQueryParams(); |
|
101 | |||
102 | 1 | return isset($params['hub_challenge']) ? $params['hub_challenge'] : null; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * Check if the request is a valid webhook request |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | 3 | public function isValidCallbackRequest() |
|
123 | |||
124 | /** |
||
125 | * Check if the request is a valid webhook request |
||
126 | * |
||
127 | * @deprecated use WebhookRequestHandler::isValidCallbackRequest() instead |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function isValid() |
||
132 | { |
||
133 | return $this->isValidCallbackRequest(); |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @return CallbackEvent[] |
||
138 | */ |
||
139 | 2 | public function getAllCallbackEvents() |
|
149 | |||
150 | /** |
||
151 | * @return Entry[] |
||
152 | */ |
||
153 | 1 | public function getEntries() |
|
157 | |||
158 | /** |
||
159 | * @return ServerRequestInterface |
||
160 | */ |
||
161 | 10 | public function getRequest() |
|
162 | { |
||
163 | 10 | if ($this->request) { |
|
164 | 10 | return $this->request; |
|
165 | } |
||
166 | |||
167 | return $this->request = ServerRequest::fromGlobals(); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @return array |
||
172 | */ |
||
173 | 5 | public function getDecodedBody() |
|
174 | { |
||
175 | 5 | if (isset($this->decodedBody)) { |
|
176 | return $this->decodedBody; |
||
177 | } |
||
178 | |||
179 | 5 | $decoded = @json_decode($this->getBody(), true); |
|
180 | |||
181 | 5 | return $this->decodedBody = null === $decoded ? [] : $decoded; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * Dispatch events to listeners |
||
186 | */ |
||
187 | 1 | public function dispatchCallbackEvents() |
|
188 | { |
||
189 | 1 | foreach ($this->getAllCallbackEvents() as $event) { |
|
190 | 1 | $this->dispatcher->dispatch($event->getName(), $event); |
|
191 | |||
192 | 1 | if ($event instanceof PostbackEvent) { |
|
193 | // Dispatch postback payload |
||
194 | $this->dispatcher->dispatch($event->getPostback()->getPayload(), $event); |
||
195 | } |
||
196 | |||
197 | 1 | if ($event instanceof MessageEvent && $event->isQuickReply()) { |
|
198 | // Dispatch quick reply payload |
||
199 | 1 | $this->dispatcher->dispatch($event->getQuickReplyPayload(), $event); |
|
200 | 1 | } |
|
201 | 1 | } |
|
202 | 1 | } |
|
203 | |||
204 | /** |
||
205 | * @param EventSubscriberInterface $subscriber |
||
206 | */ |
||
207 | 1 | public function addEventSubscriber(EventSubscriberInterface $subscriber) |
|
208 | { |
||
209 | 1 | $this->dispatcher->addSubscriber($subscriber); |
|
210 | 1 | } |
|
211 | |||
212 | /** |
||
213 | * @return Entry[] |
||
214 | */ |
||
215 | 3 | private function getHydratedEntries() |
|
232 | |||
233 | /** |
||
234 | * @return string |
||
235 | */ |
||
236 | 7 | private function getBody() |
|
237 | { |
||
238 | 7 | if (isset($this->body)) { |
|
239 | 1 | return $this->body; |
|
240 | } |
||
246 | |||
247 | /** |
||
248 | * @return bool |
||
249 | */ |
||
250 | 3 | private function isValidHubSignature() |
|
262 | } |
||
263 |