Passed
Pull Request — main (#57)
by Sílvio
03:20
created

Cacheer::__callStatic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 9
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 setUp(array $options): void
61
 * @method setUp(array $options): void
62
 */
63
final class Cacheer
64
{
65
    /**
66
    * @var string
67
    */
68
    private string $message;
69
70
    /**
71
    * @var boolean
72
    */
73
    private bool $success;
74
75
    /**
76
    * @var boolean
77
    */
78
    private bool $formatted = false;
79
80
    /**
81
    * @var bool
82
    */
83
    private bool $compression = false;
84
85
    /**
86
    * @var string|null
87
    */
88
    private ?string $encryptionKey = null;
89
90
    /**
91
    * @var FileCacheStore|DatabaseCacheStore|RedisCacheStore|ArrayCacheStore
92
    */
93
    public RedisCacheStore|DatabaseCacheStore|ArrayCacheStore|FileCacheStore $cacheStore;
94
95
    /**
96
    * @var array
97
    */
98
    public array $options = [];
99
100
    /**
101
    * @var CacheRetriever
102
    */
103
    private CacheRetriever $retriever;
104
105
    /**
106
    * @var CacheMutator
107
    */
108
    private CacheMutator $mutator;
109
110
    /**
111
    * @var CacheConfig
112
    */
113
    private CacheConfig $config;
114
115
    /**
116
    * @var Cacheer|null
117
    */
118
    private static ?Cacheer $staticInstance = null;
119
120
/**
121
    * Cacheer constructor.
122
    *
123
    * @param array $options
124
    * @param bool  $formatted
125
    * @throws RuntimeException|Exceptions\CacheFileException
126
 */
127
    public function __construct(array $options = [], bool $formatted = false)
128
    {
129
        $this->formatted = $formatted;
130
        $this->validateOptions($options);
131
        $this->retriever = new CacheRetriever($this);
132
        $this->mutator = new CacheMutator($this);
133
        $this->config = new CacheConfig($this);
134
        $this->setDriver()->useDefaultDriver();
0 ignored issues
show
Bug introduced by
The method setDriver() does not exist on Silviooosilva\CacheerPhp\Cacheer. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

134
        $this->/** @scrutinizer ignore-call */ 
135
               setDriver()->useDefaultDriver();
Loading history...
135
    }
136
137
    /**
138
     * Dynamically handle calls to missing instance methods.
139
     *
140
     * @param string $method
141
     * @param array $parameters
142
     * @return mixed
143
     * @throws BadMethodCallException
144
     */
145
    public function __call(string $method, array $parameters): mixed
146
    {
147
        if ($method === 'setConfig') {
148
            return new CacheConfig($this);
149
        }
150
151
        if ($method === 'setDriver') {
152
            return new CacheDriver($this);
153
        }
154
155
        $delegates = [$this->mutator, $this->retriever, $this->config];
156
157
        foreach ($delegates as $delegate) {
158
            if (method_exists($delegate, $method)) {
159
                return $delegate->{$method}(...$parameters);
160
            }
161
        }
162
163
        throw new BadMethodCallException("Method {$method} does not exist");
164
    }
165
166
    /**
167
     * Handle dynamic static calls by routing them through an instance.
168
     *
169
     * @param string $method
170
     * @param array $parameters
171
     * @return mixed
172
     */
173
    public static function __callStatic(string $method, array $parameters): mixed
174
    {
175
        $instance = self::instance();
176
177
        if ($instance === null) {
178
            throw new \RuntimeException("Cacheer static instance is not initialized.");
179
        }
180
181
        return $instance->__call($method, $parameters);
182
    }
183
184
    /**
185
    * Enable encryption for cached data
186
    *
187
    * @param string $key
188
    * @return $this
189
    */
190
    public function useEncryption(string $key): Cacheer
191
    {
192
        $this->encryptionKey = $key;
193
        return $this;
194
    }
195
196
    /**
197
    * Enable or disable data compression
198
    *
199
    * @param bool $status
200
    * @return $this
201
    */
202
    public function useCompression(bool $status = true): Cacheer
203
    {
204
        $this->compression = $status;
205
        return $this;
206
    }
207
208
    /**
209
    * Enables or disables the formatter for cache data.
210
    * 
211
    * @return void
212
    */
213
    public function useFormatter(): void
214
    {
215
        $this->formatted = !$this->formatted;
216
    }
217
218
    /**
219
    * Validates the options provided for the Cacheer instance.
220
    * 
221
    * @param array $options
222
    * @return void
223
    */
224
    private function validateOptions(array $options): void
225
    {
226
        $this->options = $options;
227
    }
228
229
    /**
230
    * Checks if the last operation was successful.
231
    * 
232
    * @return bool
233
    */
234
    public function isSuccess(): bool
235
    {
236
        return $this->success;
237
    }
238
239
    /**
240
    * Sets a message for the cache operation.
241
    *
242
    * @param string  $message
243
    * @param boolean $success
244
    * @return void
245
    */
246
    private function setMessage(string $message, bool $success): void
247
    {
248
        $this->message = $message;
249
        $this->success = $success;
250
    }
251
252
    /**
253
    * Retrieves the message from the last operation.
254
    * 
255
    * @return string
256
    */
257
    public function getMessage(): string
258
    {
259
        return $this->message;
260
    }
261
262
    /**
263
     * @return void
264
     */
265
    public function syncState(): void
266
    {
267
        $this->setMessage($this->cacheStore->getMessage(), $this->cacheStore->isSuccess());
268
    }
269
270
    /**
271
     * @param string $message
272
     * @param bool $success
273
     * @return void
274
     */
275
    public function setInternalState(string $message, bool $success): void
276
    {
277
        $this->setMessage($message, $success);
278
    }
279
280
    /**
281
     * @return bool
282
     */
283
    public function isFormatted(): bool
284
    {
285
        return $this->formatted;
286
    }
287
288
    /**
289
     * @return bool
290
     */
291
    public function isCompressionEnabled(): bool
292
    {
293
        return $this->compression;
294
    }
295
296
    /**
297
     * @return string|null
298
     */
299
    public function getEncryptionKey(): ?string
300
    {
301
        return $this->encryptionKey;
302
    }
303
304
    /**
305
     * Get or create the shared Cacheer instance for static calls.
306
     *
307
     * @return Cacheer
308
     */
309
    private static function instance(): Cacheer
310
    {
311
        if (self::$staticInstance === null) {
312
            self::$staticInstance = new self();
313
        }
314
        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...
315
    }
316
}
317