Passed
Push — master ( 74cbab...62dc6b )
by Stephen
02:33
created

AbstractQueryCacheAttribute::cacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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