Passed
Branch support-laravel-6 (ddd0b3)
by Pieter
06:42
created

CacheItemPool::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
namespace W2w\Laravel\Apie\Services\Psr6;
3
4
use DateTimeImmutable;
5
use Exception;
6
use Illuminate\Contracts\Cache\Repository;
7
use Madewithlove\IlluminatePsrCacheBridge\Exceptions\InvalidArgumentException;
0 ignored issues
show
Bug introduced by
The type Madewithlove\IlluminateP...nvalidArgumentException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public function __construct(Repository $repository)
27
    {
28
        $this->repository = $repository;
29
    }
30
31
    /**
32
     * Destructor.
33
     */
34
    public function __destruct()
35
    {
36
        $this->commit();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getItem($key)
43
    {
44
        $this->validateKey($key);
45
46
        if (isset($this->deferred[$key])) {
47
            return clone($this->deferred[$key]);
48
        } elseif ($this->repository->has($key)) {
49
            return new CacheItem($key, unserialize($this->repository->get($key)), true);
50
        } else {
51
            return new CacheItem($key);
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getItems(array $keys = array())
59
    {
60
        return array_combine($keys, array_map(function($key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_combine($ke... { /* ... */ }, $keys)) also could return the type false which is incompatible with the return type mandated by Psr\Cache\CacheItemPoolInterface::getItems() of Traversable|array.
Loading history...
61
            return $this->getItem($key);
62
        }, $keys));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function hasItem($key)
69
    {
70
        $this->validateKey($key);
71
72
        if (isset($this->deferred[$key])) {
73
            $item = $this->deferred[$key];
74
            $expiresAt = $this->getExpiresAt($item);
75
76
            if (!$expiresAt) {
0 ignored issues
show
introduced by
$expiresAt is of type DateTimeInterface, thus it always evaluated to true.
Loading history...
77
                return true;
78
            }
79
80
            return $expiresAt > new DateTimeImmutable();
81
        }
82
83
        return $this->repository->has($key);
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function clear()
90
    {
91
        try {
92
            $this->deferred = [];
93
            $store = $this->repository;
94
            /* @var \Illuminate\Contracts\Cache\Store $store */
95
            $store->flush();
96
        } catch (Exception $exception) {
97
            return false;
98
        }
99
100
        return true;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function deleteItem($key)
107
    {
108
        $this->validateKey($key);
109
110
        unset($this->deferred[$key]);
111
112
        if (!$this->hasItem($key)) {
113
            return true;
114
        }
115
116
        return $this->repository->forget($key);
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function deleteItems(array $keys)
123
    {
124
        // Validating all keys first.
125
        foreach ($keys as $key) {
126
            $this->validateKey($key);
127
        }
128
129
        $success = true;
130
131
        foreach ($keys as $key) {
132
            $success = $success && $this->deleteItem($key);
133
        }
134
135
        return $success;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function save(CacheItemInterface $item)
142
    {
143
        $expiresAt = $this->getExpiresAt($item);
144
145
        if (!$expiresAt) {
0 ignored issues
show
introduced by
$expiresAt is of type DateTimeInterface, thus it always evaluated to true.
Loading history...
146
            try {
147
                $this->repository->forever($item->getKey(), serialize($item->get()));
148
            } catch (Exception $exception) {
149
                return false;
150
            }
151
152
            return true;
153
        }
154
155
        $lifetime = LifetimeHelper::computeLifetime($expiresAt);
156
157
        if ($lifetime <= 0) {
158
            $this->repository->forget($item->getKey());
159
160
            return false;
161
        }
162
163
        try {
164
            $this->repository->put($item->getKey(), serialize($item->get()), $lifetime);
165
        } catch (Exception $exception) {
166
            return false;
167
        }
168
169
        return true;
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function saveDeferred(CacheItemInterface $item)
176
    {
177
        $expiresAt = $this->getExpiresAt($item);
178
179
        if ($expiresAt && ($expiresAt < new DateTimeImmutable())) {
180
            return false;
181
        }
182
183
        $item = (new CacheItem($item->getKey(), $item->get(), true))->expiresAt($expiresAt);
184
185
        $this->deferred[$item->getKey()] = $item;
186
187
        return true;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function commit()
194
    {
195
        $success = true;
196
197
        foreach ($this->deferred as $key => $item) {
198
            $success = $success && $this->save($item);
199
        }
200
201
        $this->deferred = [];
202
203
        return $success;
204
    }
205
206
    /**
207
     * @param string $key
208
     *
209
     * @throws \Psr\Cache\InvalidArgumentException
210
     */
211
    private function validateKey($key)
212
    {
213
        if (!is_string($key) || preg_match('#[{}\(\)/\\\\@:]#', $key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always true.
Loading history...
214
            throw new InvalidArgumentException();
215
        }
216
    }
217
218
    /**
219
     * @param \Madewithlove\IlluminatePsrCacheBridge\Laravel\CacheItem $item
0 ignored issues
show
Bug introduced by
The type Madewithlove\IlluminateP...ridge\Laravel\CacheItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
220
     *
221
     * @return \DateTimeInterface
222
     */
223
    private function getExpiresAt(CacheItem $item)
224
    {
225
        return $item->getExpiresAt();
226
    }
227
}
228