1 | <?php |
||||||
2 | |||||||
3 | namespace Helldar\Roles\Traits; |
||||||
4 | |||||||
5 | use Closure; |
||||||
6 | use Helldar\Roles\Facades\Config; |
||||||
7 | use Illuminate\Support\Arr; |
||||||
8 | use Illuminate\Support\Facades\Cache; |
||||||
9 | use Illuminate\Support\Str; |
||||||
10 | |||||||
11 | /** @mixin \Illuminate\Database\Eloquent\Model */ |
||||||
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( |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
38 | 6 | $this->getKeyName() |
|||||
0 ignored issues
–
show
It seems like
getKeyName() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
39 | ); |
||||||
40 | } |
||||||
41 | |||||||
42 | 6 | protected function cacheTtl(): int |
|||||
43 | { |
||||||
44 | 6 | return Config::cacheTtl(); |
|||||
45 | } |
||||||
46 | |||||||
47 | 72 | protected function allowCache(): bool |
|||||
48 | { |
||||||
49 | 72 | return Config::useCache(); |
|||||
50 | } |
||||||
51 | } |
||||||
52 |