Passed
Pull Request — main (#34)
by Sílvio
02:57
created

Cacheer::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp;
4
5
use Closure;
6
use Silviooosilva\CacheerPhp\Interface\CacheerInterface;
7
use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore;
8
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore;
9
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore;
10
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore;
11
use Silviooosilva\CacheerPhp\Helpers\CacheConfig;
12
use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter;
13
use Silviooosilva\CacheerPhp\Utils\CacheDriver;
14
use Silviooosilva\CacheerPhp\Utils\CacheStats;
15
use Silviooosilva\CacheerPhp\Utils\CacheLogger;
16
17
/**
18
 * Class CacheerPHP
19
 * @author Sílvio Silva <https://github.com/silviooosilva>
20
 * @package Silviooosilva\CacheerPhp
21
 */
22
final class Cacheer implements CacheerInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private string $message;
28
29
    /**
30
     * @var boolean
31
     */
32
    private bool $success;
33
34
    /**
35
     * @var boolean
36
     */
37
    private bool $formatted = false;
38
39
    /**
40
     * @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore
41
     */
42
    public $cacheStore;
43
44
    /**
45
     * @var array
46
    */
47
    public array $options = [];
48
49
    /**
50
     * @var CacheStats
51
     */
52
    public CacheStats $stats;
53
54
    public function __construct(array $options = [], $formatted = false)
55
    {
56
        $this->formatted = $formatted;
57
        $this->validateOptions($options);
58
        $this->setDriver()->useDefaultDriver();
59
        $logPath = $this->options['loggerPath'] ?? 'cacheer.log';
60
        $this->stats = new CacheStats(new CacheLogger($logPath));
61
    }
62
63
    /**
64
     * @param string $cacheKey
65
     * @param mixed  $cacheData
66
     * @param string $namespace
67
     * @param int|string $ttl
68
     * @return bool
69
     */
70
    public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
71
    {
72
        if (!empty($this->getCache($cacheKey, $namespace))) {
73
            return true;
74
        }
75
76
        $this->putCache($cacheKey, $cacheData, $namespace, $ttl);
77
        $this->setMessage($this->getMessage(), $this->isSuccess());
78
79
        return false;
80
    }
81
82
    /**
83
     * @param string $cacheKey
84
     * @param mixed  $cacheData
85
     * @param string $namespace
86
     * @return void
87
     */
88
    public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
89
    {
90
        $this->cacheStore->appendCache($cacheKey, $cacheData, $namespace);
91
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
92
    }
93
94
    /**
95
     * @param string $cacheKey
96
     * @param string $namespace
97
     * @return void
98
     */
99
    public function clearCache(string $cacheKey, string $namespace = '')
100
    {
101
        $this->cacheStore->clearCache($cacheKey, $namespace);
102
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
103
    }
104
105
    /**
106
     * @param string $cacheKey
107
     * @param int $amount
108
     * @param string $namespace
109
     * @return bool
110
     */
111
    public function decrement(string $cacheKey, int $amount = 1, string $namespace = '')
112
    {
113
        return $this->increment($cacheKey, ($amount * -1), $namespace);
114
    }
115
116
    /**
117
     * @param string $cacheKey
118
     * @param mixed $cacheData
119
     * @return void
120
     */
121
    public function forever(string $cacheKey, mixed $cacheData)
122
    {
123
        $this->putCache($cacheKey, $cacheData, ttl: 31536000 * 1000);
124
        $this->setMessage($this->getMessage(), $this->isSuccess());
125
    }
126
127
    /**
128
     * @return void
129
     */
130
    public function flushCache()
131
    {
132
        $this->cacheStore->flushCache();
133
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
134
    }
135
136
    /**
137
     * @param string $cacheKey
138
     * @param string $namespace
139
     * @return mixed
140
     */
141
    public function getAndForget(string $cacheKey, string $namespace = '')
142
    {
143
        $cachedData = $this->getCache($cacheKey, $namespace);
144
145
        if (!empty($cachedData)) {
146
            $this->setMessage("Cache retrieved and deleted successfully!", true);
147
            $this->clearCache($cacheKey, $namespace);
148
            return $cachedData;
149
        }
150
151
        return null;
152
    }
153
154
    /**
155
     * @param string $cacheKey
156
     * @param string $namespace
157
     * @param string|int $ttl
158
     * @return CacheDataFormatter|mixed
159
     */
160
    public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600)
161
    {
162
        $start = microtime(true);
163
        $cacheData = $this->cacheStore->getCache($cacheKey, $namespace, $ttl);
164
        $duration = microtime(true) - $start;
165
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
166
167
        if ($this->cacheStore->isSuccess() && $cacheData !== null && $cacheData !== false) {
168
            $this->stats->recordHit($duration);
169
        } else {
170
            $this->stats->recordMiss($duration);
171
        }
172
173
        return $this->formatted ? new CacheDataFormatter($cacheData) : $cacheData;
174
    }
175
176
    /**
177
     * @param string $cacheKey
178
     * @param string $namespace
179
     * @return void
180
     */
181
    public function has(string $cacheKey, string $namespace = '')
182
    {
183
        $this->cacheStore->has($cacheKey, $namespace);
184
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
185
    }
186
187
    /**
188
     * @param string $cacheKey
189
     * @param int $amount
190
     * @param string $namespace
191
     * @return bool
192
     */
193
    public function increment(string $cacheKey, int $amount = 1, string $namespace = '')
194
    {
195
        $cacheData = $this->getCache($cacheKey, $namespace);
196
197
        if(!empty($cacheData) && is_numeric($cacheData)) {
198
            $this->putCache($cacheKey, (int)($cacheData + $amount), $namespace);
199
            $this->setMessage($this->getMessage(), $this->isSuccess());
200
            return true;
201
        }
202
203
        return false;
204
    }
205
206
    /**
207
     * @return boolean
208
     */
209
    public function isSuccess()
210
    {
211
        return $this->success;
212
    }
213
214
    /**
215
     * @param string $cacheKey
216
     * @param mixed  $cacheData
217
     * @param string $namespace
218
     * @param string|int $ttl
219
     * @return void
220
     */
221
    public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600)
222
    {
223
        $start = microtime(true);
224
        $this->cacheStore->putCache($cacheKey, $cacheData, $namespace, $ttl);
225
        $duration = microtime(true) - $start;
226
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
227
        $this->stats->recordWrite($duration);
228
    }
229
230
    /**
231
     * @param array   $items
232
     * @param string  $namespace
233
     * @param integer $batchSize
234
     * @return void
235
     */
236
    public function putMany(array $items, string $namespace = '', int $batchSize = 100)
237
    {
238
        $this->cacheStore->putMany($items, $namespace, $batchSize);
239
    }
240
241
    /**
242
     * @param string $cacheKey
243
     * @param string|int $ttl
244
     * @param string $namespace
245
     * @return mixed
246
     */
247
    public function renewCache(string $cacheKey, string|int $ttl = 3600, string $namespace = '')
248
    {
249
        $renewedCache = $this->cacheStore->renewCache($cacheKey, $ttl, $namespace);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $renewedCache is correct as $this->cacheStore->renew...eKey, $ttl, $namespace) targeting Silviooosilva\CacheerPhp...acheStore::renewCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the assignment to $renewedCache is correct as $this->cacheStore->renew...eKey, $ttl, $namespace) targeting Silviooosilva\CacheerPhp...acheStore::renewCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the assignment to $renewedCache is correct as $this->cacheStore->renew...eKey, $ttl, $namespace) targeting Silviooosilva\CacheerPhp...acheStore::renewCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
Are you sure the assignment to $renewedCache is correct as $this->cacheStore->renew...eKey, $ttl, $namespace) targeting Silviooosilva\CacheerPhp...acheStore::renewCache() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
250
251
        if ($this->cacheStore->isSuccess()) {
252
            $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
253
        } else {
254
            $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
255
        }
256
257
        return $renewedCache;
258
    }
259
260
    /**
261
     * @param string $cacheKey
262
     * @param int|string $ttl
263
     * @param Closure $callback
264
     * @return mixed
265
     */
266
    public function remember(string $cacheKey, int|string $ttl, Closure $callback)
267
    {
268
        $cachedData = $this->getCache($cacheKey, ttl: $ttl);
269
270
        if(!empty($cachedData)) {
271
            return $cachedData;
272
        }
273
274
        $cacheData = $callback();
275
        $this->putCache($cacheKey, $cacheData, ttl: $ttl);
276
        $this->setMessage($this->getMessage(), $this->isSuccess());
277
278
        return $cacheData;
279
    }
280
281
    /**
282
     * @param string $cacheKey
283
     * @param Closure $callback
284
     * @return mixed
285
     */
286
    public function rememberForever(string $cacheKey, Closure $callback)
287
    {
288
        return $this->remember($cacheKey, 31536000 * 1000, $callback);
289
    }
290
291
    /**
292
     * @return CacheConfig
293
     */
294
    public function setConfig()
295
    {
296
        return new CacheConfig($this);
297
    }
298
299
    /**
300
     * @return CacheDriver
301
     */
302
    public function setDriver()
303
    {
304
        return new CacheDriver($this);
305
    }
306
307
    /**
308
     * @param string  $message
309
     * @param boolean $success
310
     * @return void
311
     */
312
    private function setMessage(string $message, bool $success)
313
    {
314
        $this->message = $message;
315
        $this->success = $success;
316
    }
317
318
    /**
319
     * @return string
320
     */
321
    public function getMessage()
322
    {
323
        return $this->message;
324
    }
325
326
    /**
327
     * @return void
328
     */
329
    public function useFormatter()
330
    {
331
        $this->formatted = !$this->formatted;
332
    }
333
334
    /**
335
     * @return CacheStats
336
     */
337
    public function getStats()
338
    {
339
        return $this->stats;
340
    }
341
342
    /**
343
     * @param array $options
344
     * @return void
345
     */
346
    private function validateOptions(array $options)
347
    {
348
        $this->options = $options;
349
    }
350
}
351