1 | <?php |
||
12 | class Webhook extends AbstractApi |
||
13 | { |
||
14 | /** |
||
15 | * @var null|\Kerox\Messenger\Api\Webhook |
||
16 | */ |
||
17 | private static $_instance; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $appSecret; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $verifyToken; |
||
28 | |||
29 | /** |
||
30 | * @var \Psr\Http\Message\ServerRequestInterface |
||
31 | */ |
||
32 | protected $request; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $body; |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $decodedBody; |
||
43 | |||
44 | /** |
||
45 | * @var \Kerox\Messenger\Model\Entry[] |
||
46 | */ |
||
47 | protected $hydratedEntries; |
||
48 | |||
49 | /** |
||
50 | * Webhook constructor. |
||
51 | * |
||
52 | * @param string $appSecret |
||
53 | * @param string $verifyToken |
||
54 | * @param string $pageToken |
||
55 | * @param \GuzzleHttp\ClientInterface $client |
||
56 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
57 | */ |
||
58 | 7 | public function __construct(string $appSecret, string $verifyToken, string $pageToken, ClientInterface $client, ServerRequestInterface $request = null) |
|
66 | |||
67 | /** |
||
68 | * @param string $appSecret |
||
69 | * @param string $verifyToken |
||
70 | * @param string $pageToken |
||
71 | * @param \GuzzleHttp\ClientInterface $client |
||
72 | * @param \Psr\Http\Message\ServerRequestInterface $request |
||
73 | * |
||
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 | * @throws \Exception |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 4 | public function getDecodedBody(): array |
|
170 | |||
171 | /** |
||
172 | * @return \Kerox\Messenger\Model\Entry[] |
||
173 | */ |
||
174 | 1 | public function getCallbackEntries(): array |
|
178 | |||
179 | /** |
||
180 | * @return array |
||
181 | */ |
||
182 | 1 | public function getCallbackEvents(): array |
|
192 | |||
193 | /** |
||
194 | * @return \Kerox\Messenger\Model\Callback\Entry[] |
||
195 | */ |
||
196 | 2 | private function getHydratedEntries(): array |
|
211 | |||
212 | /** |
||
213 | * @return bool |
||
214 | */ |
||
215 | 1 | private function isValidHubSignature(): bool |
|
228 | } |
||
229 |