|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zanzara; |
|
4
|
|
|
|
|
5
|
|
|
use React\Cache\CacheInterface; |
|
6
|
|
|
use React\Promise\PromiseInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @method get($key, $default = null) |
|
10
|
|
|
* @method set($key, $value, $ttl = null) |
|
11
|
|
|
* @method delete($key) |
|
12
|
|
|
* @method setMultiple(array $values, $ttl = null) |
|
13
|
|
|
* @method deleteMultiple(array $keys) |
|
14
|
|
|
* @method clear() |
|
15
|
|
|
* @method has($key) |
|
16
|
|
|
*/ |
|
17
|
|
|
class ZanzaraCache |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var CacheInterface|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $cache; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ZanzaraLogger |
|
26
|
|
|
*/ |
|
27
|
|
|
private $logger; |
|
28
|
|
|
|
|
29
|
|
|
private const CONVERSATION = "CONVERSATION"; |
|
30
|
|
|
|
|
31
|
|
|
private const CHATDATA = " CHATDATA"; |
|
32
|
|
|
|
|
33
|
|
|
private const USERDATA = "USERDATA"; |
|
34
|
|
|
|
|
35
|
|
|
private const GLOBALDATA = "GLOBALDATA"; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* ZanzaraLogger constructor. |
|
39
|
|
|
* @param CacheInterface|null $cache |
|
40
|
|
|
* @param ZanzaraLogger $logger |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(?CacheInterface $cache, ZanzaraLogger $logger) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->logger = $logger; |
|
45
|
|
|
$this->cache = $cache; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getGlobalCacheData() |
|
49
|
|
|
{ |
|
50
|
|
|
$cacheKey = self::GLOBALDATA; |
|
51
|
|
|
return $this->doGet($cacheKey); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setGlobalCacheData(string $key, $data) |
|
55
|
|
|
{ |
|
56
|
|
|
$cacheKey = self::GLOBALDATA; |
|
57
|
|
|
return $this->doSet($cacheKey, $key, $data); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getCacheGlobalDataItem(string $key) |
|
61
|
|
|
{ |
|
62
|
|
|
$cacheKey = self::GLOBALDATA; |
|
63
|
|
|
return $this->getCacheItem($cacheKey, $key); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function deleteCacheGlobalData() |
|
67
|
|
|
{ |
|
68
|
|
|
$cacheKey = self::GLOBALDATA; |
|
69
|
|
|
return $this->deleteCache([$cacheKey]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function deleteCacheItemGlobalData(string $key) |
|
73
|
|
|
{ |
|
74
|
|
|
$cacheKey = self::GLOBALDATA; |
|
75
|
|
|
return $this->deleteCacheItem($cacheKey, $key); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the correct key value for chatId data stored in cache |
|
80
|
|
|
* @param $chatId |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
private function getChatIdKey(int $chatId) |
|
84
|
|
|
{ |
|
85
|
|
|
return ZanzaraCache::CHATDATA . strval($chatId); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getCacheChatData(int $chatId) |
|
89
|
|
|
{ |
|
90
|
|
|
$cacheKey = $this->getChatIdKey($chatId); |
|
91
|
|
|
return $this->doGet($cacheKey); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getCacheChatDataItem(int $chatId, string $key) |
|
95
|
|
|
{ |
|
96
|
|
|
$cacheKey = $this->getChatIdKey($chatId); |
|
97
|
|
|
return $this->getCacheItem($cacheKey, $key); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function setCacheChatData(int $chatId, string $key, $data) |
|
101
|
|
|
{ |
|
102
|
|
|
$cacheKey = $this->getChatIdKey($chatId); |
|
103
|
|
|
return $this->doSet($cacheKey, $key, $data); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function deleteAllCacheChatData(int $chatId) |
|
107
|
|
|
{ |
|
108
|
|
|
$cacheKey = $this->getChatIdKey($chatId); |
|
109
|
|
|
return $this->deleteCache([$cacheKey]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function deleteCacheChatDataItem(int $chatId, string $key) |
|
113
|
|
|
{ |
|
114
|
|
|
$cacheKey = $this->getChatIdKey($chatId); |
|
115
|
|
|
return $this->deleteCacheItem($cacheKey, $key); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the correct key value for userId data stored in cache |
|
120
|
|
|
* @param $userId |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
private function getUserIdKey(int $userId) |
|
124
|
|
|
{ |
|
125
|
|
|
return ZanzaraCache::USERDATA . strval($userId); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function getCacheUserData(int $userId) |
|
129
|
|
|
{ |
|
130
|
|
|
$cacheKey = $this->getUserIdKey($userId); |
|
131
|
|
|
return $this->doGet($cacheKey); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getCacheUserDataItem(int $userId, string $key) |
|
135
|
|
|
{ |
|
136
|
|
|
$cacheKey = $this->getUserIdKey($userId); |
|
137
|
|
|
return $this->getCacheItem($cacheKey, $key); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public function setCacheUserData(int $userId, string $key, $data) |
|
141
|
|
|
{ |
|
142
|
|
|
$cacheKey = $this->getUserIdKey($userId); |
|
143
|
|
|
return $this->doSet($cacheKey, $key, $data); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public function deleteAllCacheUserData(int $userId) |
|
147
|
|
|
{ |
|
148
|
|
|
$cacheKey = $this->getUserIdKey($userId); |
|
149
|
|
|
return $this->deleteCache([$cacheKey]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function deleteCacheItemUserData(int $userId, string $key) |
|
153
|
|
|
{ |
|
154
|
|
|
$cacheKey = $this->getUserIdKey($userId); |
|
155
|
|
|
return $this->deleteCacheItem($cacheKey, $key); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get key of the conversation by chatId |
|
160
|
|
|
* @param $chatId |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
private function getConversationKey(int $chatId) |
|
164
|
|
|
{ |
|
165
|
|
|
return ZanzaraCache::CONVERSATION . strval($chatId); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function setConversationHandler(int $chatId, $data) |
|
169
|
|
|
{ |
|
170
|
|
|
$key = "state"; |
|
171
|
|
|
$cacheKey = $this->getConversationKey($chatId); |
|
172
|
|
|
return $this->doSet($cacheKey, $key, $data); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* delete a cache iteam and return the promise |
|
177
|
|
|
* @param $chatId |
|
178
|
|
|
* @return PromiseInterface |
|
179
|
|
|
*/ |
|
180
|
|
|
public function deleteConversationCache(int $chatId) |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->deleteCache([$this->getConversationKey($chatId)]); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Use only to call native method of CacheInterface |
|
187
|
|
|
* @param $name |
|
188
|
|
|
* @param $arguments |
|
189
|
|
|
* @return PromiseInterface |
|
190
|
|
|
*/ |
|
191
|
|
|
public function __call($name, $arguments): ?PromiseInterface |
|
192
|
|
|
{ |
|
193
|
|
|
return call_user_func_array([$this->cache, $name], $arguments); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Delete a key inside array stored in cacheKey |
|
198
|
|
|
* @param $cacheKey |
|
199
|
|
|
* @param $key |
|
200
|
|
|
* @return PromiseInterface |
|
201
|
|
|
*/ |
|
202
|
|
|
public function deleteCacheItem(string $cacheKey, $key) |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->cache->get($cacheKey)->then(function ($arrayData) use ($cacheKey, $key) { |
|
|
|
|
|
|
205
|
|
|
if (!$arrayData) { |
|
206
|
|
|
return true; //there isn't anything so it's deleted |
|
207
|
|
|
} else { |
|
208
|
|
|
unset($arrayData[$key]); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
return $this->cache->set($cacheKey, $arrayData)->then(function ($result) { |
|
212
|
|
|
if ($result !== true) { |
|
213
|
|
|
$this->logger->errorWriteCache($result); |
|
214
|
|
|
} |
|
215
|
|
|
return $result; |
|
216
|
|
|
}); |
|
217
|
|
|
}); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* delete a cache iteam and return the promise |
|
222
|
|
|
* @param array $keys |
|
223
|
|
|
* @return PromiseInterface |
|
224
|
|
|
*/ |
|
225
|
|
|
public function deleteCache(array $keys) |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->cache->deleteMultiple($keys)->then(function ($result) { |
|
228
|
|
|
if ($result !== true) { |
|
229
|
|
|
$this->logger->errorClearCache($result); |
|
230
|
|
|
} |
|
231
|
|
|
return $result; |
|
232
|
|
|
}); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Get cache item inside array stored in cacheKey |
|
237
|
|
|
* @param $cacheKey |
|
238
|
|
|
* @param $key |
|
239
|
|
|
* @return PromiseInterface |
|
240
|
|
|
*/ |
|
241
|
|
|
public function getCacheItem(string $cacheKey, $key) |
|
242
|
|
|
{ |
|
243
|
|
|
return $this->cache->get($cacheKey)->then(function ($arrayData) use ($key) { |
|
244
|
|
|
if ($arrayData && array_key_exists($key, $arrayData)) { |
|
245
|
|
|
return $arrayData[$key]; |
|
246
|
|
|
} else { |
|
247
|
|
|
return null; |
|
248
|
|
|
} |
|
249
|
|
|
}); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function doGet(string $cacheKey) |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->cache->get($cacheKey); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Wipe entire cache. |
|
259
|
|
|
* @return PromiseInterface |
|
260
|
|
|
*/ |
|
261
|
|
|
public function wipeCache() |
|
262
|
|
|
{ |
|
263
|
|
|
return $this->cache->clear(); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* set a cache value and return the promise |
|
268
|
|
|
* @param $cacheKey |
|
269
|
|
|
* @param $key |
|
270
|
|
|
* @param $data |
|
271
|
|
|
* @return PromiseInterface |
|
272
|
|
|
*/ |
|
273
|
|
|
public function doSet(string $cacheKey, string $key, $data) |
|
274
|
|
|
{ |
|
275
|
|
|
return $this->cache->get($cacheKey)->then(function ($arrayData) use ($key, $data, $cacheKey) { |
|
276
|
|
|
if (!$arrayData) { |
|
277
|
|
|
$arrayData = array(); |
|
278
|
|
|
$arrayData[$key] = $data; |
|
279
|
|
|
} else { |
|
280
|
|
|
$arrayData[$key] = $data; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return $this->cache->set($cacheKey, $arrayData)->then(function ($result) { |
|
284
|
|
|
if ($result !== true) { |
|
285
|
|
|
$this->logger->errorWriteCache($result); |
|
286
|
|
|
} |
|
287
|
|
|
return $result; |
|
288
|
|
|
}); |
|
289
|
|
|
}); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Used by ListenerResolver to call the correct handler stored inside cache |
|
294
|
|
|
* @param $chatId |
|
295
|
|
|
* @param $update |
|
296
|
|
|
* @param $container |
|
297
|
|
|
* @return PromiseInterface |
|
298
|
|
|
*/ |
|
299
|
|
|
public function callHandlerByChatId(int $chatId, $update, $container) |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->cache->get($this->getConversationKey($chatId))->then(function (array $conversation) use ($update, $chatId, $container) { |
|
|
|
|
|
|
302
|
|
|
if (!empty($conversation)) { |
|
303
|
|
|
$handler = $conversation["state"]; |
|
304
|
|
|
$handler(new Context($update, $container)); |
|
305
|
|
|
} |
|
306
|
|
|
}, function ($err) use ($container, $update) { |
|
|
|
|
|
|
307
|
|
|
$this->logger->errorUpdate($update, $err); |
|
308
|
|
|
})->otherwise(function ($err, $update, $chatId) { |
|
|
|
|
|
|
309
|
|
|
$this->logger->errorUpdate($err, $update); |
|
310
|
|
|
$this->deleteConversationCache($chatId); |
|
311
|
|
|
}); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
} |
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.