Passed
Push — master ( 4581a8...197035 )
by Stephen
02:40
created

AbstractQueryCacheAttribute::isCached()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Queries;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Sfneal\Caching\Traits\Cacheable;
7
use Sfneal\Helpers\Redis\RedisCache;
8
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
75
    {
76
        return RedisCache::exists($this->cacheKey());
77
    }
78
}
79