Completed
Push — develop ( 32d851...a318ff )
by Michele
87:15 queued 85:13
created

Context::appendUserData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
     * Append data to an existing chat cache item. The item value is always an array.
192
     *
193
     * Eg:
194
     * $ctx->appendChatData('users', ['Mike', 'John'])->then(function($result) {
195
     *
196
     * });
197
     *
198
     * @param $key
199
     * @param $data
200
     * @return PromiseInterface
201
     */
202
    public function appendChatData($key, $data): PromiseInterface
203
    {
204
        $chatId = $this->update->getEffectiveChat()->getId();
205
        return $this->cache->appendCacheChatData($chatId, $key, $data);
206
    }
207
208
    /**
209
     * Deletes an item from the chat data.
210
     *
211
     * Eg:
212
     * $ctx->deleteChatDataItem('age')->then(function($result) {
213
     *
214
     * });
215
     *
216
     * @param $key
217
     * @return PromiseInterface
218
     */
219
    public function deleteChatDataItem($key): PromiseInterface
220
    {
221
        $chatId = $this->update->getEffectiveChat()->getId();
222
        return $this->cache->deleteCacheChatDataItem($chatId, $key);
223
    }
224
225
    /**
226
     * Deletes all chat data.
227
     *
228
     * Eg:
229
     * $ctx->deleteChatData()->then(function($result) {
230
     *
231
     * });
232
     *
233
     * @return PromiseInterface
234
     */
235
    public function deleteChatData(): PromiseInterface
236
    {
237
        $chatId = $this->update->getEffectiveChat()->getId();
238
        return $this->cache->deleteAllCacheChatData($chatId);
239
    }
240
241
    /**
242
     * Returns all the user-related data.
243
     *
244
     * Eg:
245
     * $ctx->getUserData()->then(function($data) {
246
     *      $age = $data['age'];
247
     * });
248
     *
249
     * @return PromiseInterface
250
     */
251
    public function getUserData(): PromiseInterface
252
    {
253
        $userId = $this->update->getEffectiveUser()->getId();
254
        return $this->cache->getCacheUserData($userId);
255
    }
256
257
    /**
258
     * Gets an item of the user data.
259
     *
260
     * Eg:
261
     * $ctx->getUserDataItem('age')->then(function($age) {
262
     *
263
     * });
264
     *
265
     * @param $key
266
     * @return PromiseInterface
267
     */
268
    public function getUserDataItem($key): PromiseInterface
269
    {
270
        $userId = $this->update->getEffectiveUser()->getId();
271
        return $this->cache->getCacheUserDataItem($userId, $key);
272
    }
273
274
    /**
275
     * Sets an item of the user data.
276
     *
277
     * Eg:
278
     * $ctx->setUserData('age', 21)->then(function($result) {
279
     *
280
     * });
281
     *
282
     * @param $key
283
     * @param $data
284
     * @return PromiseInterface
285
     */
286
    public function setUserData($key, $data): PromiseInterface
287
    {
288
        $userId = $this->update->getEffectiveUser()->getId();
289
        return $this->cache->setCacheUserData($userId, $key, $data);
290
    }
291
292
    /**
293
     * Append data to an existing user cache item. The item value is always an array.
294
     *
295
     * Eg:
296
     * $ctx->appendUserData('users', ['Mike', 'John'])->then(function($result) {
297
     *
298
     * });
299
     *
300
     * @param $key
301
     * @param $data
302
     * @return PromiseInterface
303
     */
304
    public function appendUserData($key, $data): PromiseInterface
305
    {
306
        $userId = $this->update->getEffectiveUser()->getId();
307
        return $this->cache->appendCacheUserData($userId, $key, $data);
308
    }
309
310
    /**
311
     * Deletes an item from the user data.
312
     *
313
     * Eg:
314
     * $ctx->deleteUserDataItem('age')->then(function($result) {
315
     *
316
     * });
317
     *
318
     * @param $key
319
     * @return PromiseInterface
320
     */
321
    public function deleteUserDataItem($key): PromiseInterface
322
    {
323
        $userId = $this->update->getEffectiveUser()->getId();
324
        return $this->cache->deleteCacheItemUserData($userId, $key);
325
    }
326
327
    /**
328
     * Deletes all user data.
329
     *
330
     * Eg:
331
     * $ctx->deleteUserData()->then(function($result) {
332
     *
333
     * });
334
     *
335
     * @return PromiseInterface
336
     */
337
    public function deleteUserData(): PromiseInterface
338
    {
339
        $userId = $this->update->getEffectiveUser()->getId();
340
        return $this->cache->deleteAllCacheUserData($userId);
341
    }
342
343
    /**
344
     * Sets an item of the global data.
345
     * This cache is not related to any chat or user.
346
     *
347
     * Eg:
348
     * $ctx->setGlobalData('age', 21)->then(function($result) {
349
     *
350
     * });
351
     *
352
     * @param $key
353
     * @param $data
354
     * @return PromiseInterface
355
     */
356
    public function setGlobalData($key, $data)
357
    {
358
        return $this->cache->setGlobalCacheData($key, $data);
359
    }
360
361
    /**
362
     * Append data to an existing global cache item. The item value is always an array.
363
     *
364
     * Eg:
365
     * $ctx->appendGlobalData('users', ['Mike', 'John'])->then(function($result) {
366
     *
367
     * });
368
     *
369
     * @param $key
370
     * @param $data
371
     * @return PromiseInterface
372
     */
373
    public function appendGlobalData($key, $data): PromiseInterface
374
    {
375
        return $this->cache->appendGlobalCacheData($key, $data);
376
    }
377
378
    /**
379
     * Returns all the global data.
380
     * This cache is not related to any chat or user.
381
     *
382
     * Eg:
383
     * $ctx->getGlobalData()->then(function($data) {
384
     *      $age = $data['age'];
385
     * });
386
     *
387
     * @return PromiseInterface
388
     */
389
    public function getGlobalData(): PromiseInterface
390
    {
391
        return $this->cache->getGlobalCacheData();
392
    }
393
394
    /**
395
     * Gets an item of the global data.
396
     * This cache is not related to any chat or user.
397
     *
398
     * Eg:
399
     * $ctx->getGlobalDataItem('age')->then(function($age) {
400
     *
401
     * });
402
     *
403
     * @param $key
404
     * @return PromiseInterface
405
     */
406
    public function getGlobalDataItem($key): PromiseInterface
407
    {
408
        return $this->cache->getCacheGlobalDataItem($key);
409
    }
410
411
    /**
412
     * Deletes an item from the global data.
413
     * This cache is not related to any chat or user.
414
     *
415
     * Eg:
416
     * $ctx->deleteGlobalDataItem('age')->then(function($result) {
417
     *
418
     * });
419
     *
420
     * @param $key
421
     * @return PromiseInterface
422
     */
423
    public function deleteGlobalDataItem($key): PromiseInterface
424
    {
425
        return $this->cache->deleteCacheItemGlobalData($key);
426
    }
427
428
    /**
429
     * Deletes all global data.
430
     *
431
     * Eg:
432
     * $ctx->deleteGlobalData()->then(function($result) {
433
     *
434
     * });
435
     *
436
     * @return PromiseInterface
437
     */
438
    public function deleteGlobalData(): PromiseInterface
439
    {
440
        return $this->cache->deleteCacheGlobalData();
441
    }
442
443
    /**
444
     * Wipe entire cache.
445
     *
446
     * @return PromiseInterface
447
     */
448
    public function wipeCache(): PromiseInterface
449
    {
450
        return $this->cache->wipeCache();
451
    }
452
453
    /**
454
     * Get container instance
455
     * @return ContainerInterface
456
     */
457
    public function getContainer(): ContainerInterface
458
    {
459
        return $this->container;
460
    }
461
462
}
463