1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Zanzara; |
||||
6 | |||||
7 | use Psr\Container\ContainerInterface; |
||||
8 | use React\EventLoop\LoopInterface; |
||||
9 | use React\Http\Browser; |
||||
10 | use React\Promise\PromiseInterface; |
||||
11 | use Zanzara\Support\CallableResolver; |
||||
12 | use Zanzara\Telegram\TelegramTrait; |
||||
13 | use Zanzara\Telegram\Type\CallbackQuery; |
||||
14 | use Zanzara\Telegram\Type\ChannelPost; |
||||
15 | use Zanzara\Telegram\Type\Chat; |
||||
16 | use Zanzara\Telegram\Type\ChosenInlineResult; |
||||
17 | use Zanzara\Telegram\Type\EditedChannelPost; |
||||
18 | use Zanzara\Telegram\Type\EditedMessage; |
||||
19 | use Zanzara\Telegram\Type\InlineQuery; |
||||
20 | use Zanzara\Telegram\Type\Message; |
||||
21 | use Zanzara\Telegram\Type\Poll\Poll; |
||||
22 | use Zanzara\Telegram\Type\Poll\PollAnswer; |
||||
23 | use Zanzara\Telegram\Type\Shipping\PreCheckoutQuery; |
||||
24 | use Zanzara\Telegram\Type\Shipping\ShippingQuery; |
||||
25 | use Zanzara\Telegram\Type\Update; |
||||
26 | use Zanzara\Telegram\Type\User; |
||||
27 | |||||
28 | /** |
||||
29 | * Update shortcut methods |
||||
30 | * @method int getUpdateId() |
||||
31 | * @method Message|null getMessage() |
||||
32 | * @method EditedMessage|null getEditedMessage() |
||||
33 | * @method ChannelPost|null getChannelPost() |
||||
34 | * @method EditedChannelPost|null getEditedChannelPost() |
||||
35 | * @method InlineQuery|null getInlineQuery() |
||||
36 | * @method ChosenInlineResult|null getChosenInlineResult() |
||||
37 | * @method CallbackQuery|null getCallbackQuery() |
||||
38 | * @method ShippingQuery|null getShippingQuery() |
||||
39 | * @method PreCheckoutQuery|null getPreCheckoutQuery() |
||||
40 | * @method Poll|null getPoll() |
||||
41 | * @method PollAnswer|null getPollAnswer() |
||||
42 | * @method User|null getEffectiveUser() |
||||
43 | * @method Chat|null getEffectiveChat() |
||||
44 | * |
||||
45 | * @see Update |
||||
46 | */ |
||||
47 | class Context |
||||
48 | { |
||||
49 | use TelegramTrait, CallableResolver; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
50 | |||||
51 | /** |
||||
52 | * Array used to pass data between middleware. |
||||
53 | * |
||||
54 | * @var array |
||||
55 | */ |
||||
56 | private $data = []; |
||||
57 | |||||
58 | /** |
||||
59 | * @var ZanzaraCache |
||||
60 | */ |
||||
61 | private $cache; |
||||
62 | |||||
63 | /** |
||||
64 | * @var ConversationManager |
||||
65 | */ |
||||
66 | private $conversationManager; |
||||
67 | |||||
68 | /** |
||||
69 | * @param Update $update |
||||
70 | * @param ContainerInterface $container |
||||
71 | */ |
||||
72 | public function __construct(Update $update, ContainerInterface $container) |
||||
73 | { |
||||
74 | $this->update = $update; |
||||
75 | $this->container = $container; |
||||
76 | $this->browser = $container->get(Browser::class); |
||||
77 | $this->cache = $container->get(ZanzaraCache::class); |
||||
78 | $this->conversationManager = $container->get(ConversationManager::class); |
||||
79 | } |
||||
80 | |||||
81 | /** |
||||
82 | * @param string $key |
||||
83 | * @param $value |
||||
84 | */ |
||||
85 | public function set(string $key, $value): void |
||||
86 | { |
||||
87 | $this->data[$key] = $value; |
||||
88 | } |
||||
89 | |||||
90 | /** |
||||
91 | * @param string $key |
||||
92 | * @return mixed|null |
||||
93 | */ |
||||
94 | public function get(string $key) |
||||
95 | { |
||||
96 | return $this->data[$key] ?? null; |
||||
97 | } |
||||
98 | |||||
99 | /** |
||||
100 | * @param $name |
||||
101 | * @param $arguments |
||||
102 | * @return mixed |
||||
103 | */ |
||||
104 | public function __call($name, $arguments) |
||||
105 | { |
||||
106 | return $this->update->$name($arguments); |
||||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * @return Update|null |
||||
111 | */ |
||||
112 | public function getUpdate(): ?Update |
||||
113 | { |
||||
114 | return $this->update; |
||||
115 | } |
||||
116 | |||||
117 | /** |
||||
118 | * Used to either start a new conversation or to set next step handler for an existing conversation. |
||||
119 | * Conversations are based on chat_id. |
||||
120 | * By default a in-memory cache is used to keep the conversation's state. |
||||
121 | * See https://github.com/badfarm/zanzara/wiki#conversations-and-user-data-cache. |
||||
122 | * Use the returned promise to know if the operation was successful. |
||||
123 | * |
||||
124 | * This callable must be take on parameter of type Context |
||||
125 | * @param $handler |
||||
126 | * @param bool $skipListeners if true the conversation handler has precedence over the listeners, so the listener |
||||
127 | * callbacks are not executed. |
||||
128 | * @return PromiseInterface |
||||
129 | * @throws \DI\DependencyException |
||||
130 | * @throws \DI\NotFoundException |
||||
131 | */ |
||||
132 | public function nextStep($handler, bool $skipListeners = false): PromiseInterface |
||||
133 | { |
||||
134 | // update is not null when used within the Context |
||||
135 | $chatId = $this->update->/** @scrutinizer ignore-call */ getEffectiveChat()->getId(); |
||||
136 | return $this->conversationManager->setConversationHandler($chatId, $this->getCallable($handler), $skipListeners); |
||||
137 | } |
||||
138 | |||||
139 | /** |
||||
140 | * Ends the conversation by cleaning the cache. |
||||
141 | * Conversations are based on chat_id. |
||||
142 | * See https://github.com/badfarm/zanzara/wiki#conversations-and-user-data-cache. |
||||
143 | * Use the returned promise to know if the operation was successful. |
||||
144 | * |
||||
145 | * @return PromiseInterface |
||||
146 | */ |
||||
147 | public function endConversation(): PromiseInterface |
||||
148 | { |
||||
149 | // update is not null when used within the Context |
||||
150 | $chatId = $this->update->/** @scrutinizer ignore-call */ getEffectiveChat()->getId(); |
||||
151 | return $this->conversationManager->deleteConversationCache($chatId); |
||||
152 | } |
||||
153 | |||||
154 | /** |
||||
155 | * Returns all the chat-related data. |
||||
156 | * |
||||
157 | * Eg: |
||||
158 | * $ctx->getChatData()->then(function($data) { |
||||
159 | * $age = $data['age']; |
||||
160 | * }); |
||||
161 | * |
||||
162 | * @return PromiseInterface |
||||
163 | */ |
||||
164 | public function getChatData() |
||||
165 | { |
||||
166 | $chatId = $this->update->getEffectiveChat()->getId(); |
||||
0 ignored issues
–
show
The method
getEffectiveChat() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
167 | return $this->cache->getCacheChatData($chatId); |
||||
168 | } |
||||
169 | |||||
170 | /** |
||||
171 | * Gets an item of the chat data. |
||||
172 | * |
||||
173 | * Eg: |
||||
174 | * $ctx->getChatDataItem('age')->then(function($age) { |
||||
175 | * |
||||
176 | * }); |
||||
177 | * |
||||
178 | * @param $key |
||||
179 | * @return PromiseInterface |
||||
180 | */ |
||||
181 | public function getChatDataItem($key): PromiseInterface |
||||
182 | { |
||||
183 | $chatId = $this->update->getEffectiveChat()->getId(); |
||||
184 | return $this->cache->getCacheChatDataItem($chatId, $key); |
||||
185 | } |
||||
186 | |||||
187 | /** |
||||
188 | * Sets an item of the chat data. |
||||
189 | * |
||||
190 | * Eg: |
||||
191 | * $ctx->setChatData('age', 21)->then(function($result) { |
||||
192 | * |
||||
193 | * }); |
||||
194 | * |
||||
195 | * @param $key |
||||
196 | * @param $data |
||||
197 | * @param $ttl |
||||
198 | * @return PromiseInterface |
||||
199 | */ |
||||
200 | public function setChatData($key, $data, $ttl = false): PromiseInterface |
||||
201 | { |
||||
202 | $chatId = $this->update->getEffectiveChat()->getId(); |
||||
203 | return $this->cache->setCacheChatData($chatId, $key, $data, $ttl); |
||||
204 | } |
||||
205 | |||||
206 | /** |
||||
207 | * Append data to an existing chat cache item. The item value is always an array. |
||||
208 | * |
||||
209 | * Eg: |
||||
210 | * $ctx->appendChatData('users', ['Mike', 'John'])->then(function($result) { |
||||
211 | * |
||||
212 | * }); |
||||
213 | * |
||||
214 | * @param $key |
||||
215 | * @param $data |
||||
216 | * @param $ttl |
||||
217 | * @return PromiseInterface |
||||
218 | */ |
||||
219 | public function appendChatData($key, $data, $ttl = false): PromiseInterface |
||||
220 | { |
||||
221 | $chatId = $this->update->getEffectiveChat()->getId(); |
||||
222 | return $this->cache->appendCacheChatData($chatId, $key, $data, $ttl); |
||||
223 | } |
||||
224 | |||||
225 | /** |
||||
226 | * Deletes an item from the chat data. |
||||
227 | * |
||||
228 | * Eg: |
||||
229 | * $ctx->deleteChatDataItem('age')->then(function($result) { |
||||
230 | * |
||||
231 | * }); |
||||
232 | * |
||||
233 | * @param $key |
||||
234 | * @return PromiseInterface |
||||
235 | */ |
||||
236 | public function deleteChatDataItem($key): PromiseInterface |
||||
237 | { |
||||
238 | $chatId = $this->update->getEffectiveChat()->getId(); |
||||
239 | return $this->cache->deleteCacheChatDataItem($chatId, $key); |
||||
240 | } |
||||
241 | |||||
242 | /** |
||||
243 | * Deletes all chat data. |
||||
244 | * |
||||
245 | * Eg: |
||||
246 | * $ctx->deleteChatData()->then(function($result) { |
||||
247 | * |
||||
248 | * }); |
||||
249 | * |
||||
250 | * @return PromiseInterface |
||||
251 | */ |
||||
252 | public function deleteChatData(): PromiseInterface |
||||
253 | { |
||||
254 | $chatId = $this->update->getEffectiveChat()->getId(); |
||||
255 | return $this->cache->deleteAllCacheChatData($chatId); |
||||
256 | } |
||||
257 | |||||
258 | /** |
||||
259 | * Returns all the user-related data. |
||||
260 | * |
||||
261 | * Eg: |
||||
262 | * $ctx->getUserData()->then(function($data) { |
||||
263 | * $age = $data['age']; |
||||
264 | * }); |
||||
265 | * |
||||
266 | * @return PromiseInterface |
||||
267 | */ |
||||
268 | public function getUserData(): PromiseInterface |
||||
269 | { |
||||
270 | $userId = $this->update->getEffectiveUser()->getId(); |
||||
271 | return $this->cache->getCacheUserData($userId); |
||||
272 | } |
||||
273 | |||||
274 | /** |
||||
275 | * Gets an item of the user data. |
||||
276 | * |
||||
277 | * Eg: |
||||
278 | * $ctx->getUserDataItem('age')->then(function($age) { |
||||
279 | * |
||||
280 | * }); |
||||
281 | * |
||||
282 | * @param $key |
||||
283 | * @return PromiseInterface |
||||
284 | */ |
||||
285 | public function getUserDataItem($key): PromiseInterface |
||||
286 | { |
||||
287 | $userId = $this->update->getEffectiveUser()->getId(); |
||||
288 | return $this->cache->getCacheUserDataItem($userId, $key); |
||||
289 | } |
||||
290 | |||||
291 | /** |
||||
292 | * Sets an item of the user data. |
||||
293 | * |
||||
294 | * Eg: |
||||
295 | * $ctx->setUserData('age', 21)->then(function($result) { |
||||
296 | * |
||||
297 | * }); |
||||
298 | * |
||||
299 | * @param $key |
||||
300 | * @param $data |
||||
301 | * @param $ttl |
||||
302 | * @return PromiseInterface |
||||
303 | */ |
||||
304 | public function setUserData($key, $data, $ttl = false): PromiseInterface |
||||
305 | { |
||||
306 | $userId = $this->update->getEffectiveUser()->getId(); |
||||
307 | return $this->cache->setCacheUserData($userId, $key, $data, $ttl); |
||||
308 | } |
||||
309 | |||||
310 | /** |
||||
311 | * Append data to an existing user cache item. The item value is always an array. |
||||
312 | * |
||||
313 | * Eg: |
||||
314 | * $ctx->appendUserData('users', ['Mike', 'John'])->then(function($result) { |
||||
315 | * |
||||
316 | * }); |
||||
317 | * |
||||
318 | * @param $key |
||||
319 | * @param $data |
||||
320 | * @param $ttl |
||||
321 | * @return PromiseInterface |
||||
322 | */ |
||||
323 | public function appendUserData($key, $data, $ttl = false): PromiseInterface |
||||
324 | { |
||||
325 | $userId = $this->update->getEffectiveUser()->getId(); |
||||
326 | return $this->cache->appendCacheUserData($userId, $key, $data, $ttl); |
||||
327 | } |
||||
328 | |||||
329 | /** |
||||
330 | * Deletes an item from the user data. |
||||
331 | * |
||||
332 | * Eg: |
||||
333 | * $ctx->deleteUserDataItem('age')->then(function($result) { |
||||
334 | * |
||||
335 | * }); |
||||
336 | * |
||||
337 | * @param $key |
||||
338 | * @return PromiseInterface |
||||
339 | */ |
||||
340 | public function deleteUserDataItem($key): PromiseInterface |
||||
341 | { |
||||
342 | $userId = $this->update->getEffectiveUser()->getId(); |
||||
343 | return $this->cache->deleteCacheItemUserData($userId, $key); |
||||
344 | } |
||||
345 | |||||
346 | /** |
||||
347 | * Deletes all user data. |
||||
348 | * |
||||
349 | * Eg: |
||||
350 | * $ctx->deleteUserData()->then(function($result) { |
||||
351 | * |
||||
352 | * }); |
||||
353 | * |
||||
354 | * @return PromiseInterface |
||||
355 | */ |
||||
356 | public function deleteUserData(): PromiseInterface |
||||
357 | { |
||||
358 | $userId = $this->update->getEffectiveUser()->getId(); |
||||
359 | return $this->cache->deleteAllCacheUserData($userId); |
||||
360 | } |
||||
361 | |||||
362 | /** |
||||
363 | * Sets an item of the global data. |
||||
364 | * This cache is not related to any chat or user. |
||||
365 | * |
||||
366 | * Eg: |
||||
367 | * $ctx->setGlobalData('age', 21)->then(function($result) { |
||||
368 | * |
||||
369 | * }); |
||||
370 | * |
||||
371 | * @param $key |
||||
372 | * @param $data |
||||
373 | * @param $ttl |
||||
374 | * @return PromiseInterface |
||||
375 | */ |
||||
376 | public function setGlobalData($key, $data, $ttl = false) |
||||
377 | { |
||||
378 | return $this->cache->setGlobalCacheData($key, $data, $ttl); |
||||
379 | } |
||||
380 | |||||
381 | /** |
||||
382 | * Append data to an existing global cache item. The item value is always an array. |
||||
383 | * |
||||
384 | * Eg: |
||||
385 | * $ctx->appendGlobalData('users', ['Mike', 'John'])->then(function($result) { |
||||
386 | * |
||||
387 | * }); |
||||
388 | * |
||||
389 | * @param $key |
||||
390 | * @param $data |
||||
391 | * @param $ttl |
||||
392 | * @return PromiseInterface |
||||
393 | */ |
||||
394 | public function appendGlobalData($key, $data, $ttl = false): PromiseInterface |
||||
395 | { |
||||
396 | return $this->cache->appendGlobalCacheData($key, $data, $ttl); |
||||
397 | } |
||||
398 | |||||
399 | /** |
||||
400 | * Returns all the global data. |
||||
401 | * This cache is not related to any chat or user. |
||||
402 | * |
||||
403 | * Eg: |
||||
404 | * $ctx->getGlobalData()->then(function($data) { |
||||
405 | * $age = $data['age']; |
||||
406 | * }); |
||||
407 | * |
||||
408 | * @return PromiseInterface |
||||
409 | */ |
||||
410 | public function getGlobalData(): PromiseInterface |
||||
411 | { |
||||
412 | return $this->cache->getGlobalCacheData(); |
||||
413 | } |
||||
414 | |||||
415 | /** |
||||
416 | * Gets an item of the global data. |
||||
417 | * This cache is not related to any chat or user. |
||||
418 | * |
||||
419 | * Eg: |
||||
420 | * $ctx->getGlobalDataItem('age')->then(function($age) { |
||||
421 | * |
||||
422 | * }); |
||||
423 | * |
||||
424 | * @param $key |
||||
425 | * @return PromiseInterface |
||||
426 | */ |
||||
427 | public function getGlobalDataItem($key): PromiseInterface |
||||
428 | { |
||||
429 | return $this->cache->getCacheGlobalDataItem($key); |
||||
430 | } |
||||
431 | |||||
432 | /** |
||||
433 | * Deletes an item from the global data. |
||||
434 | * This cache is not related to any chat or user. |
||||
435 | * |
||||
436 | * Eg: |
||||
437 | * $ctx->deleteGlobalDataItem('age')->then(function($result) { |
||||
438 | * |
||||
439 | * }); |
||||
440 | * |
||||
441 | * @param $key |
||||
442 | * @return PromiseInterface |
||||
443 | */ |
||||
444 | public function deleteGlobalDataItem($key): PromiseInterface |
||||
445 | { |
||||
446 | return $this->cache->deleteCacheItemGlobalData($key); |
||||
447 | } |
||||
448 | |||||
449 | /** |
||||
450 | * Deletes all global data. |
||||
451 | * |
||||
452 | * Eg: |
||||
453 | * $ctx->deleteGlobalData()->then(function($result) { |
||||
454 | * |
||||
455 | * }); |
||||
456 | * |
||||
457 | * @return PromiseInterface |
||||
458 | */ |
||||
459 | public function deleteGlobalData(): PromiseInterface |
||||
460 | { |
||||
461 | return $this->cache->deleteCacheGlobalData(); |
||||
462 | } |
||||
463 | |||||
464 | /** |
||||
465 | * Wipe entire cache. |
||||
466 | * |
||||
467 | * @return PromiseInterface |
||||
468 | */ |
||||
469 | public function wipeCache(): PromiseInterface |
||||
470 | { |
||||
471 | return $this->cache->wipeCache(); |
||||
472 | } |
||||
473 | |||||
474 | /** |
||||
475 | * Get container instance |
||||
476 | * @return ContainerInterface |
||||
477 | */ |
||||
478 | public function getContainer(): ContainerInterface |
||||
479 | { |
||||
480 | return $this->container; |
||||
481 | } |
||||
482 | |||||
483 | /** |
||||
484 | * @return LoopInterface |
||||
485 | */ |
||||
486 | public function getLoop(): LoopInterface |
||||
487 | { |
||||
488 | return $this->container->get(LoopInterface::class); |
||||
489 | } |
||||
490 | |||||
491 | /** |
||||
492 | * @return bool |
||||
493 | */ |
||||
494 | public function isCallbackQuery(): bool |
||||
495 | { |
||||
496 | return $this->getCallbackQuery() !== null; |
||||
497 | } |
||||
498 | } |
||||
499 |