Completed
Pull Request — master (#1)
by James
02:59
created

SimpleCacheAdapter::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Roave\DoctrineSimpleCache;
5
6
use Doctrine\Common\Cache\Cache as DoctrineCache;
7
use Doctrine\Common\Cache\ClearableCache;
8
use Doctrine\Common\Cache\MultiGetCache;
9
use Doctrine\Common\Cache\MultiPutCache;
10
use Psr\SimpleCache\CacheInterface as PsrCache;
11
12
final class SimpleCacheAdapter implements PsrCache
13
{
14
    /**
15
     * @var DoctrineCache
16
     */
17
    private $doctrineCache;
18
19 12
    public function __construct(DoctrineCache $doctrineCache)
20
    {
21 12
        $this->doctrineCache = $doctrineCache;
22 12
    }
23
24
    /**
25
     * {@inheritDoc}
26
     */
27 1
    public function get($key, $default = null)
28
    {
29 1
        return $this->doctrineCache->fetch($key);
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35 1
    public function set($key, $value, $ttl = null)
36
    {
37 1
        return $this->doctrineCache->save($key, $value, $ttl);
0 ignored issues
show
Bug introduced by
It seems like $ttl defined by parameter $ttl on line 35 can also be of type null; however, Doctrine\Common\Cache\Cache::save() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     */
43 3
    public function delete($key)
44
    {
45 3
        return $this->doctrineCache->delete($key);
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     * @throws \Roave\DoctrineSimpleCache\CacheException
51
     */
52 2
    public function clear()
53
    {
54 2
        if (!$this->doctrineCache instanceof ClearableCache) {
55 1
            throw CacheException::fromNonClearableCache($this->doctrineCache);
56
        }
57
58 1
        return $this->doctrineCache->deleteAll();
59
    }
60
61
    /**
62
     * {@inheritDoc}
63
     * @throws \Roave\DoctrineSimpleCache\CacheException
64
     */
65 2
    public function getMultiple($keys, $default = null)
66
    {
67 2
        if (!$this->doctrineCache instanceof MultiGetCache) {
68 1
            throw CacheException::fromNonMultiGetCache($this->doctrineCache);
69
        }
70
71 1
        return $this->doctrineCache->fetchMultiple($keys);
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     * @throws \Roave\DoctrineSimpleCache\CacheException
77
     */
78 2
    public function setMultiple($values, $ttl = null)
79
    {
80 2
        if (!$this->doctrineCache instanceof MultiPutCache) {
81 1
            throw CacheException::fromNonMultiGetCache($this->doctrineCache);
82
        }
83
84 1
        return $this->doctrineCache->saveMultiple($values, $ttl);
85
    }
86
87
    /**
88
     * {@inheritDoc}
89
     */
90 2
    public function deleteMultiple($keys)
91
    {
92 2
        $success = true;
93
94 2
        foreach ($keys as $key) {
95 2
            if (!$this->delete($key)) {
96 2
                $success = false;
97
            }
98
        }
99
100 2
        return $success;
101
    }
102
103
    /**
104
     * {@inheritDoc}
105
     */
106 1
    public function has($key)
107
    {
108 1
        return $this->doctrineCache->contains($key);
109
    }
110
}
111