AbstractQueryCacheAttribute   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 3 1
A cacheKey() 0 5 1
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