Cacheer   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 414
Duplicated Lines 0 %

Importance

Changes 16
Bugs 1 Features 1
Metric Value
eloc 49
c 16
b 1
f 1
dl 0
loc 414
rs 9.84
wmc 32

32 Methods

Rating   Name   Duplication   Size   Complexity  
A increment() 0 3 1
A flushCache() 0 3 1
A useEncryption() 0 4 1
A forever() 0 3 1
A getEncryptionKey() 0 3 1
A renewCache() 0 3 1
A rememberForever() 0 3 1
A validateOptions() 0 3 1
A useCompression() 0 4 1
A getCache() 0 3 1
A has() 0 3 1
A add() 0 3 1
A clearCache() 0 3 1
A putCache() 0 3 1
A getAll() 0 3 1
A isCompressionEnabled() 0 3 1
A setMessage() 0 4 1
A remember() 0 3 1
A putMany() 0 3 1
A __construct() 0 7 1
A getMessage() 0 3 1
A useFormatter() 0 3 1
A appendCache() 0 3 1
A getAndForget() 0 3 1
A decrement() 0 3 1
A isSuccess() 0 3 1
A setDriver() 0 3 1
A isFormatted() 0 3 1
A setConfig() 0 3 1
A getMany() 0 3 1
A setInternalState() 0 3 1
A syncState() 0 3 1
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 RuntimeException;
15
use Silviooosilva\CacheerPhp\Service\CacheRetriever;
16
use Silviooosilva\CacheerPhp\Service\CacheMutator;
17
18
/**
19
* Class CacheerPHP
20
* @author Sílvio Silva <https://github.com/silviooosilva>
21
* @package Silviooosilva\CacheerPhp
22
*/
23
final class Cacheer implements CacheerInterface
24
{
25
    /**
26
    * @var string
27
    */
28
    private string $message;
29
30
    /**
31
    * @var boolean
32
    */
33
    private bool $success;
34
35
    /**
36
    * @var boolean
37
    */
38
    private bool $formatted = false;
39
40
    /**
41
    * @var bool
42
    */
43
    private bool $compression = false;
44
45
    /**
46
    * @var string|null
47
    */
48
    private ?string $encryptionKey = null;
49
50
    /**
51
    * @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore
52
    */
53
    public RedisCacheStore|DatabaseCacheStore|ArrayCacheStore|FileCacheStore $cacheStore;
54
55
    /**
56
    * @var array
57
    */
58
    public array $options = [];
59
60
    /**
61
    * @var CacheRetriever
62
    */
63
    private CacheRetriever $retriever;
64
65
    /**
66
    * @var CacheMutator
67
    */
68
    private CacheMutator $mutator;
69
70
/**
71
    * Cacheer constructor.
72
    *
73
    * @param array $options
74
    * @param bool  $formatted
75
    * @throws RuntimeException|Exceptions\CacheFileException
76
 */
77
    public function __construct(array $options = [], bool $formatted = false)
78
    {
79
        $this->formatted = $formatted;
80
        $this->validateOptions($options);
81
        $this->retriever = new CacheRetriever($this);
82
        $this->mutator = new CacheMutator($this);
83
        $this->setDriver()->useDefaultDriver();
84
    }
85
86
    /**
87
    * Adds data to the cache if it does not already exist.
88
    *
89
    * @param string $cacheKey
90
    * @param mixed  $cacheData
91
    * @param string $namespace
92
    * @param int|string $ttl
93
    * @return bool
94
    */
95
    public function add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600): bool
96
    {
97
        return $this->mutator->add($cacheKey, $cacheData, $namespace, $ttl);
98
    }
99
100
    /**
101
    * Appends data to an existing cache item.
102
    * 
103
    * @param string $cacheKey
104
    * @param mixed  $cacheData
105
    * @param string $namespace
106
    * @return void
107
    */
108
    public function appendCache(string $cacheKey, mixed $cacheData, string $namespace = ''): void
109
    {
110
        $this->mutator->appendCache($cacheKey, $cacheData, $namespace);
111
    }
112
113
    /**
114
    * Clears a specific cache item.
115
    * 
116
    * @param string $cacheKey
117
    * @param string $namespace
118
    * @return void
119
    */
120
    public function clearCache(string $cacheKey, string $namespace = ''): void
121
    {
122
        $this->mutator->clearCache($cacheKey, $namespace);
123
    }
124
125
    /**
126
    * Decrements a cache item by a specified amount.
127
    *  
128
    * @param string $cacheKey
129
    * @param int $amount
130
    * @param string $namespace
131
    * @return bool
132
    */
133
    public function decrement(string $cacheKey, int $amount = 1, string $namespace = ''): bool
134
    {
135
        return $this->mutator->decrement($cacheKey, $amount, $namespace);
136
    }
137
138
    /**
139
    * Store data in the cache permanently.
140
    *
141
    * @param string $cacheKey
142
    * @param mixed $cacheData
143
    * @return void
144
    */
145
    public function forever(string $cacheKey, mixed $cacheData): void
146
    {
147
        $this->mutator->forever($cacheKey, $cacheData);
148
    }
149
150
    /**
151
    * Flushes all cache items.
152
    * 
153
    * @return void
154
    */
155
    public function flushCache(): void
156
    {
157
        $this->mutator->flushCache();
158
    }
159
160
    /**
161
    * Retrieves a cache item and deletes it from the cache.
162
    * 
163
    * @param string $cacheKey
164
    * @param string $namespace
165
    * @return mixed
166
    */
167
    public function getAndForget(string $cacheKey, string $namespace = ''): mixed
168
    {
169
        return $this->retriever->getAndForget($cacheKey, $namespace);
170
    }
171
172
    /**
173
    * Gets all items in a specific namespace.
174
    * 
175
    * @param string $namespace
176
    * @return CacheDataFormatter|mixed
177
    */
178
    public function getAll(string $namespace = ''): mixed
179
    {
180
        return $this->retriever->getAll($namespace);
181
    }
182
183
    /**
184
    * Retrieves a single cache item.
185
    * 
186
    * @param string $cacheKey
187
    * @param string $namespace
188
    * @param string|int $ttl
189
    * @return CacheDataFormatter|mixed
190
    */
191
    public function getCache(string $cacheKey, string $namespace = '', string|int $ttl = 3600): mixed
192
    {
193
        return $this->retriever->getCache($cacheKey, $namespace, $ttl);
194
    }
195
196
    /**
197
    * Retrieves multiple cache items by their keys.
198
    * 
199
    * @param array $cacheKeys
200
    * @param string $namespace
201
    * @param string|int $ttl
202
    * @return CacheDataFormatter|array
203
     */
204
    public function getMany(array $cacheKeys, string $namespace = '', string|int $ttl = 3600): CacheDataFormatter|array
205
    {
206
        return $this->retriever->getMany($cacheKeys, $namespace, $ttl);
207
    }
208
209
    /**
210
    * Checks if a cache item exists.
211
    * 
212
    * @param string $cacheKey
213
    * @param string $namespace
214
    * @return void
215
    */
216
    public function has(string $cacheKey, string $namespace = ''): void
217
    {
218
        $this->retriever->has($cacheKey, $namespace);
219
    }
220
221
    /**
222
    * Increments a cache item by a specified amount.
223
    * 
224
    * @param string $cacheKey
225
    * @param int $amount
226
    * @param string $namespace
227
    * @return bool
228
    */
229
    public function increment(string $cacheKey, int $amount = 1, string $namespace = ''): bool
230
    {
231
        return $this->mutator->increment($cacheKey, $amount, $namespace);
232
    }
233
234
    /**
235
    * Checks if the last operation was successful.
236
    * 
237
    * @return bool
238
    */
239
    public function isSuccess(): bool
240
    {
241
        return $this->success;
242
    }
243
244
    /**
245
    * Stores an item in the cache with a specific TTL.
246
    * 
247
    * @param string $cacheKey
248
    * @param mixed  $cacheData
249
    * @param string $namespace
250
    * @param string|int $ttl
251
    * @return void
252
    */
253
    public function putCache(string $cacheKey, mixed $cacheData, string $namespace = '', string|int $ttl = 3600): void
254
    {
255
        $this->mutator->putCache($cacheKey, $cacheData, $namespace, $ttl);
256
    }
257
258
    /**
259
    * Stores multiple items in the cache.
260
    *  
261
    * @param array   $items
262
    * @param string  $namespace
263
    * @param integer $batchSize
264
    * @return void
265
    */
266
    public function putMany(array $items, string $namespace = '', int $batchSize = 100): void
267
    {
268
        $this->mutator->putMany($items, $namespace, $batchSize);
269
    }
270
271
    /**
272
    * Renews the cache for a specific key with a new TTL.
273
    * 
274
    * @param string $cacheKey
275
    * @param string|int $ttl
276
    * @param string $namespace
277
    * @return void
278
    */
279
    public function renewCache(string $cacheKey, string|int $ttl = 3600, string $namespace = ''): void
280
    {
281
        $this->mutator->renewCache($cacheKey, $ttl, $namespace);
282
    }
283
284
    /**
285
    * Retrieves a cache item or executes a callback to store it if not found.
286
    * 
287
    * @param string $cacheKey
288
    * @param int|string $ttl
289
    * @param Closure $callback
290
    * @return mixed
291
    */
292
    public function remember(string $cacheKey, int|string $ttl, Closure $callback): mixed
293
    {
294
        return $this->retriever->remember($cacheKey, $ttl, $callback);
295
    }
296
297
    /**
298
    * Retrieves a cache item or executes a callback to store it permanently if not found.
299
    * 
300
    * @param string $cacheKey
301
    * @param Closure $callback
302
    * @return mixed
303
    */
304
    public function rememberForever(string $cacheKey, Closure $callback): mixed
305
    {
306
        return $this->retriever->rememberForever($cacheKey, $callback);
307
    }
308
309
    /**
310
    * Returns a CacheConfig instance for configuration management.
311
    * 
312
    * @return CacheConfig
313
    */
314
    public function setConfig(): CacheConfig
315
    {
316
        return new CacheConfig($this);
317
    }
318
319
    /**
320
    * Sets the cache driver based on the configuration.
321
    * 
322
    * @return CacheDriver
323
    */
324
    public function setDriver(): CacheDriver
325
    {
326
        return new CacheDriver($this);
327
    }
328
329
    /**
330
    * Sets a message for the cache operation.
331
    *
332
    * @param string  $message
333
    * @param boolean $success
334
    * @return void
335
    */
336
    private function setMessage(string $message, bool $success): void
337
    {
338
        $this->message = $message;
339
        $this->success = $success;
340
    }
341
342
    /**
343
    * Retrieves the message from the last operation.
344
    * 
345
    * @return string
346
    */
347
    public function getMessage(): string
348
    {
349
        return $this->message;
350
    }
351
352
    /**
353
     * @return void
354
     */
355
    public function syncState(): void
356
    {
357
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
358
    }
359
360
    /**
361
     * @param string $message
362
     * @param bool $success
363
     * @return void
364
     */
365
    public function setInternalState(string $message, bool $success): void
366
    {
367
        $this->setMessage($message, $success);
368
    }
369
370
    /**
371
     * @return bool
372
     */
373
    public function isFormatted(): bool
374
    {
375
        return $this->formatted;
376
    }
377
378
    /**
379
     * @return bool
380
     */
381
    public function isCompressionEnabled(): bool
382
    {
383
        return $this->compression;
384
    }
385
386
    /**
387
     * @return string|null
388
     */
389
    public function getEncryptionKey(): ?string
390
    {
391
        return $this->encryptionKey;
392
    }
393
394
    /**
395
    * Enables or disables the formatter for cache data.
396
    * 
397
    * @return void
398
    */
399
    public function useFormatter(): void
400
    {
401
        $this->formatted = !$this->formatted;
402
    }
403
404
    /**
405
    * Validates the options provided for the Cacheer instance.
406
    * 
407
    * @param array $options
408
    * @return void
409
    */
410
    private function validateOptions(array $options): void
411
    {
412
        $this->options = $options;
413
    }
414
415
    /**
416
    * Enable or disable data compression
417
    *
418
    * @param bool $status
419
    * @return $this
420
    */
421
    public function useCompression(bool $status = true): Cacheer
422
    {
423
        $this->compression = $status;
424
        return $this;
425
    }
426
427
    /**
428
    * Enable encryption for cached data
429
    *
430
    * @param string $key
431
    * @return $this
432
    */
433
    public function useEncryption(string $key): Cacheer
434
    {
435
        $this->encryptionKey = $key;
436
        return $this;
437
    }
438
}
439