Passed
Pull Request — main (#59)
by Sílvio
03:16
created

Cacheer::useCompression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 2
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp;
4
5
use Closure;
6
use Silviooosilva\CacheerPhp\CacheStore\DatabaseCacheStore;
7
use Silviooosilva\CacheerPhp\CacheStore\FileCacheStore;
8
use Silviooosilva\CacheerPhp\CacheStore\RedisCacheStore;
9
use Silviooosilva\CacheerPhp\CacheStore\ArrayCacheStore;
10
use Silviooosilva\CacheerPhp\Helpers\CacheConfig;
11
use Silviooosilva\CacheerPhp\Utils\CacheDataFormatter;
12
use Silviooosilva\CacheerPhp\Utils\CacheDriver;
13
use RuntimeException;
14
use Silviooosilva\CacheerPhp\Service\CacheRetriever;
15
use Silviooosilva\CacheerPhp\Service\CacheMutator;
16
use BadMethodCallException;
17
18
/**
19
 * Class CacheerPHP
20
 *
21
 * @author Sílvio Silva <https://github.com/silviooosilva>
22
 * @package Silviooosilva\CacheerPhp
23
 *
24
 * @method static bool add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
25
 * @method bool add(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
26
 * @method static bool appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
27
 * @method bool appendCache(string $cacheKey, mixed $cacheData, string $namespace = '')
28
 * @method static bool clearCache(string $cacheKey, string $namespace = '')
29
 * @method bool clearCache(string $cacheKey, string $namespace = '')
30
 * @method static bool decrement(string $cacheKey, int $amount = 1, string $namespace = '')
31
 * @method bool decrement(string $cacheKey, int $amount = 1, string $namespace = '')
32
 * @method static bool flushCache()
33
 * @method bool flushCache()
34
 * @method static bool forever(string $cacheKey, mixed $cacheData)
35
 * @method bool forever(string $cacheKey, mixed $cacheData)
36
 * @method static mixed getAndForget(string $cacheKey, string $namespace = '')
37
 * @method mixed getAndForget(string $cacheKey, string $namespace = '')
38
 * @method static CacheDataFormatter|mixed getAll(string $namespace = '')
39
 * @method CacheDataFormatter|mixed getAll(string $namespace = '')
40
 * @method static mixed getCache(string $cacheKey, string $namespace = '', int|string $ttl = 3600)
41
 * @method mixed getCache(string $cacheKey, string $namespace = '', int|string $ttl = 3600)
42
 * @method static array|CacheDataFormatter getMany(array $cacheKeys, string $namespace = '', int|string $ttl = 3600)
43
 * @method array|CacheDataFormatter getMany(array $cacheKeys, string $namespace = '', int|string $ttl = 3600)
44
 * @method static getOptions(): array
45
 * @method getOptions(): array
46
 * @method static bool has(string $cacheKey, string $namespace = '')
47
 * @method bool has(string $cacheKey, string $namespace = '')
48
 * @method static bool increment(string $cacheKey, int $amount = 1, string $namespace = '')
49
 * @method bool increment(string $cacheKey, int $amount = 1, string $namespace = '')
50
 * @method static bool putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
51
 * @method bool putCache(string $cacheKey, mixed $cacheData, string $namespace = '', int|string $ttl = 3600)
52
 * @method static bool putMany(array $items, string $namespace = '', int $batchSize = 100)
53
 * @method bool putMany(array $items, string $namespace = '', int $batchSize = 100)
54
 * @method static mixed remember(string $cacheKey, int|string $ttl, Closure $callback)
55
 * @method mixed remember(string $cacheKey, int|string $ttl, Closure $callback)
56
 * @method static mixed rememberForever(string $cacheKey, Closure $callback)
57
 * @method mixed rememberForever(string $cacheKey, Closure $callback)
58
 * @method static bool renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '')
59
 * @method bool renewCache(string $cacheKey, int|string $ttl = 3600, string $namespace = '')
60
 * @method static setConfig(): CacheConfig
61
 * @method setConfig(): CacheConfig
62
 * @method static setDriver(): CacheDriver
63
 * @method setDriver(): CacheDriver
64
 * @method static setUp(array $options): void
65
 * @method setUp(array $options): void
66
 * @method static CacheConfig setConfig()
67
 * @method CacheConfig setConfig()
68
 * @method static CacheDriver setDriver()
69
 * @method CacheDriver setDriver()
70
 * @method static Cacheer useEncryption(string $key)
71
 * @method Cacheer useEncryption(string $key)
72
 * @method static Cacheer useCompression(bool $status = true)
73
 * @method Cacheer useCompression(bool $status = true)
74
 * @method static void useFormatter()
75
 * @method void useFormatter()
76
 * @method static bool isSuccess()
77
 * @method bool isSuccess()
78
 * @method static string getMessage()
79
 * @method string getMessage()
80
 * @method static void syncState()
81
 * @method void syncState()
82
 * @method static void setInternalState(string $message, bool $success)
83
 * @method void setInternalState(string $message, bool $success)
84
 * @method static bool isFormatted()
85
 * @method bool isFormatted()
86
 * @method static bool isCompressionEnabled()
87
 * @method bool isCompressionEnabled()
88
 * @method static string|null getEncryptionKey()
89
 * @method string|null getEncryptionKey()
90
 */
91
final class Cacheer
92
{
93
    /**
94
    * @var string
95
    */
96
    private string $message;
97
98
    /**
99
    * @var boolean
100
    */
101
    private bool $success;
102
103
    /**
104
    * @var boolean
105
    */
106
    private bool $formatted = false;
107
108
    /**
109
    * @var bool
110
    */
111
    private bool $compression = false;
112
113
    /**
114
    * @var string|null
115
    */
116
    private ?string $encryptionKey = null;
117
118
    /**
119
    * @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore
120
    */
121
    public RedisCacheStore|DatabaseCacheStore|ArrayCacheStore|FileCacheStore $cacheStore;
122
123
    /**
124
    * @var array
125
    */
126
    public array $options = [];
127
128
    /**
129
    * @var CacheRetriever
130
    */
131
    private CacheRetriever $retriever;
132
133
    /**
134
    * @var CacheMutator
135
    */
136
    private CacheMutator $mutator;
137
138
    /**
139
    * @var CacheConfig
140
    */
141
    private CacheConfig $config;
142
143
    /**
144
    * @var Cacheer|null
145
    */
146
    private static ?Cacheer $staticInstance = null;
147
148
/**
149
    * Cacheer constructor.
150
    *
151
    * @param array $options
152
    * @param bool  $formatted
153
    * @throws RuntimeException|Exceptions\CacheFileException
154
 */
155
    public function __construct(array $options = [], bool $formatted = false)
156
    {
157
        $this->formatted = $formatted;
158
        $this->validateOptions($options);
159
        $this->retriever = new CacheRetriever($this);
160
        $this->mutator = new CacheMutator($this);
161
        $this->config = new CacheConfig($this);
162
        (new CacheDriver($this))->useDefaultDriver();
163
    }
164
165
    /**
166
     * Dynamically handle calls to missing instance methods.
167
     *
168
     * @param string $method
169
     * @param array $parameters
170
     * @return mixed
171
     * @throws BadMethodCallException
172
     */
173
    public function __call(string $method, array $parameters): mixed
174
    {
175
        if ($method === 'setDriver') {
176
            return new CacheDriver($this);
177
        }
178
179
        if ($method === 'setConfig') {
180
            return $this->config;
181
        }
182
183
        if (method_exists($this, $method)) {
184
            return $this->{$method}(...$parameters);
185
        }
186
187
        $delegates = [$this->mutator, $this->retriever, $this->config];
188
189
        foreach ($delegates as $delegate) {
190
            if (method_exists($delegate, $method)) {
191
                return $delegate->{$method}(...$parameters);
192
            }
193
        }
194
195
        throw new BadMethodCallException("Method {$method} does not exist");
196
    }
197
198
    /**
199
     * Retrieve the configuration helper for the shared instance.
200
     *
201
     * @return CacheConfig
202
     */
203
    public static function setConfig(): CacheConfig
204
    {
205
        return new CacheConfig(self::instance());
206
    }
207
208
    /**
209
     * Retrieve the driver helper for the shared instance.
210
     *
211
     * @return CacheDriver
212
     */
213
    public static function setDriver(): CacheDriver
214
    {
215
        return new CacheDriver(self::instance());
216
    }
217
218
    /**
219
     * Handle dynamic static calls by routing them through an instance.
220
     *
221
     * @param string $method
222
     * @param array $parameters
223
     * @return mixed
224
     */
225
    public static function __callStatic(string $method, array $parameters): mixed
226
    {
227
        $instance = self::instance();
228
229
        if ($instance === null) {
230
            throw new \RuntimeException("Cacheer static instance is not initialized.");
231
        }
232
233
        return $instance->__call($method, $parameters);
234
    }
235
236
    /**
237
    * Enable encryption for cached data
238
    *
239
    * @param string $key
240
    * @return $this
241
    */
242
    protected function useEncryption(string $key): Cacheer
243
    {
244
        $this->encryptionKey = $key;
245
        return $this;
246
    }
247
248
    /**
249
    * Enable or disable data compression
250
    *
251
    * @param bool $status
252
    * @return $this
253
    */
254
    protected function useCompression(bool $status = true): Cacheer
255
    {
256
        $this->compression = $status;
257
        return $this;
258
    }
259
260
    /**
261
    * Enables or disables the formatter for cache data.
262
    * 
263
    * @return void
264
    */
265
    protected function useFormatter(): void
266
    {
267
        $this->formatted = !$this->formatted;
268
    }
269
270
    /**
271
    * Validates the options provided for the Cacheer instance.
272
    * 
273
    * @param array $options
274
    * @return void
275
    */
276
    private function validateOptions(array $options): void
277
    {
278
        $this->options = $options;
279
    }
280
281
    /**
282
    * Checks if the last operation was successful.
283
    * 
284
    * @return bool
285
    */
286
    protected function isSuccess(): bool
287
    {
288
        return $this->success;
289
    }
290
291
    /**
292
    * Sets a message for the cache operation.
293
    *
294
    * @param string  $message
295
    * @param boolean $success
296
    * @return void
297
    */
298
    private function setMessage(string $message, bool $success): void
299
    {
300
        $this->message = $message;
301
        $this->success = $success;
302
    }
303
304
    /**
305
    * Retrieves the message from the last operation.
306
    * 
307
    * @return string
308
    */
309
    protected function getMessage(): string
310
    {
311
        return $this->message;
312
    }
313
314
    /**
315
     * @return void
316
     */
317
    protected function syncState(): void
318
    {
319
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
320
    }
321
322
    /**
323
     * @param string $message
324
     * @param bool $success
325
     * @return void
326
     */
327
    protected function setInternalState(string $message, bool $success): void
328
    {
329
        $this->setMessage($message, $success);
330
    }
331
332
    /**
333
     * @return bool
334
     */
335
    protected function isFormatted(): bool
336
    {
337
        return $this->formatted;
338
    }
339
340
    /**
341
     * @return bool
342
     */
343
    protected function isCompressionEnabled(): bool
344
    {
345
        return $this->compression;
346
    }
347
348
    /**
349
     * @return string|null
350
     */
351
    protected function getEncryptionKey(): ?string
352
    {
353
        return $this->encryptionKey;
354
    }
355
356
    /**
357
     * Get or create the shared Cacheer instance for static calls.
358
     *
359
     * @return Cacheer
360
     * Get or create the shared Cacheer instance for static calls.
361
     *
362
     * @return Cacheer
363
     */
364
    private static function instance(): Cacheer
365
    {
366
        if (self::$staticInstance === null) {
367
            self::$staticInstance = new self();
368
        }
369
        return self::$staticInstance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::staticInstance could return the type null which is incompatible with the type-hinted return Silviooosilva\CacheerPhp\Cacheer. Consider adding an additional type-check to rule them out.
Loading history...
370
    }
371
}
372