CacheEntity::setDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Mostafaznv\LaraCache;
4
5
use Closure;
6
7
class CacheEntity
8
{
9
    /**
10
     * Cache name
11
     *
12
     * @var string
13
     */
14
    public string $name;
15
16
    /**
17
     * Cache Driver (store)
18
     *
19
     * @var string
20
     */
21
    public string $driver;
22
23
    /**
24
     * Indicate if cache should exist forever
25
     *
26
     * @var bool
27
     */
28
    public bool $forever = true;
29
30
    /**
31
     * Indicates if caching operation should perform in the background or not
32
     *
33
     * @var bool
34
     */
35
    public bool $isQueueable;
36
37
    /**
38
     * Queue name
39
     *
40
     * @var string
41
     */
42
    public string $queueName;
43
44
    /**
45
     * Queue connection
46
     *
47
     * @var string
48
     */
49
    public string $queueConnection;
50
51
    /**
52
     * Indicate if cache should exist till end of day
53
     *
54
     * @var bool
55
     */
56
    public bool $validForRestOfDay = false;
57
58
    /**
59
     * Indicate if cache should exist till end of week
60
     *
61
     * @var bool
62
     */
63
    public bool $validForRestOfWeek = false;
64
65
    /**
66
     * Cache TTL in seconds
67
     * ttl = 0 means we want cache queries forever
68
     *
69
     * @var int
70
     */
71
    public int $ttl = 0;
72
73
    /**
74
     * Indicate if queries should refresh after create
75
     *
76
     * @var bool
77
     */
78
    public bool $refreshAfterCreate = true;
79
80
    /**
81
     * Indicate if queries should refresh after update
82
     *
83
     * @var bool
84
     */
85
    public bool $refreshAfterUpdate = true;
86
87
    /**
88
     * Indicate if queries should refresh after delete
89
     *
90
     * @var bool
91
     */
92
    public bool $refreshAfterDelete = true;
93
94
    /**
95
     * Indicate if queries should refresh after restore
96
     *
97
     * @var bool
98
     */
99
    public bool $refreshAfterRestore = true;
100
101
    /**
102
     * Specify default value of cache entity
103
     *
104
     * @var mixed
105
     */
106
    public mixed $default = null;
107
108
    /**
109
     * The anonymous function that should be executed to store cache values to cache store.
110
     *
111
     * @var Closure|null
112
     */
113
    public ?Closure $cacheClosure = null;
114
115
116
    public function __construct(string $name)
117
    {
118
        $this->name = $name;
119
        $this->driver = config('laracache.driver') ?? config('cache.default');
120
121
        $queue = config('laracache.queue');
122
123
        if (is_array($queue)) {
124
            $this->isQueueable = $queue['status'] ?? false;
125
            $this->queueName = $queue['name'] ?? 'default';
126
            $this->queueConnection = $queue['connection'] ?? config('queue.default');
127
        }
128
        else {
129
            $this->isQueueable = (bool)$queue;
130
            $this->queueName = 'default';
131
            $this->queueConnection = config('queue.default');
132
        }
133
    }
134
135
    /**
136
     * Create a new cache entity.
137
     *
138
     * @param string $name
139
     * @return CacheEntity
140
     */
141
    public static function make(string $name): CacheEntity
142
    {
143
        return new static($name);
144
    }
145
146
    /**
147
     * Specify custom driver for cache entity
148
     *
149
     * @param string $driver
150
     * @return $this
151
     */
152
    public function setDriver(string $driver): CacheEntity
153
    {
154
        $this->driver = $driver;
155
156
        return $this;
157
    }
158
159
    /**
160
     * Specify if cache operation should perform in background or not
161
     *
162
     * @param bool $status
163
     * @param string $onConnection
164
     * @param string $onQueue
165
     * @return $this
166
     */
167
    public function isQueueable(bool $status = true, string $onConnection = '', string $onQueue = ''): CacheEntity
168
    {
169
        $this->isQueueable = $status;
170
171
        if ($onConnection) {
172
            $this->queueConnection = $onConnection;
173
        }
174
175
        if ($onQueue) {
176
            $this->queueName = $onQueue;
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * Specify that the cache should refresh after create a model instance
184
     *
185
     * @param bool $status
186
     * @return $this
187
     */
188
    public function refreshAfterCreate(bool $status = true): CacheEntity
189
    {
190
        $this->refreshAfterCreate = $status;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Specify that the cache should refresh after update a model instance
197
     *
198
     * @param bool $status
199
     * @return $this
200
     */
201
    public function refreshAfterUpdate(bool $status = true): CacheEntity
202
    {
203
        $this->refreshAfterUpdate = $status;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Specify that the cache should refresh after delete a model instance
210
     *
211
     * @param bool $status
212
     * @return $this
213
     */
214
    public function refreshAfterDelete(bool $status = true): CacheEntity
215
    {
216
        $this->refreshAfterDelete = $status;
217
218
        return $this;
219
    }
220
221
    /**
222
     * Specify that the cache should refresh after restore a model instance
223
     *
224
     * @param bool $status
225
     * @return $this
226
     */
227
    public function refreshAfterRestore(bool $status = true): CacheEntity
228
    {
229
        $this->refreshAfterRestore = $status;
230
231
        return $this;
232
    }
233
234
    /**
235
     * Specify that cache entity should exist there forever
236
     *
237
     * @return $this
238
     */
239
    public function forever(): CacheEntity
240
    {
241
        $this->forever = true;
242
        $this->validForRestOfDay = false;
243
        $this->validForRestOfWeek = false;
244
        $this->ttl = 0;
245
246
        return $this;
247
    }
248
249
    /**
250
     * Specify that cache entity should exist there till end of day
251
     *
252
     * @return $this
253
     */
254
    public function validForRestOfDay(): CacheEntity
255
    {
256
        $this->validForRestOfDay = true;
257
        $this->validForRestOfWeek = false;
258
        $this->forever = false;
259
        $this->ttl = 0;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Specify that cache entity should exist there till end of week
266
     *
267
     * @return $this
268
     */
269
    public function validForRestOfWeek(): CacheEntity
270
    {
271
        $this->validForRestOfDay = false;
272
        $this->validForRestOfWeek = true;
273
        $this->forever = false;
274
        $this->ttl = 0;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Specify cache time to live in second
281
     *
282
     * @param int $seconds
283
     * @return $this
284
     */
285
    public function ttl(int $seconds): CacheEntity
286
    {
287
        $this->ttl = max($seconds, 0);
288
        $this->forever = $this->ttl === 0;
289
        $this->validForRestOfDay = false;
290
        $this->validForRestOfWeek = false;
291
292
        return $this;
293
    }
294
295
    /**
296
     * Set default value of cache entity
297
     *
298
     * @param mixed $defaultValue
299
     * @return $this
300
     */
301
    public function setDefault(mixed $defaultValue): CacheEntity
302
    {
303
        $this->default = $defaultValue;
304
305
        return $this;
306
    }
307
308
    /**
309
     * Get TTL
310
     *
311
     * @return int
312
     * @internal
313
     */
314
    public function getTtl(): int
315
    {
316
        if ($this->forever) {
317
            return 0;
318
        }
319
320
        if ($this->validForRestOfDay) {
321
            return day_ending_seconds();
322
        }
323
324
        if ($this->validForRestOfWeek) {
325
            return week_ending_seconds();
326
        }
327
328
        return $this->ttl;
329
    }
330
331
    /**
332
     * Specify cache closure
333
     *
334
     * @param Closure $closure
335
     * @return $this
336
     */
337
    public function cache(Closure $closure): CacheEntity
338
    {
339
        $this->cacheClosure = $closure;
340
341
        return $this;
342
    }
343
}
344