Passed
Branch develop (0d58e4)
by Michele
02:41
created

Context::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zanzara;
6
7
use Clue\React\Buzz\Browser;
8
use Psr\Container\ContainerInterface;
9
use React\Promise\PromiseInterface;
10
use Zanzara\Telegram\TelegramTrait;
11
use Zanzara\Telegram\Type\CallbackQuery;
12
use Zanzara\Telegram\Type\ChannelPost;
13
use Zanzara\Telegram\Type\Chat;
14
use Zanzara\Telegram\Type\ChosenInlineResult;
15
use Zanzara\Telegram\Type\EditedChannelPost;
16
use Zanzara\Telegram\Type\EditedMessage;
17
use Zanzara\Telegram\Type\InlineQuery;
18
use Zanzara\Telegram\Type\Message;
19
use Zanzara\Telegram\Type\Poll\Poll;
20
use Zanzara\Telegram\Type\Poll\PollAnswer;
21
use Zanzara\Telegram\Type\Shipping\PreCheckoutQuery;
22
use Zanzara\Telegram\Type\Shipping\ShippingQuery;
23
use Zanzara\Telegram\Type\Update;
24
use Zanzara\Telegram\Type\User;
25
26
/**
27
 * Update shortcut methods
28
 * @method int getUpdateId()
29
 * @method Message|null getMessage()
30
 * @method EditedMessage|null getEditedMessage()
31
 * @method ChannelPost|null getChannelPost()
32
 * @method EditedChannelPost|null getEditedChannelPost()
33
 * @method InlineQuery|null getInlineQuery()
34
 * @method ChosenInlineResult|null getChosenInlineResult()
35
 * @method CallbackQuery|null getCallbackQuery()
36
 * @method ShippingQuery|null getShippingQuery()
37
 * @method PreCheckoutQuery|null getPreCheckoutQuery()
38
 * @method Poll|null getPoll()
39
 * @method PollAnswer|null getPollAnswer()
40
 * @method User|null getEffectiveUser()
41
 * @method Chat|null getEffectiveChat()
42
 *
43
 * @see Update
44
 */
45
class Context
46
{
47
    use TelegramTrait;
0 ignored issues
show
Bug introduced by
The trait Zanzara\Telegram\TelegramTrait requires the property $result which is not provided by Zanzara\Context.
Loading history...
48
49
    /**
50
     * Array used to pass data between middleware.
51
     *
52
     * @var array
53
     */
54
    private $data = [];
55
56
    /**
57
     * @var ZanzaraCache
58
     */
59
    private $cache;
60
61
    /**
62
     * @param Update $update
63
     * @param ContainerInterface $container
64
     */
65
    public function __construct(Update $update, ContainerInterface $container)
66
    {
67
        $this->update = $update;
68
        $this->container = $container;
69
        $this->browser = $container->get(Browser::class);
70
        $this->cache = $container->get(ZanzaraCache::class);
71
    }
72
73
    /**
74
     * @param string $key
75
     * @param $value
76
     */
77
    public function set(string $key, $value): void
78
    {
79
        $this->data[$key] = $value;
80
    }
81
82
    /**
83
     * @param string $key
84
     * @return mixed|null
85
     */
86
    public function get(string $key)
87
    {
88
        return $this->data[$key] ?? null;
89
    }
90
91
    /**
92
     * @param $name
93
     * @param $arguments
94
     * @return mixed
95
     */
96
    public function __call($name, $arguments)
97
    {
98
        return $this->update->$name($arguments);
99
    }
100
101
    /**
102
     * @return Update
103
     */
104
    public function getUpdate(): Update
105
    {
106
        return $this->update;
107
    }
108
109
    /**
110
     * Used to either start a new conversation or to set next step handler for an existing conversation.
111
     * Conversations are based on chat_id.
112
     * By default a in-memory cache is used to keep the conversation's state.
113
     * See https://github.com/badfarm/zanzara/wiki#conversations-and-user-data-cache.
114
     * Use the returned promise to know if the operation was successful.
115
     *
116
     * @param callable $handler This callable must be take on parameter of type Context
117
     * @return PromiseInterface
118
     */
119
    public function nextStep(callable $handler): PromiseInterface
120
    {
121
        $chatId = $this->update->getEffectiveChat()->getId();
122
        return $this->cache->setConversationHandler($chatId, $handler);
123
    }
124
125
    /**
126
     * Ends the conversation by cleaning the cache.
127
     * Conversations are based on chat_id.
128
     * See https://github.com/badfarm/zanzara/wiki#conversations-and-user-data-cache.
129
     * Use the returned promise to know if the operation was successful.
130
     *
131
     * @return PromiseInterface
132
     */
133
    public function endConversation(): PromiseInterface
134
    {
135
        $chatId = $this->update->getEffectiveChat()->getId();
136
        return $this->cache->deleteConversationCache($chatId);
137
    }
138
139
    /**
140
     * Returns all the chat-related data.
141
     *
142
     * Eg:
143
     * $ctx->getChatData()->then(function($data) {
144
     *      $age = $data['age'];
145
     * });
146
     *
147
     * @return PromiseInterface
148
     */
149
    public function getChatData()
150
    {
151
        $chatId = $this->update->getEffectiveChat()->getId();
152
        return $this->cache->getCacheChatData($chatId);
153
    }
154
155
    /**
156
     * Gets an item of the chat data.
157
     *
158
     * Eg:
159
     * $ctx->getChatDataItem('age')->then(function($age) {
160
     *
161
     * });
162
     *
163
     * @param $key
164
     * @return PromiseInterface
165
     */
166
    public function getChatDataItem($key): PromiseInterface
167
    {
168
        $chatId = $this->update->getEffectiveChat()->getId();
169
        return $this->cache->getCacheChatDataItem($chatId, $key);
170
    }
171
172
    /**
173
     * Sets an item of the chat data.
174
     *
175
     * Eg:
176
     * $ctx->setChatData('age', 21)->then(function($result) {
177
     *
178
     * });
179
     *
180
     * @param $key
181
     * @param $data
182
     * @return PromiseInterface
183
     */
184
    public function setChatData($key, $data): PromiseInterface
185
    {
186
        $chatId = $this->update->getEffectiveChat()->getId();
187
        return $this->cache->setCacheChatData($chatId, $key, $data);
188
    }
189
190
    /**
191
     * Deletes an item from the chat data.
192
     *
193
     * Eg:
194
     * $ctx->deleteChatDataItem('age')->then(function($result) {
195
     *
196
     * });
197
     *
198
     * @param $key
199
     * @return PromiseInterface
200
     */
201
    public function deleteChatDataItem($key): PromiseInterface
202
    {
203
        $chatId = $this->update->getEffectiveChat()->getId();
204
        return $this->cache->deleteCacheChatDataItem($chatId, $key);
205
    }
206
207
    /**
208
     * Deletes all chat data.
209
     *
210
     * Eg:
211
     * $ctx->deleteChatData()->then(function($result) {
212
     *
213
     * });
214
     *
215
     * @return PromiseInterface
216
     */
217
    public function deleteChatData(): PromiseInterface
218
    {
219
        $chatId = $this->update->getEffectiveChat()->getId();
220
        return $this->cache->deleteAllCacheChatData($chatId);
221
    }
222
223
    /**
224
     * Returns all the user-related data.
225
     *
226
     * Eg:
227
     * $ctx->getUserData()->then(function($data) {
228
     *      $age = $data['age'];
229
     * });
230
     *
231
     * @return PromiseInterface
232
     */
233
    public function getUserData(): PromiseInterface
234
    {
235
        $userId = $this->update->getEffectiveUser()->getId();
236
        return $this->cache->getCacheUserData($userId);
237
    }
238
239
    /**
240
     * Gets an item of the user data.
241
     *
242
     * Eg:
243
     * $ctx->getUserDataItem('age')->then(function($age) {
244
     *
245
     * });
246
     *
247
     * @param $key
248
     * @return PromiseInterface
249
     */
250
    public function getUserDataItem($key): PromiseInterface
251
    {
252
        $userId = $this->update->getEffectiveUser()->getId();
253
        return $this->cache->getCacheUserDataItem($userId, $key);
254
    }
255
256
    /**
257
     * Sets an item of the user data.
258
     *
259
     * Eg:
260
     * $ctx->setUserData('age', 21)->then(function($result) {
261
     *
262
     * });
263
     *
264
     * @param $key
265
     * @param $data
266
     * @return PromiseInterface
267
     */
268
    public function setUserData($key, $data): PromiseInterface
269
    {
270
        $userId = $this->update->getEffectiveUser()->getId();
271
        return $this->cache->setCacheUserData($userId, $key, $data);
272
    }
273
274
    /**
275
     * Deletes an item from the user data.
276
     *
277
     * Eg:
278
     * $ctx->deleteUserDataItem('age')->then(function($result) {
279
     *
280
     * });
281
     *
282
     * @param $key
283
     * @return PromiseInterface
284
     */
285
    public function deleteUserDataItem($key): PromiseInterface
286
    {
287
        $userId = $this->update->getEffectiveUser()->getId();
288
        return $this->cache->deleteCacheItemUserData($userId, $key);
289
    }
290
291
    /**
292
     * Deletes all user data.
293
     *
294
     * Eg:
295
     * $ctx->deleteUserData()->then(function($result) {
296
     *
297
     * });
298
     *
299
     * @return PromiseInterface
300
     */
301
    public function deleteUserData(): PromiseInterface
302
    {
303
        $userId = $this->update->getEffectiveUser()->getId();
304
        return $this->cache->deleteAllCacheUserData($userId);
305
    }
306
307
    /**
308
     * Sets an item of the global data.
309
     * This cache is not related to any chat or user.
310
     *
311
     * Eg:
312
     * $ctx->setGlobalData('age', 21)->then(function($result) {
313
     *
314
     * });
315
     *
316
     * @param $key
317
     * @param $data
318
     * @return PromiseInterface
319
     */
320
    public function setGlobalData($key, $data)
321
    {
322
        return $this->cache->setGlobalCacheData($key, $data);
323
    }
324
325
    /**
326
     * Returns all the global data.
327
     * This cache is not related to any chat or user.
328
     *
329
     * Eg:
330
     * $ctx->getGlobalData()->then(function($data) {
331
     *      $age = $data['age'];
332
     * });
333
     *
334
     * @return PromiseInterface
335
     */
336
    public function getGlobalData(): PromiseInterface
337
    {
338
        return $this->cache->getGlobalCacheData();
339
    }
340
341
    /**
342
     * Gets an item of the global data.
343
     * This cache is not related to any chat or user.
344
     *
345
     * Eg:
346
     * $ctx->getGlobalDataItem('age')->then(function($age) {
347
     *
348
     * });
349
     *
350
     * @param $key
351
     * @return PromiseInterface
352
     */
353
    public function getGlobalDataItem($key): PromiseInterface
354
    {
355
        return $this->cache->getCacheGlobalDataItem($key);
356
    }
357
358
    /**
359
     * Deletes an item from the global data.
360
     * This cache is not related to any chat or user.
361
     *
362
     * Eg:
363
     * $ctx->deleteGlobalDataItem('age')->then(function($result) {
364
     *
365
     * });
366
     *
367
     * @param $key
368
     * @return PromiseInterface
369
     */
370
    public function deleteGlobalDataItem($key): PromiseInterface
371
    {
372
        return $this->cache->deleteCacheItemGlobalData($key);
373
    }
374
375
    /**
376
     * Deletes all global data.
377
     *
378
     * Eg:
379
     * $ctx->deleteGlobalData()->then(function($result) {
380
     *
381
     * });
382
     *
383
     * @return PromiseInterface
384
     */
385
    public function deleteGlobalData(): PromiseInterface
386
    {
387
        return $this->cache->deleteCacheGlobalData();
388
    }
389
390
    /**
391
     * Wipe entire cache.
392
     *
393
     * @return PromiseInterface
394
     */
395
    public function wipeCache(): PromiseInterface
396
    {
397
        return $this->cache->wipeCache();
398
    }
399
400
    /**
401
     * Get container instance
402
     * @return ContainerInterface
403
     */
404
    public function getContainer(): ContainerInterface
405
    {
406
        return $this->container;
407
    }
408
409
}
410