Passed
Push — master ( 6692d3...4dd3a5 )
by Sébastien
02:56 queued 15s
created

CachableTrait::setCacheLifetime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bdf\Prime\Query\Extension;
4
5
use Bdf\Prime\Cache\CacheInterface;
6
use Bdf\Prime\Cache\CacheKey;
7
use Bdf\Prime\Connection\ConnectionInterface;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
8
use Bdf\Prime\Query\Contract\Cachable;
0 ignored issues
show
introduced by
Unused use statement
Loading history...
9
10
/**
11
 * Provides result cache on queries
12
 *
13
 * @see Cachable
14
 *
15
 * @property ConnectionInterface $connection
16
 *
17
 * @todo Cache statement instead of assoc array result ?
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
18
 */
19
trait CachableTrait
20
{
21
    /**
22
     * @var null|CacheInterface
23
     */
24
    protected $cache;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
25
26
    /**
27
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
28
     */
29
    protected $disableCache = true;
30
31
    /**
32
     * @var CacheKey
33
     */
34
    protected $cacheKey = null;
35
36
37
    /**
38
     * @see Cachable::cache()
39
     */
40 4
    public function cache()
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
41
    {
42 4
        return $this->disableCache ? null : $this->cache;
0 ignored issues
show
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
43
    }
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $cache should have a doc-comment as per coding-style.
Loading history...
46
     * @see Cachable::setCache()
47
     */
48 482
    public function setCache(CacheInterface $cache = null)
49
    {
50 482
        $this->disableCache = $cache === null;
0 ignored issues
show
Coding Style introduced by
The value of a comparison must not be assigned to a variable
Loading history...
51 482
        $this->cache = $cache;
52
53 482
        return $this;
54
    }
55
56
    /**
57
     * @see Cachable::disableCache()
58
     */
59 7
    public function disableCache()
60
    {
61 7
        $this->disableCache = true;
62
63 7
        return $this;
64
    }
65
66
    /**
67
     * Define the cache lifetime
68
     *
69
     * @param int $lifetime The cache lifetime in seconds
0 ignored issues
show
Coding Style introduced by
Expected "integer" but found "int" for parameter type
Loading history...
70
     *
71
     * @return $this
72
     */
73 1
    public function setCacheLifetime(int $lifetime)
74
    {
75 1
        $this->getCacheKey()->setLifetime($lifetime);
76
77 1
        return $this;
78
    }
79
80
    /**
81
     * Define the cache key
82
     *
83
     * @param string $cacheKey
84
     *
85
     * @return $this
86
     */
87 1
    public function setCacheKey(string $cacheKey)
88
    {
89 1
        $this->getCacheKey()->setKey($cacheKey);
90
91 1
        return $this;
92
    }
93
94
    /**
95
     * Define the cache namespace
96
     *
97
     * @param string $namespace
98
     *
99
     * @return $this
100
     */
101 1
    public function setCacheNamespace(string $namespace)
102
    {
103 1
        $this->getCacheKey()->setNamespace($namespace);
104
105 1
        return $this;
106
    }
107
108
    /**
109
     * Get the cache key
110
     *
111
     * @return CacheKey
112
     */
113 560
    public function getCacheKey(): CacheKey
114
    {
115 560
        if ($this->cacheKey === null) {
116 546
            return $this->cacheKey = new CacheKey(
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
117
                function () { return $this->cacheNamespace(); },
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
introduced by
There should be no white space after an opening "{"
Loading history...
introduced by
There should be no white space before a closing "}"
Loading history...
Coding Style introduced by
Closing brace of nested function must be on a new line
Loading history...
118
                function () { return $this->cacheKey(); }
0 ignored issues
show
Coding Style introduced by
Opening brace must be the last content on the line
Loading history...
introduced by
There should be no white space after an opening "{"
Loading history...
introduced by
There should be no white space before a closing "}"
Loading history...
Coding Style introduced by
Closing brace of nested function must be on a new line
Loading history...
119
            );
120
        }
121
122 139
        return $this->cacheKey;
123
    }
124
125
    /**
126
     * Retrieve data from cache, or execute the query and save into cache
127
     *
128
     * @return mixed
129
     */
130 559
    protected function executeCached()
131
    {
132 559
        $key = $this->getCacheKey();
133
134 559
        if ($this->disableCache || !$key->valid()) {
135 543
            return $this->connection->execute($this)->all();
0 ignored issues
show
Bug introduced by
$this of type Bdf\Prime\Query\Extension\CachableTrait is incompatible with the type Bdf\Prime\Query\Contract\Compilable expected by parameter $query of Bdf\Prime\Connection\Con...ionInterface::execute(). ( Ignorable by Annotation )

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

135
            return $this->connection->execute(/** @scrutinizer ignore-type */ $this)->all();
Loading history...
136
        }
137
138 23
        $data = $this->cache->get($key);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

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

138
        /** @scrutinizer ignore-call */ 
139
        $data = $this->cache->get($key);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
140 23
        if ($data !== null) {
0 ignored issues
show
introduced by
The condition $data !== null is always true.
Loading history...
141 5
            return $data;
142
        }
143
144 23
        $data = $this->connection->execute($this)->all();
145
146 23
        $this->cache->set($key, $data);
147
148 23
        return $data;
149
    }
150
151
    /**
152
     * Clear the cache when a write operation is performed
153
     */
154 565
    protected function clearCacheOnWrite()
155
    {
156 565
        if ($this->cache) {
157 10
            $this->cache->flush($this->getCacheKey()->namespace());
158
        }
159 565
    }
160
161
    /**
162
     * Get the cache key
163
     * The cache key is generated from the query string
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
164
     *
165
     * @return string
166
     */
167
    protected function cacheKey()
168
    {
169
        return null;
170
    }
171
172
    /**
173
     * Get cache namespace
174
     * The namespace is in form : [connection name] ":" [table name]
0 ignored issues
show
introduced by
Doc comment short description must be on a single line, further text should be a separate paragraph
Loading history...
175
     *
176
     * @return string
177
     */
178 16
    protected function cacheNamespace()
179
    {
180 16
        return $this->connection->getName().':'.(isset($this->statements['tables'][0]['table']) ? $this->statements['tables'][0]['table'] : '');
0 ignored issues
show
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
181
    }
182
}
183