1 | <?php |
||||
2 | /** |
||||
3 | * Created by PhpStorm. |
||||
4 | * User: arthur |
||||
5 | * Date: 11.10.18 |
||||
6 | * Time: 23:33. |
||||
7 | */ |
||||
8 | |||||
9 | namespace Foundation\Traits; |
||||
10 | |||||
11 | use Foundation\Cache\ModelCache; |
||||
12 | use Illuminate\Contracts\Support\Arrayable; |
||||
13 | |||||
14 | /** |
||||
15 | * Trait Cacheable. |
||||
16 | * |
||||
17 | * @property int $cacheTime |
||||
18 | * @property array $secondaryCacheIndexes |
||||
19 | */ |
||||
20 | trait Cacheable |
||||
21 | { |
||||
22 | private static $caching; |
||||
23 | |||||
24 | 1 | public static function cache(): ModelCache |
|||
25 | { |
||||
26 | 1 | if (! isset(static::$caching)) { |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
27 | 1 | static::$caching = new ModelCache(static::class, get_class_property(static::class, 'secondaryCacheIndexes'), get_class_property(static::class, 'cacheTime')); |
|||
0 ignored issues
–
show
It seems like
get_class_property(stati...secondaryCacheIndexes') can also be of type null ; however, parameter $indexes of Foundation\Cache\ModelCache::__construct() does only seem to accept array , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
28 | } |
||||
29 | |||||
30 | 1 | return static::$caching; |
|||
31 | } |
||||
32 | |||||
33 | public static function find($id, $columns = ['*']) |
||||
34 | { |
||||
35 | if (static::cache()->enabled()) { |
||||
36 | $model = static::cache()->find($id) ?? static::recache($id); |
||||
37 | |||||
38 | return static::filterFromColumns($model, $columns); |
||||
39 | } |
||||
40 | |||||
41 | return static::findWithoutCache($id, $columns); |
||||
42 | } |
||||
43 | |||||
44 | private static function recache($id) |
||||
45 | { |
||||
46 | $model = static::findWithoutCache($id); |
||||
47 | static::cache()->store($model); |
||||
48 | |||||
49 | return $model; |
||||
50 | } |
||||
51 | |||||
52 | public static function findWithoutCache($id, $columns = ['*']) |
||||
53 | { |
||||
54 | $model = new static(); |
||||
55 | if (is_array($id) || $id instanceof Arrayable) { |
||||
56 | return $model::whereIn($model->getKeyName(), $id)->get($columns); |
||||
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
![]() |
|||||
57 | } |
||||
58 | |||||
59 | return $model::whereKey($id)->first($columns); |
||||
60 | } |
||||
61 | |||||
62 | private static function filterFromColumns($model, $columns) |
||||
63 | { |
||||
64 | if ($model === null) { |
||||
65 | return; |
||||
66 | } |
||||
67 | |||||
68 | if ($columns !== ['*']) { |
||||
69 | return collect($model)->first($columns); |
||||
70 | } |
||||
71 | |||||
72 | return $model; |
||||
73 | } |
||||
74 | } |
||||
75 |