Total Complexity | 4 |
Total Lines | 68 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
9 | abstract class AbstractQueryCacheAttribute extends AbstractQuery |
||
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 | */ |
||
42 | public function __construct(int $model_key) |
||
43 | { |
||
44 | $this->model_key = $model_key; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Retrieve a Service's title. |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function execute(): string |
||
53 | { |
||
54 | return $this->model::query()->find($this->model_key)->getAttribute($this->attribute); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Key to use for cache store. |
||
59 | * |
||
60 | * @return string |
||
61 | */ |
||
62 | public function cacheKey(): string |
||
63 | { |
||
64 | $table = (new $this->model)->getTable(); |
||
65 | |||
66 | return "{$table}:{$this->model_key}:{$this->attribute}"; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Determine if the query is currently cached |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function isCached(): bool |
||
77 | } |
||
78 | } |
||
79 |