AbstractQueryCacheAttribute::execute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sfneal\Queries;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Sfneal\Caching\Traits\Cacheable;
7
8
abstract class AbstractQueryCacheAttribute extends AbstractQuery
9
{
10
    /**
11
     * Inherit cache methods.
12
     */
13
    use Cacheable;
14
15
    /**
16
     * Target Model.
17
     *
18
     * @var Model
19
     */
20
    protected $model;
21
22
    /**
23
     * Model's attribute to cache.
24
     *
25
     * @var string
26
     */
27
    protected $attribute;
28
29
    /**
30
     * Model ID.
31
     *
32
     * @var int
33
     */
34
    public $model_key;
35
36
    /**
37
     * QueryCacheAttribute constructor.
38
     *
39
     * @param int $model_key
40
     */
41
    public function __construct(int $model_key)
42
    {
43
        $this->model_key = $model_key;
44
    }
45
46
    /**
47
     * Retrieve a Service's title.
48
     *
49
     * @return string
50
     */
51
    public function execute(): string
52
    {
53
        return $this->model::query()->find($this->model_key)->getAttribute($this->attribute);
54
    }
55
56
    /**
57
     * Key to use for cache store.
58
     *
59
     * @return string
60
     */
61
    public function cacheKey(): string
62
    {
63
        $table = (new $this->model)->getTable();
64
65
        return "{$table}:{$this->model_key}#{$this->attribute}";
66
    }
67
}
68