1 | <?php |
||
2 | |||
3 | namespace Sfneal\Queries; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Builder; |
||
6 | use Sfneal\Caching\Traits\Cacheable; |
||
7 | use Sfneal\Models\Model; |
||
8 | |||
9 | class CacheModelQuery extends Query |
||
10 | { |
||
11 | /** |
||
12 | * Inherit cache methods. |
||
13 | */ |
||
14 | use Cacheable; |
||
15 | |||
16 | /** |
||
17 | * Target Model. |
||
18 | * |
||
19 | * @var Model |
||
20 | */ |
||
21 | protected $model; |
||
22 | |||
23 | /** |
||
24 | * Model's attribute to cache. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $attribute; |
||
29 | |||
30 | /** |
||
31 | * Model ID. |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | public $model_key; |
||
36 | |||
37 | /** |
||
38 | * QueryCacheAttribute constructor. |
||
39 | * |
||
40 | * @param int $model_key |
||
41 | * @param string|null $model |
||
42 | * @param string|null $attribute |
||
43 | */ |
||
44 | public function __construct(int $model_key, string $model = null, string $attribute = null) |
||
45 | { |
||
46 | $this->model_key = $model_key; |
||
47 | $this->model = $this->model ?? $model; |
||
0 ignored issues
–
show
|
|||
48 | $this->attribute = $this->attribute ?? $attribute; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Retrieve a Query builder. |
||
53 | * |
||
54 | * @return Builder |
||
55 | */ |
||
56 | protected function builder(): Builder |
||
57 | { |
||
58 | return $this->model::query(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Retrieve a Service's title. |
||
63 | * |
||
64 | * @return Model|string |
||
65 | */ |
||
66 | public function execute() |
||
67 | { |
||
68 | // Retrieve the model |
||
69 | $model = $this->builder()->find($this->model_key); |
||
70 | |||
71 | // Return the entire model if no attribute is set |
||
72 | if (is_null($this->attribute)) { |
||
0 ignored issues
–
show
|
|||
73 | return $model; |
||
74 | } |
||
75 | |||
76 | // Return a specific model |
||
77 | else { |
||
78 | return $model->getAttribute($this->attribute); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Key to use for cache store. |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function cacheKey(): string |
||
88 | { |
||
89 | $table = $this->model::getTableName(); |
||
90 | $key = "{$table}:{$this->model_key}"; |
||
91 | |||
92 | return $key.(is_null($this->attribute) ? '' : ":{$this->attribute}"); |
||
0 ignored issues
–
show
|
|||
93 | } |
||
94 | } |
||
95 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.