Passed
Pull Request — main (#48)
by Sílvio
03:09
created

Cacheer::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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