Completed
Pull Request — master (#332)
by Alexander M.
34:09
created

CacheAdapter::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

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 5
rs 10
1
<?php
2
3
namespace Doctrine\Common\Cache\Psr6;
4
5
use Doctrine\Common\Cache\Cache;
6
use Doctrine\Common\Cache\ClearableCache;
7
use Doctrine\Common\Cache\MultiOperationCache;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Cache\CacheItemPoolInterface;
10
11
final class CacheAdapter implements CacheItemPoolInterface
12
{
13
    /** @var Cache|ClearableCache|MultiOperationCache */
14
    private $cache;
15
16
    /**
0 ignored issues
show
introduced by
Found multi-line comment for property \Doctrine\Common\Cache\Psr6\CacheAdapter::$deferredItems with single line content, use one-line comment instead.
Loading history...
17
     * @var CacheItemInterface[]
18
     */
19
    private $deferredItems = [];
20
21
    public function __construct(Cache $cache)
22
    {
23
        $this->cache = $cache;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function getItem($key): CacheItemInterface
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
30
    {
31
        $this->assertValidKey($key);
32
33
        if (isset($this->deferredItems[$key])) {
34
            return new CacheItem($key, $this->deferredItems[$key]->get());
35
        }
36
37
        return new CacheItem($key, $this->cache->fetch($key));
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Doctrine\Common\Cache\ClearableCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\ClearableCache. ( Ignorable by Annotation )

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

37
        return new CacheItem($key, $this->cache->/** @scrutinizer ignore-call */ fetch($key));
Loading history...
Bug introduced by
The method fetch() does not exist on Doctrine\Common\Cache\MultiOperationCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\MultiOperationCache. ( Ignorable by Annotation )

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

37
        return new CacheItem($key, $this->cache->/** @scrutinizer ignore-call */ fetch($key));
Loading history...
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function getItems(array $keys = []): iterable
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
44
    {
45
        $this->assertValidKeys($keys);
46
47
        $fetchedValues = $this->cache->fetchMultiple($keys);
0 ignored issues
show
Bug introduced by
The method fetchMultiple() does not exist on Doctrine\Common\Cache\Cache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\Cache. ( Ignorable by Annotation )

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

47
        /** @scrutinizer ignore-call */ 
48
        $fetchedValues = $this->cache->fetchMultiple($keys);
Loading history...
Bug introduced by
The method fetchMultiple() does not exist on Doctrine\Common\Cache\ClearableCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\ClearableCache. ( Ignorable by Annotation )

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

47
        /** @scrutinizer ignore-call */ 
48
        $fetchedValues = $this->cache->fetchMultiple($keys);
Loading history...
48
        $items = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
49
        foreach ($keys as $key) {
50
            $items[$key] = new CacheItem($key, isset($this->deferredItems[$key]) ? $this->deferredItems[$key]->get() : (\array_key_exists($key, $fetchedValues) ? $fetchedValues[$key] : false));
0 ignored issues
show
introduced by
Function \array_key_exists() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
51
        }
52
53
        return $items;
54
    }
55
56
    /**
57
     * @inheritDoc
58
     */
59
    public function hasItem($key): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
60
    {
61
        $this->assertValidKey($key);
62
63
        return isset($this->deferredItems[$key])
64
            ? !$this->isExpired($this->deferredItems[$key])
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
65
            : $this->cache->contains($key)
0 ignored issues
show
Bug introduced by
The method contains() does not exist on Doctrine\Common\Cache\MultiOperationCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\MultiOperationCache. ( Ignorable by Annotation )

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

65
            : $this->cache->/** @scrutinizer ignore-call */ contains($key)
Loading history...
Bug introduced by
The method contains() does not exist on Doctrine\Common\Cache\ClearableCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\ClearableCache. ( Ignorable by Annotation )

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

65
            : $this->cache->/** @scrutinizer ignore-call */ contains($key)
Loading history...
66
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ");" but found ")\n ;"
Loading history...
67
    }
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function clear(): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
73
    {
74
        $this->deferredItems = [];
75
76
        return $this->cache->deleteAll();
0 ignored issues
show
Bug introduced by
The method deleteAll() does not exist on Doctrine\Common\Cache\Cache. Did you maybe mean delete()? ( Ignorable by Annotation )

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

76
        return $this->cache->/** @scrutinizer ignore-call */ deleteAll();

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...
Bug introduced by
The method deleteAll() does not exist on Doctrine\Common\Cache\MultiOperationCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\MultiOperationCache. ( Ignorable by Annotation )

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

76
        return $this->cache->/** @scrutinizer ignore-call */ deleteAll();
Loading history...
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function deleteItem($key): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
83
    {
84
        $this->assertValidKey($key);
85
        unset($this->deferredItems[$key]);
86
87
        return $this->cache->delete($key);
0 ignored issues
show
Bug introduced by
The method delete() does not exist on Doctrine\Common\Cache\MultiOperationCache. Did you maybe mean deleteMultiple()? ( Ignorable by Annotation )

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

87
        return $this->cache->/** @scrutinizer ignore-call */ delete($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...
Bug introduced by
The method delete() does not exist on Doctrine\Common\Cache\ClearableCache. Did you maybe mean deleteAll()? ( Ignorable by Annotation )

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

87
        return $this->cache->/** @scrutinizer ignore-call */ delete($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...
88
    }
89
90
    /**
91
     * @inheritDoc
92
     */
93
    public function deleteItems(array $keys): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
94
    {
95
        $this->assertValidKeys($keys);
96
97
        foreach ($keys as $key) {
98
            unset($this->deferredItems[$key]);
99
        }
100
101
        return $this->cache->deleteMultiple($keys);
0 ignored issues
show
Bug introduced by
The method deleteMultiple() does not exist on Doctrine\Common\Cache\Cache. Did you maybe mean delete()? ( Ignorable by Annotation )

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

101
        return $this->cache->/** @scrutinizer ignore-call */ deleteMultiple($keys);

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...
Bug introduced by
The method deleteMultiple() does not exist on Doctrine\Common\Cache\ClearableCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\ClearableCache. ( Ignorable by Annotation )

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

101
        return $this->cache->/** @scrutinizer ignore-call */ deleteMultiple($keys);
Loading history...
102
    }
103
104
    /**
105
     * @inheritDoc
106
     */
107
    public function save(CacheItemInterface $item): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
108
    {
109
        unset($this->deferredItems[$item->getKey()]);
110
111
        if ($this->isExpired($item)) {
112
            return $this->cache->delete($item->getKey());
113
        }
114
115
        return $this->cache->save($item->getKey(), $item->get(), !$item instanceof CacheItem || null === $item->getExpiration() ? 0 : $item->getExpiration()->getTimestamp() - time());
0 ignored issues
show
Bug introduced by
The method save() does not exist on Doctrine\Common\Cache\MultiOperationCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\MultiOperationCache. ( Ignorable by Annotation )

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

115
        return $this->cache->/** @scrutinizer ignore-call */ save($item->getKey(), $item->get(), !$item instanceof CacheItem || null === $item->getExpiration() ? 0 : $item->getExpiration()->getTimestamp() - time());
Loading history...
Bug introduced by
The method save() does not exist on Doctrine\Common\Cache\ClearableCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\ClearableCache. ( Ignorable by Annotation )

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

115
        return $this->cache->/** @scrutinizer ignore-call */ save($item->getKey(), $item->get(), !$item instanceof CacheItem || null === $item->getExpiration() ? 0 : $item->getExpiration()->getTimestamp() - time());
Loading history...
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Yoda comparisons are disallowed.
Loading history...
introduced by
Function time() should not be referenced via a fallback global name, but via a use statement.
Loading history...
116
    }
117
118
    /**
119
     * @inheritDoc
120
     */
121
    public function saveDeferred(CacheItemInterface $item): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
122
    {
123
        if ($this->isExpired($item)) {
124
            return $this->cache->delete($item->getKey());
125
        }
126
127
        $this->deferredItems[$item->getKey()] = $item;
128
129
        return true;
130
    }
131
132
    /**
133
     * @inheritDoc
134
     */
135
    public function commit(): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
136
    {
137
        $success =  $this->cache->saveMultiple(array_map(
0 ignored issues
show
Bug introduced by
The method saveMultiple() does not exist on Doctrine\Common\Cache\ClearableCache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\ClearableCache. ( Ignorable by Annotation )

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

137
        /** @scrutinizer ignore-call */ 
138
        $success =  $this->cache->saveMultiple(array_map(
Loading history...
Bug introduced by
The method saveMultiple() does not exist on Doctrine\Common\Cache\Cache. Since it exists in all sub-types, consider adding an abstract or default implementation to Doctrine\Common\Cache\Cache. ( Ignorable by Annotation )

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

137
        /** @scrutinizer ignore-call */ 
138
        $success =  $this->cache->saveMultiple(array_map(
Loading history...
introduced by
Function array_map() should not be referenced via a fallback global name, but via a use statement.
Loading history...
138
            static function (CacheItemInterface $cacheItem) {
139
                return $cacheItem->get();
140
            },
141
            $this->deferredItems
142
        ));
143
144
        if ($success) {
145
            $this->deferredItems = [];
146
        }
147
148
        return $success;
149
    }
150
151
    public function __destruct()
152
    {
153
        $this->commit();
154
    }
155
156
    private function assertValidKey($key): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
157
    {
158
        if (!\is_string($key) || '' === $key || preg_match('#[\{\}\(\)/\\\\@:]#', $key)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
introduced by
Function \is_string() should not be referenced via a fully qualified name, but via a use statement.
Loading history...
introduced by
Yoda comparisons are disallowed.
Loading history...
introduced by
Function preg_match() should not be referenced via a fallback global name, but via a use statement.
Loading history...
159
            throw new InvalidArgumentException('Invalid cache key.');
160
        }
161
    }
162
163
    private function assertValidKeys(iterable $keys): void
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
164
    {
165
        foreach ($keys as $key) {
166
            $this->assertValidKey($key);
167
        }
168
    }
169
170
    private function isExpired(CacheItemInterface $item): bool
0 ignored issues
show
introduced by
There must be exactly 1 whitespace between closing parenthesis and return type colon.
Loading history...
171
    {
172
        return $item instanceof CacheItem
173
            && null !== $item->getExpiration()
0 ignored issues
show
introduced by
Yoda comparisons are disallowed.
Loading history...
174
            && $item->getExpiration() < new \DateTimeImmutable()
0 ignored issues
show
introduced by
Class \DateTimeImmutable should not be referenced via a fully qualified name, but via a use statement.
Loading history...
175
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ");" but found ")\n ;"
Loading history...
176
    }
177
}
178