Passed
Push — develop ( 3d2eea...96fcf0 )
by
unknown
07:38
created

Context::deleteGlobalData()   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 0
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
     * @param Update $update
58
     * @param ContainerInterface $container
59
     */
60
    public function __construct(Update $update, ContainerInterface $container)
61
    {
62
        $this->update = $update;
63
        $this->container = $container;
64
        $this->browser = $container->get(Browser::class);
65
    }
66
67
    /**
68
     * @param string $key
69
     * @param $value
70
     */
71
    public function set(string $key, $value): void
72
    {
73
        $this->data[$key] = $value;
74
    }
75
76
    /**
77
     * @param string $key
78
     * @return mixed|null
79
     */
80
    public function get(string $key)
81
    {
82
        return $this->data[$key] ?? null;
83
    }
84
85
    /**
86
     * @param $name
87
     * @param $arguments
88
     * @return mixed
89
     */
90
    public function __call($name, $arguments)
91
    {
92
        return $this->update->$name($arguments);
93
    }
94
95
    /**
96
     * @return Update
97
     */
98
    public function getUpdate(): Update
99
    {
100
        return $this->update;
101
    }
102
103
104
105
106
    /**
107
     * Save the next state with callable function. Used to go to the next conversation handler
108
     * @param callable $handler
109
     * @return PromiseInterface
110
     */
111
    public function nextStep(callable $handler)
112
    {
113
        $chatId = $this->update->getEffectiveChat()->getId();
114
        $cache = $this->container->get(ZanzaraCache::class);
115
        return $cache->setConversation($chatId, $handler);
116
    }
117
118
    /**
119
     * Clean the cache for the conversation based on chatId
120
     */
121
    public function endConversation()
122
    {
123
        $chatId = $this->update->getEffectiveChat()->getId();
124
        $cache = $this->container->get(ZanzaraCache::class);
125
        return $cache->deleteConversationCache($chatId);
126
    }
127
128
129
    /**
130
     * Return all the chat data of the context
131
     * @return PromiseInterface
132
     */
133
    public function getChatData()
134
    {
135
        $chatId = $this->update->getEffectiveChat()->getId();
136
        $cache = $this->container->get(ZanzaraCache::class);
137
        return $cache->getCacheChatData($chatId);
138
    }
139
140
    /**
141
     * Get chat data by key
142
     * @param $key
143
     * @return PromiseInterface
144
     */
145
    public function getItemChatData($key)
146
    {
147
        $chatId = $this->update->getEffectiveChat()->getId();
148
        $cache = $this->container->get(ZanzaraCache::class);
149
        return $cache->getItemCacheChatData($chatId, $key);
150
    }
151
152
    /**
153
     * Set chat data by key
154
     * @param $key
155
     * @param $data
156
     * @return PromiseInterface
157
     */
158
    public function setChatData($key, $data)
159
    {
160
        $chatId = $this->update->getEffectiveChat()->getId();
161
        $cache = $this->container->get(ZanzaraCache::class);
162
        return $cache->setCacheChatData($chatId, $key, $data);
163
    }
164
165
    /**
166
     * Delete an item of the chat data
167
     * @param $key
168
     * @return PromiseInterface
169
     */
170
    public function deleteItemChatData($key)
171
    {
172
        $chatId = $this->update->getEffectiveChat()->getId();
173
        $cache = $this->container->get(ZanzaraCache::class);
174
        return $cache->deleteCacheItemChatData($chatId, $key);
175
    }
176
177
    /**
178
     * Delete all chat data
179
     * @return PromiseInterface
180
     */
181
    public function deleteChatData()
182
    {
183
        $chatId = $this->update->getEffectiveChat()->getId();
184
        $cache = $this->container->get(ZanzaraCache::class);
185
        return $cache->deleteAllCacheChatData($chatId);
186
    }
187
188
189
    /**
190
     * Get user data by context
191
     * @return PromiseInterface
192
     */
193
    public function getUserData()
194
    {
195
        $userId = $this->update->getEffectiveUser()->getId();
196
        $cache = $this->container->get(ZanzaraCache::class);
197
        return $cache->getCacheUserData($userId);
198
    }
199
200
    /**
201
     * Get user data by key
202
     * @param $key
203
     * @return PromiseInterface
204
     */
205
    public function getItemUserData($key)
206
    {
207
        $chatId = $this->update->getEffectiveUser()->getId();
208
        $cache = $this->container->get(ZanzaraCache::class);
209
        return $cache->getItemCacheUserData($chatId, $key);
210
    }
211
212
    /**
213
     * Set user data by key
214
     * @param $key
215
     * @param $data
216
     * @return PromiseInterface
217
     */
218
    public function setUserData($key, $data)
219
    {
220
        $userId = $this->update->getEffectiveUser()->getId();
221
        $cache = $this->container->get(ZanzaraCache::class);
222
        return $cache->setCacheUserData($userId, $key, $data);
223
    }
224
225
    /**
226
     * Delete item of user data by key
227
     * @param $key
228
     * @return PromiseInterface
229
     */
230
    public function deleteItemUserData($key)
231
    {
232
        $userId = $this->update->getEffectiveUser()->getId();
233
        $cache = $this->container->get(ZanzaraCache::class);
234
        return $cache->deleteCacheItemUserData($userId, $key);
235
    }
236
237
    /**
238
     * Delete all user data
239
     * @return PromiseInterface
240
     */
241
    public function deleteUserData()
242
    {
243
        $chatId = $this->update->getEffectiveUser()->getId();
244
        $cache = $this->container->get(ZanzaraCache::class);
245
        return $cache->deleteAllCacheUserData($chatId);
246
    }
247
248
249
    /**
250
     * Set global data
251
     * @param $key
252
     * @param $data
253
     * @return PromiseInterface
254
     */
255
    public function setGlobalData($key, $data)
256
    {
257
        $cache = $this->container->get(ZanzaraCache::class);
258
        return $cache->setGlobalCacheData($key, $data);
259
    }
260
261
    /**
262
     * Gel all global data
263
     * @return PromiseInterface
264
     */
265
    public function getGlobalData()
266
    {
267
        $cache = $this->container->get(ZanzaraCache::class);
268
        return $cache->getGlobalCacheData();
269
    }
270
271
    /**
272
     * Get item of global data by key
273
     * @param $key
274
     * @return PromiseInterface
275
     */
276
    public function getItemGlobalData($key)
277
    {
278
        $cache = $this->container->get(ZanzaraCache::class);
279
        return $cache->getCacheItemGlobalData($key);
280
    }
281
282
    /**
283
     * Delete item of global data by key
284
     * @param $key
285
     * @return PromiseInterface
286
     */
287
    public function deleteItemGlobalData($key)
288
    {
289
        $cache = $this->container->get(ZanzaraCache::class);
290
        return $cache->deleteCacheItemGlobalData($key);
291
    }
292
293
    /**
294
     * Delete all global data
295
     * @return PromiseInterface
296
     */
297
    public function deleteGlobalData()
298
    {
299
        $cache = $this->container->get(ZanzaraCache::class);
300
        return $cache->deleteCacheGlobalData();
301
    }
302
303
    /**
304
     * Wipe entire cache
305
     * @return PromiseInterface
306
     */
307
    public function wipeCache()
308
    {
309
        $cache = $this->container->get(ZanzaraCache::class);
310
        return $cache->wipeCache();
311
    }
312
313
314
    /**
315
     * Get container instance
316
     * @return ContainerInterface
317
     */
318
    public function getContainer(): ContainerInterface
319
    {
320
        return $this->container;
321
    }
322
323
}
324