Issues (21)

src/Cacheer.php (1 issue)

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