Total Complexity | 6 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | trait Cacheable |
||
13 | { |
||
14 | 72 | protected function cache(string $prefix, Closure $callback, $values = []) |
|
15 | { |
||
16 | 72 | if ($this->allowCache()) { |
|
17 | 6 | $key = $this->cacheKey($prefix, $values); |
|
18 | 6 | $ttl = $this->cacheTtl(); |
|
19 | |||
20 | 6 | return Cache::remember($key, $ttl, $callback); |
|
21 | } |
||
22 | |||
23 | 66 | return $callback(); |
|
24 | } |
||
25 | |||
26 | 6 | protected function cacheKey(string $prefix, $values = []): string |
|
27 | { |
||
28 | 6 | $values = Arr::flatten(Arr::wrap($values)); |
|
29 | |||
30 | 6 | return Str::slug( |
|
31 | 6 | $prefix . '-' . $this->cacheUserKey() . '-' . implode('-', $values) |
|
32 | ); |
||
33 | } |
||
34 | |||
35 | 6 | protected function cacheUserKey(): string |
|
36 | { |
||
37 | 6 | return $this->getAttribute( |
|
|
|||
38 | 6 | $this->getKeyName() |
|
39 | ); |
||
40 | } |
||
41 | |||
42 | 6 | protected function cacheTtl(): int |
|
45 | } |
||
46 | |||
47 | 72 | protected function allowCache(): bool |
|
48 | { |
||
52 |