1 | <?php |
||
12 | class Webhook extends AbstractApi |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var null|\Kerox\Messenger\Api\Webhook |
||
17 | */ |
||
18 | private static $_instance; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $appSecret; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $verifyToken; |
||
29 | |||
30 | /** |
||
31 | * @var \Psr\Http\Message\ServerRequestInterface |
||
32 | */ |
||
33 | protected $request; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $body; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $decodedBody; |
||
44 | |||
45 | /** |
||
46 | * @var \Kerox\Messenger\Model\Entry[] |
||
47 | */ |
||
48 | protected $hydratedEntries; |
||
49 | |||
50 | /** |
||
51 | * Webhook constructor. |
||
52 | * |
||
53 | * @param string $appSecret |
||
54 | * @param string $verifyToken |
||
55 | * @param string $pageToken |
||
56 | * @param \GuzzleHttp\ClientInterface $client |
||
57 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
58 | */ |
||
59 | 7 | public function __construct(string $appSecret, string $verifyToken, string $pageToken, ClientInterface $client, ServerRequestInterface $request = null) |
|
67 | |||
68 | /** |
||
69 | * @param string $appSecret |
||
70 | * @param string $verifyToken |
||
71 | * @param string $pageToken |
||
72 | * @param \GuzzleHttp\ClientInterface $client |
||
73 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
74 | * @return \Kerox\Messenger\Api\Webhook |
||
75 | */ |
||
76 | 1 | public static function getInstance(string $appSecret, string $verifyToken, string $pageToken, ClientInterface $client, ServerRequestInterface $request = null): Webhook |
|
84 | |||
85 | /** |
||
86 | * @return bool |
||
87 | */ |
||
88 | 1 | public function isValidToken(): bool |
|
89 | { |
||
90 | 1 | if ($this->request->getMethod() !== 'GET') { |
|
91 | return false; |
||
92 | } |
||
93 | |||
94 | 1 | $params = $this->request->getQueryParams(); |
|
95 | 1 | if (!isset($params['hub_verify_token'])) { |
|
96 | return false; |
||
97 | } |
||
98 | |||
99 | 1 | return ($params['hub_mode'] === 'subscribe' && $params['hub_verify_token'] === $this->verifyToken); |
|
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return string|null |
||
104 | */ |
||
105 | 1 | public function challenge() |
|
111 | |||
112 | /** |
||
113 | * @return \Kerox\Messenger\Response\WebhookResponse |
||
114 | */ |
||
115 | 1 | public function subscribe(): WebhookResponse |
|
122 | |||
123 | /** |
||
124 | * @return bool |
||
125 | */ |
||
126 | 1 | public function isValidCallback(): bool |
|
139 | |||
140 | /** |
||
141 | * @return string |
||
142 | */ |
||
143 | 4 | public function getBody(): string |
|
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | * @throws \Exception |
||
155 | */ |
||
156 | 4 | public function getDecodedBody(): array |
|
169 | |||
170 | /** |
||
171 | * @return \Kerox\Messenger\Model\Entry[] |
||
172 | */ |
||
173 | 1 | public function getCallbackEntries(): array |
|
177 | |||
178 | /** |
||
179 | * @return array |
||
180 | */ |
||
181 | 1 | public function getCallbackEvents(): array |
|
191 | |||
192 | /** |
||
193 | * @return \Kerox\Messenger\Model\Callback\Entry[] |
||
194 | */ |
||
195 | 2 | private function getHydratedEntries(): array |
|
210 | |||
211 | /** |
||
212 | * @return bool |
||
213 | */ |
||
214 | 1 | private function isValidHubSignature(): bool |
|
227 | } |
||
228 |