Completed
Pull Request — master (#19)
by Wouter
01:22
created

CacheItemPool::save()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 13
cts 15
cp 0.8667
rs 9.1288
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5.0592
1
<?php
2
namespace Madewithlove\IlluminatePsrCacheBridge\Laravel;
3
4
use DateTimeImmutable;
5
use Exception;
6
use Illuminate\Contracts\Cache\Repository;
7
use Madewithlove\IlluminatePsrCacheBridge\Exceptions\InvalidArgumentException;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Cache\CacheItemPoolInterface;
10
11
class CacheItemPool implements CacheItemPoolInterface
12
{
13
    /**
14
     * @var \Illuminate\Contracts\Cache\Repository
15
     */
16
    private $repository;
17
18
    /**
19
     * @var \Psr\Cache\CacheItemInterface[]
20
     */
21
    private $deferred = [];
22
23
    /**
24
     * @param \Illuminate\Contracts\Cache\Repository $repository
25
     */
26 142
    public function __construct(Repository $repository)
27
    {
28 142
        $this->repository = $repository;
29 142
    }
30
31
    /**
32
     * Destructor.
33
     */
34 23
    public function __destruct()
35
    {
36 23
        $this->commit();
37 23
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 77
    public function getItem($key)
43
    {
44 77
        $this->validateKey($key);
45
46 60
        if (isset($this->deferred[$key])) {
47 5
            return clone($this->deferred[$key]);
48 60
        } elseif ($this->repository->has($key)) {
49 26
            return new CacheItem($key, unserialize($this->repository->get($key)), true);
50
        } else {
51 58
            return new CacheItem($key);
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getItems(array $keys = array())
59
    {
60 22
        return array_combine($keys, array_map(function($key) {
61 20
            return $this->getItem($key);
62 22
        }, $keys));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 35
    public function hasItem($key)
69
    {
70 35
        $this->validateKey($key);
71
72 17
        if (isset($this->deferred[$key])) {
73 3
            $item = $this->deferred[$key];
74 3
            $expiresAt = $this->getExpiresAt($item);
0 ignored issues
show
Compatibility introduced by
$item of type object<Psr\Cache\CacheItemInterface> is not a sub-type of object<Madewithlove\Illu...idge\Laravel\CacheItem>. It seems like you assume a concrete implementation of the interface Psr\Cache\CacheItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
75
76 3
            if (!$expiresAt) {
77 1
                return true;
78
            }
79
80 2
            return $expiresAt > new DateTimeImmutable();
81
        }
82
83 14
        return $this->repository->has($key);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 122
    public function clear()
90
    {
91
        try {
92 122
            $this->deferred = [];
93 122
            $store = $this->repository;
94
            /* @var \Illuminate\Contracts\Cache\Store $store */
95 122
            $store->flush();
96 1
        } catch (Exception $exception) {
97 1
            return false;
98
        }
99
100 121
        return true;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 26
    public function deleteItem($key)
107
    {
108 26
        $this->validateKey($key);
109
110 8
        unset($this->deferred[$key]);
111
112 8
        if (!$this->hasItem($key)) {
113 5
            return true;
114
        }
115
116 5
        return $this->repository->forget($key);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122 22
    public function deleteItems(array $keys)
123
    {
124
        // Validating all keys first.
125 22
        foreach ($keys as $key) {
126 21
            $this->validateKey($key);
127
        }
128
129 4
        $success = true;
130
131 4
        foreach ($keys as $key) {
132 3
            $success = $success && $this->deleteItem($key);
133
        }
134
135 4
        return $success;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 35
    public function save(CacheItemInterface $item)
142
    {
143 35
        $expiresAt = $this->getExpiresAt($item);
0 ignored issues
show
Compatibility introduced by
$item of type object<Psr\Cache\CacheItemInterface> is not a sub-type of object<Madewithlove\Illu...idge\Laravel\CacheItem>. It seems like you assume a concrete implementation of the interface Psr\Cache\CacheItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
144
145 35
        if (!$expiresAt) {
146
            try {
147 30
                $this->repository->forever($item->getKey(), serialize($item->get()));
148 1
            } catch (Exception $exception) {
149 1
                return false;
150
            }
151
152 29
            return true;
153
        }
154
155 5
        $lifetime = LifetimeHelper::computeLifetime($expiresAt);
156
157 5
        if ($lifetime <= 0) {
158 1
            $this->repository->forget($item->getKey());
159
160 1
            return false;
161
        }
162
163
        try {
164 5
            $this->repository->put($item->getKey(), serialize($item->get()), $lifetime);
165
        } catch (Exception $exception) {
166
            return false;
167
        }
168
169 5
        return true;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175 13
    public function saveDeferred(CacheItemInterface $item)
176
    {
177 13
        $expiresAt = $this->getExpiresAt($item);
0 ignored issues
show
Compatibility introduced by
$item of type object<Psr\Cache\CacheItemInterface> is not a sub-type of object<Madewithlove\Illu...idge\Laravel\CacheItem>. It seems like you assume a concrete implementation of the interface Psr\Cache\CacheItemInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
178
179 13
        if ($expiresAt && ($expiresAt < new DateTimeImmutable())) {
180 1
            return false;
181
        }
182
183 12
        $item = (new CacheItem($item->getKey(), $item->get(), true))->expiresAt($expiresAt);
184
185 12
        $this->deferred[$item->getKey()] = $item;
186
187 12
        return true;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193 31
    public function commit()
194
    {
195 31
        $success = true;
196
197 31
        foreach ($this->deferred as $key => $item) {
198 9
            $success = $success && $this->save($item);
199
        }
200
201 31
        $this->deferred = [];
202
203 31
        return $success;
204
    }
205
206
    /**
207
     * @param string $key
208
     *
209
     * @throws \Psr\Cache\InvalidArgumentException
210
     */
211 136
    private function validateKey($key)
212
    {
213 136
        if (!is_string($key) || preg_match('#[{}\(\)/\\\\@:]#', $key)) {
214 88
            throw new InvalidArgumentException();
215
        }
216 83
    }
217
218
    /**
219
     * @param \Madewithlove\IlluminatePsrCacheBridge\Laravel\CacheItem $item
220
     *
221
     * @return \DateTimeInterface
222
     */
223 39
    private function getExpiresAt(CacheItem $item)
224
    {
225 39
        return $item->getExpiresAt();
226
    }
227
}
228