AbstractEnhancedProvider::has()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.125

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 18
ccs 4
cts 8
cp 0.5
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 4.125
1
<?php namespace Comodojo\SimpleCache\Providers;
2
3
use \Comodojo\Cache\Traits\StatefulTrait;
4
use \Comodojo\Cache\Traits\NamespaceTrait;
5
use \Comodojo\SimpleCache\Interfaces\EnhancedSimpleCacheInterface;
6
use \Comodojo\Foundation\Utils\ClassProperties;
7
use \Comodojo\Foundation\Utils\UniqueId;
8
use \Psr\Log\LoggerInterface;
9
use \Comodojo\Cache\Components\KeyValidator;
10
use \DateTime;
11
use \DateInterval;
12
use \Traversable;
13
use \Comodojo\Exception\SimpleCacheException;
14
use \Comodojo\Exception\InvalidSimpleCacheArgumentException;
15
use \Exception;
16
17
/**
18
 * @package     Comodojo Cache
19
 * @author      Marco Giovinazzi <[email protected]>
20
 * @license     MIT
21
 *
22
 * LICENSE:
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
 * THE SOFTWARE.
31
 */
32
33
abstract class AbstractEnhancedProvider
34
    extends AbstractProvider
35
    implements EnhancedSimpleCacheInterface {
36
37
    use StatefulTrait;
38
    use NamespaceTrait;
39
40
    protected $driver;
41
42
    protected $default_properties = [];
43
44
    protected $properties;
45
46
    private $queue = [];
0 ignored issues
show
introduced by
The private property $queue is not used, and could be removed.
Loading history...
47
48 145
    public function __construct(array $properties = [], LoggerInterface $logger = null) {
49
50 145
        parent::__construct($logger);
51
52 145
        $this->properties = ClassProperties::create($this->default_properties)->merge($properties);
53
54 145
        $this->setId(UniqueId::generate(64));
55
56 145
    }
57
58 55
    public function getProperties() {
59
60 55
        return $this->properties;
61
62
    }
63
64 91
    public function get($key, $default = null) {
65
66 91
        if ( KeyValidator::validateKey($key) === false ) {
67
            throw new InvalidSimpleCacheArgumentException('Invalid key provided');
68
        }
69
70
        try {
71
72 91
            $data = $this->driver->get($key, $this->getNamespace());
73
74
        } catch (Exception $e) {
75
76
            $this->setState(self::CACHE_ERROR, $e->getMessage());
77
            $data = null;
78
79
        }
80
81 91
        if ( $data === null ) return $default;
82
83 61
        return unserialize($data);
84
85
    }
86
87 104
    public function set($key, $value, $ttl = null) {
88
89 104
        if ( KeyValidator::validateKey($key) === false ) {
90
            throw new InvalidSimpleCacheArgumentException('Invalid key provided');
91
        }
92
93 104
        if ( $value === null ) {
94 7
            throw new InvalidSimpleCacheArgumentException('Cannot cache a null value');
95
        }
96
97
        $real_ttl;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $real_ttl seems to be never defined.
Loading history...
98
99 97
        if ( $ttl == null || $ttl == 0 ) $real_ttl = 0;
100 22
        else if ( $ttl instanceof DateInterval ) $real_ttl = $ttl->format('%s');
101 22
        else $real_ttl = intval($ttl);
102
103
        try {
104
105 97
            $data = $this->driver->set($key, $this->getNamespace(), serialize($value), $real_ttl);
106
107
        } catch (Exception $e) {
108
109
            $this->setState(self::CACHE_ERROR, $e->getMessage());
110
            $data = false;
111
112
        }
113
114 97
        return $data;
115
116
    }
117
118 16
    public function delete($key) {
119
120 16
        if ( KeyValidator::validateKey($key) === false ) {
121
            throw new InvalidSimpleCacheArgumentException('Invalid key provided');
122
        }
123
124
        try {
125
126 16
            $data = $this->driver->delete($key, $this->getNamespace());
127
128
        } catch (Exception $e) {
129
130
            $this->setState(self::CACHE_ERROR, $e->getMessage());
131
            $data = false;
132
133
        }
134
135 16
        return $data;
136
137
    }
138
139 8
    public function clear() {
140
141
        try {
142
143 8
            $data = $this->driver->clear();
144
145
        } catch (Exception $e) {
146
147
            $this->setState(self::CACHE_ERROR, $e->getMessage());
148
            $data = false;
149
150
        }
151
152 8
        return $data;
153
154
    }
155
156 8
    public function getMultiple($keys, $default = null) {
157
158 8
        if ( !is_array($keys) && !($keys instanceof Traversable) ) {
159
            throw new InvalidSimpleCacheArgumentException('Invalid keys provided');
160
        }
161
162 8
        foreach ( $keys as $key ) {
163 8
            if ( KeyValidator::validateKey($key) === false ) {
164 8
                throw new InvalidSimpleCacheArgumentException('Invalid key provided');
165
            }
166
        }
167
168
        try {
169
170 8
            $data = $this->driver->getMultiple($keys, $this->getNamespace());
171
172
        } catch (Exception $e) {
173
174
            $this->setState(self::CACHE_ERROR, $e->getMessage());
175
            $data = array_combine($keys, array_fill(0, count($keys), null));
0 ignored issues
show
Bug introduced by
It seems like $keys can also be of type Traversable; however, parameter $keys of array_combine() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

175
            $data = array_combine(/** @scrutinizer ignore-type */ $keys, array_fill(0, count($keys), null));
Loading history...
Bug introduced by
It seems like $keys can also be of type Traversable; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

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

175
            $data = array_combine($keys, array_fill(0, count(/** @scrutinizer ignore-type */ $keys), null));
Loading history...
176
177
        }
178
179 8
        return array_map(function($value) use($default) {
180 8
            if ( $value === null ) return $default;
181 7
            return unserialize($value);
182 8
        }, $data);
183
184
    }
185
186 8
    public function setMultiple($values, $ttl = null) {
187
188 8
        if ( !is_array($values) && !($values instanceof Traversable) ) {
189
            throw new InvalidSimpleCacheArgumentException('Invalid keys provided');
190
        }
191
192 8
        $real_values = [];
193
194 8
        foreach ( $values as $key => $value ) {
195 8
            if ( KeyValidator::validateKey($key) === false ) {
196
                throw new InvalidSimpleCacheArgumentException('Invalid key provided');
197
            }
198 8
            if ( $value === null ) {
199
                throw new InvalidSimpleCacheArgumentException('Cannot cache a null value');
200
            }
201 8
            $real_values[$key] = serialize($value);
202
        }
203
204
        $real_ttl;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $real_ttl seems to be never defined.
Loading history...
205
206 8
        if ( $ttl == null || $ttl == 0 ) $real_ttl = 0;
207
        else if ( $ttl instanceof DateInterval ) $real_ttl = $ttl->format('%s');
208
        else $real_ttl = intval($ttl);
209
210
        try {
211
212 8
            $data = $this->driver->setMultiple($real_values, $this->getNamespace(), $real_ttl);
213
214
        } catch (Exception $e) {
215
216
            $this->setState(self::CACHE_ERROR, $e->getMessage());
217
            $data = false;
218
219
        }
220
221 8
        return $data;
222
223
    }
224
225 8
    public function deleteMultiple($keys) {
226
227 8
        if ( !is_array($keys) && !($keys instanceof Traversable) ) {
228
            throw new InvalidSimpleCacheArgumentException('Invalid keys provided');
229
        }
230
231 8
        foreach ( $keys as $key ) {
232 8
            if ( KeyValidator::validateKey($key) === false ) {
233 8
                throw new InvalidSimpleCacheArgumentException('Invalid key provided');
234
            }
235
        }
236
237
        try {
238
239 8
            $data = $this->driver->deleteMultiple($keys, $this->getNamespace());
240
241
        } catch (Exception $e) {
242
243
            $this->setState(self::CACHE_ERROR, $e->getMessage());
244
            $data = false;
245
246
        }
247
248 8
        return $data;
249
250
    }
251
252 33
    public function has($key) {
253
254 33
        if ( KeyValidator::validateKey($key) === false ) {
255
            throw new InvalidSimpleCacheArgumentException('Invalid key provided');
256
        }
257
258
        try {
259
260 33
            $data = $this->driver->has($key, $this->getNamespace());
261
262
        } catch (Exception $e) {
263
264
            $this->setState(self::CACHE_ERROR, $e->getMessage());
265
            $data = false;
266
267
        }
268
269 33
        return $data;
270
271
    }
272
273
    abstract public function getStats();
274
275 8
    public function clearNamespace() {
276
277
        try {
278
279 8
            $data = $this->driver->clear($this->getNamespace());
280
281
        } catch (Exception $e) {
282
283
            $this->setState(self::CACHE_ERROR, $e->getMessage());
284
            $data = false;
285
286
        }
287
288 8
        return $data;
289
290
    }
291
292 145
    public function test() {
293
294 145
        if ( $this->driver->test() ) {
295
296 145
            $this->setState(self::CACHE_SUCCESS);
297
298 145
            return true;
299
300
        }
301
302
        $error = $this->driver->getName()." driver unavailable, disabling provider ".$this->getId()." administratively";
303
304
        $this->logger->error($error);
305
306
        $this->setState(self::CACHE_ERROR, $error);
307
308
        return false;
309
310
    }
311
312
}
313