Passed
Push — main ( 994ffc...08f4bd )
by Sílvio
01:02 queued 14s
created

Cacheer   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 397
Duplicated Lines 0 %

Importance

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

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