Passed
Push — develop ( c9c887...c18356 )
by Michele
53s queued 11s
created

ZanzaraCache   A

Complexity

Total Complexity 39

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Importance

Changes 7
Bugs 3 Features 1
Metric Value
eloc 90
c 7
b 3
f 1
dl 0
loc 308
rs 9.28
wmc 39

30 Methods

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