Passed
Push — master ( 642b52...a20a1d )
by Gabriel
11:08
created

CanCache::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

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
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace Nip\Cache\Cacheable;
4
5
use DateInterval;
6
use Nip\Utility\Str;
7
8
/**
9
 * Trait CanCache
10
 * @package Nip\Cache\Cacheable
11
 */
12
trait CanCache
13
{
14
    use HasCacheStore;
15
16
    protected ?CacheData $data = null;
17
    protected $needsCaching = false;
18
19
    /**
20
     * @param null $needCaching
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $needCaching is correct as it would always require null to be passed?
Loading history...
21
     * @return bool
22
     */
23
    public function needsCaching($needCaching = null): bool
24
    {
25
        if (is_bool($needCaching)) {
0 ignored issues
show
introduced by
The condition is_bool($needCaching) is always false.
Loading history...
26
            $this->needsCaching = $needCaching;
27
        }
28
        return $this->needsCaching;
29
    }
30
    public function getData(): ?CacheData
31
    {
32
        $this->checkInitData();
33
        return $this->data;
34
    }
35
36
    protected function checkInitData()
37
    {
38
        if ($this->data !== null) {
39
            return;
40
        }
41
        $cacheStore = $this->cacheStore();
42
        if ($cacheStore->has($this->cacheName())) {
43
            $this->data = $cacheStore->get($this->cacheName());
44
        } else {
45
            $this->data = new CacheData();
46
            if (method_exists($this, 'generateCacheData')) {
47
                $data = $this->generateCacheData();
48
                $this->setDataToCache($data);
49
            }
50
            $this->saveDataToCache();
51
        }
52
    }
53
54
    protected function checkSaveCache()
55
    {
56
        if ($this->needsCaching() !== true) {
57
            return;
58
        }
59
        $this->saveDataToCache();
60
    }
61
62
    /**
63
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
64
     * @return mixed|null
65
     * @throws \Psr\SimpleCache\InvalidArgumentException
66
     */
67
    protected function getDataFromCache($key = null, $default = null)
68
    {
69
        $key = $this->dataCacheKey($key);
70
        if ($this->getData()->offsetExists($key)) {
71
            return $this->getData()->get($key);
72
        }
73
        $value = $this->generateCacheDataKey($key, $default);
74
        $this->setDataToCache($value, $key);
75
        return $value;
76
    }
77
78
    /**
79
     * @param $data
80
     * @param $key
81
     * @return $this
82
     */
83
    protected function setDataToCache($data, $key = null)
84
    {
85
        $key = $this->dataCacheKey($key);
86
        $this->getData()->set($data, $key);
87
        $this->needsCaching(true);
88
        return $this;
89
    }
90
91
    /**
92
     * @param $data
93
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
94
     * @noinspection PhpDocMissingThrowsInspection
95
     */
96
    protected function saveDataToCache($data = null, $key = null)
97
    {
98
        if ($data) {
99
            $this->setDataToCache($data, $key);
100
        }
101
        /** @noinspection PhpUnhandledExceptionInspection */
102
        $this->cacheStore()->set($this->cacheName(), $this->getData(), $this->dataCacheTtl($key));
103
        $this->needsCaching(false);
104
    }
105
106
    /**
107
     * @param $key
108
     * @return string
109
     */
110
    protected function dataCacheKey($key = null): string
111
    {
112
        if ($key !== null) {
113
            return $key;
114
        }
115
        return CacheData::DEFAULT_KEY;
116
    }
117
118
    protected function cacheName(): string
119
    {
120
        return Str::slug(__CLASS__);
121
    }
122
123
    /**
124
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
125
     * @return DateInterval
126
     */
127
    protected function dataCacheTtl($key = null)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

127
    protected function dataCacheTtl(/** @scrutinizer ignore-unused */ $key = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
    {
129
        return DateInterval::createFromDateString('10 years');
130
    }
131
132
    protected function generateCacheDataKey(string $key, mixed $default = null)
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

132
    protected function generateCacheDataKey(/** @scrutinizer ignore-unused */ string $key, mixed $default = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    {
134
        return $default;
135
    }
136
137
    public function __destruct()
138
    {
139
        $this->checkSaveCache();
140
    }
141
}
142