Test Setup Failed
Pull Request — main (#58)
by Sílvio
09:03
created

Cacheer::setConfig()   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 0
dl 0
loc 3
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_exists($this, $method)) {
176
            return $this->{$method}(...$parameters);
177
        }
178
179
        $delegates = [$this->mutator, $this->retriever, $this->config];
180
181
        foreach ($delegates as $delegate) {
182
            if (method_exists($delegate, $method)) {
183
                return $delegate->{$method}(...$parameters);
184
            }
185
        }
186
187
        throw new BadMethodCallException("Method {$method} does not exist");
188
    }
189
190
    /**
191
     * Retrieve the configuration helper for the shared instance.
192
     *
193
     * @return CacheConfig
194
     */
195
    public static function setConfig(): CacheConfig
196
    {
197
        return new CacheConfig(self::instance());
198
    }
199
200
    /**
201
     * Retrieve the driver helper for the shared instance.
202
     *
203
     * @return CacheDriver
204
     */
205
    public static function setDriver(): CacheDriver
206
    {
207
        return new CacheDriver(self::instance());
208
    }
209
210
    /**
211
     * Handle dynamic static calls by routing them through an instance.
212
     *
213
     * @param string $method
214
     * @param array $parameters
215
     * @return mixed
216
     */
217
    public static function __callStatic(string $method, array $parameters): mixed
218
    {
219
        $instance = self::instance();
220
221
        if ($instance === null) {
222
            throw new \RuntimeException("Cacheer static instance is not initialized.");
223
        }
224
225
        return $instance->__call($method, $parameters);
226
    }
227
228
    /**
229
    * Enable encryption for cached data
230
    *
231
    * @param string $key
232
    * @return $this
233
    */
234
    protected function useEncryption(string $key): Cacheer
235
    {
236
        $this->encryptionKey = $key;
237
        return $this;
238
    }
239
240
    /**
241
    * Enable or disable data compression
242
    *
243
    * @param bool $status
244
    * @return $this
245
    */
246
    protected function useCompression(bool $status = true): Cacheer
247
    {
248
        $this->compression = $status;
249
        return $this;
250
    }
251
252
    /**
253
    * Enables or disables the formatter for cache data.
254
    * 
255
    * @return void
256
    */
257
    protected function useFormatter(): void
258
    {
259
        $this->formatted = !$this->formatted;
260
    }
261
262
    /**
263
    * Validates the options provided for the Cacheer instance.
264
    * 
265
    * @param array $options
266
    * @return void
267
    */
268
    private function validateOptions(array $options): void
269
    {
270
        $this->options = $options;
271
    }
272
273
    /**
274
    * Checks if the last operation was successful.
275
    * 
276
    * @return bool
277
    */
278
    protected function isSuccess(): bool
279
    {
280
        return $this->success;
281
    }
282
283
    /**
284
    * Sets a message for the cache operation.
285
    *
286
    * @param string  $message
287
    * @param boolean $success
288
    * @return void
289
    */
290
    private function setMessage(string $message, bool $success): void
291
    {
292
        $this->message = $message;
293
        $this->success = $success;
294
    }
295
296
    /**
297
    * Retrieves the message from the last operation.
298
    * 
299
    * @return string
300
    */
301
    protected function getMessage(): string
302
    {
303
        return $this->message;
304
    }
305
306
    /**
307
     * @return void
308
     */
309
    protected function syncState(): void
310
    {
311
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
312
    }
313
314
    /**
315
     * @param string $message
316
     * @param bool $success
317
     * @return void
318
     */
319
    protected function setInternalState(string $message, bool $success): void
320
    {
321
        $this->setMessage($message, $success);
322
    }
323
324
    /**
325
     * @return bool
326
     */
327
    protected function isFormatted(): bool
328
    {
329
        return $this->formatted;
330
    }
331
332
    /**
333
     * @return bool
334
     */
335
    protected function isCompressionEnabled(): bool
336
    {
337
        return $this->compression;
338
    }
339
340
    /**
341
     * @return string|null
342
     */
343
    protected function getEncryptionKey(): ?string
344
    {
345
        return $this->encryptionKey;
346
    }
347
348
    /**
349
     * Get or create the shared Cacheer instance for static calls.
350
     *
351
     * @return Cacheer
352
     */
353
    private static function instance(): Cacheer
354
    {
355
        if (self::$staticInstance === null) {
356
            self::$staticInstance = new self();
357
        }
358
        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...
359
    }
360
}
361