Psr6Cache::pull()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Mosaic\Cache\Adapters\Psr6;
4
5
use Closure;
6
use Mosaic\Cache\Cache;
7
use Psr\Cache\CacheItemInterface;
8
use Psr\Cache\CacheItemPoolInterface;
9
use Psr\Cache\InvalidArgumentException;
10
11
class Psr6Cache implements Cache, CacheItemPoolInterface
12
{
13
    /**
14
     * @var CacheItemPoolInterface
15
     */
16
    private $pool;
17
18
    /**
19
     * @param CacheItemPoolInterface $pool
20
     */
21
    public function __construct(CacheItemPoolInterface $pool)
22
    {
23
        $this->pool = $pool;
24
    }
25
26
    /**
27
     * Determine if an item exists in the cache.
28
     *
29
     * @param  string $key
30
     * @return bool
31
     */
32
    public function has($key)
33
    {
34
        return $this->pool->hasItem($key);
35
    }
36
37
    /**
38
     * Retrieve an item from the cache by key.
39
     *
40
     * @param  string $key
41
     * @param  mixed  $default
42
     * @return mixed
43
     */
44
    public function get($key, $default = null)
45
    {
46
        if (!$this->has($key)) {
47
            return $default;
48
        }
49
50
        return $this->pool->getItem($key)->get();
51
    }
52
53
    /**
54
     * Retrieve an item from the cache and delete it.
55
     *
56
     * @param  string $key
57
     * @param  mixed  $default
58
     * @return mixed
59
     */
60
    public function pull($key, $default = null)
61
    {
62
        $value = $this->get($key, $default);
63
64
        $this->forget($key);
65
66
        return $value;
67
    }
68
69
    /**
70
     * Store an item in the cache.
71
     *
72
     * @param  string        $key
73
     * @param  mixed         $value
74
     * @param  \DateTime|int $minutes
75
     * @return bool
76
     */
77
    public function put($key, $value, $minutes)
78
    {
79
        $cache = $this->pool->getItem($key);
80
81
        $cache->set($value);
82
        $cache->expiresAfter($minutes * 60);
83
84
        return $this->pool->save($cache);
85
    }
86
87
    /**
88
     * Store an item in the cache if the key does not exist.
89
     *
90
     * @param  string        $key
91
     * @param  mixed         $value
92
     * @param  \DateTime|int $minutes
93
     * @return bool
94
     */
95
    public function add($key, $value, $minutes)
96
    {
97
        if ($this->has($key)) {
98
            return false;
99
        }
100
101
        return $this->put($key, $value, $minutes);
102
    }
103
104
    /**
105
     * Store an item in the cache indefinitely.
106
     *
107
     * @param  string $key
108
     * @param  mixed  $value
109
     * @return bool
110
     */
111
    public function forever($key, $value)
112
    {
113
        $cache = $this->pool->getItem($key);
114
115
        $cache->set($value);
116
117
        return $this->pool->save($cache);
118
    }
119
120
    /**
121
     * Get an item from the cache, or store the default value.
122
     *
123
     * @param  string        $key
124
     * @param  \DateTime|int $minutes
125
     * @param  \Closure      $callback
126
     * @return mixed
127
     */
128 View Code Duplication
    public function remember($key, $minutes, Closure $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        if (!is_null($value = $this->get($key))) {
131
            return $value;
132
        }
133
134
        $this->put($key, $value = $callback(), $minutes);
135
136
        return $value;
137
    }
138
139
    /**
140
     * Get an item from the cache, or store the default value forever.
141
     *
142
     * @param  string   $key
143
     * @param  \Closure $callback
144
     * @return mixed
145
     */
146 View Code Duplication
    public function rememberForever($key, Closure $callback)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        if (!is_null($value = $this->get($key))) {
149
            return $value;
150
        }
151
152
        $this->forever($key, $value = $callback());
153
154
        return $value;
155
    }
156
157
    /**
158
     * Remove an item from the cache.
159
     *
160
     * @param  string $key
161
     * @return bool
162
     */
163
    public function forget($key)
164
    {
165
        return $this->pool->deleteItem($key);
166
    }
167
168
    /**
169
     * Returns a Cache Item representing the specified key.
170
     *
171
     * This method must always return a CacheItemInterface object, even in case of
172
     * a cache miss. It MUST NOT return null.
173
     *
174
     * @param string $key
175
     *                    The key for which to return the corresponding Cache Item.
176
     *
177
     * @throws InvalidArgumentException
178
     *                                  If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
179
     *                                  MUST be thrown.
180
     *
181
     * @return CacheItemInterface
182
     *                            The corresponding Cache Item.
183
     */
184
    public function getItem($key)
185
    {
186
        return $this->pool->getItem($key);
187
    }
188
189
    /**
190
     * Returns a traversable set of cache items.
191
     *
192
     * @param array $keys
193
     *                    An indexed array of keys of items to retrieve.
194
     *
195
     * @throws InvalidArgumentException
196
     *                                  If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
197
     *                                  MUST be thrown.
198
     *
199
     * @return array|\Traversable
200
     *                            A traversable collection of Cache Items keyed by the cache keys of
201
     *                            each item. A Cache item will be returned for each key, even if that
202
     *                            key is not found. However, if no keys are specified then an empty
203
     *                            traversable MUST be returned instead.
204
     */
205
    public function getItems(array $keys = [])
206
    {
207
        return $this->pool->getItems($keys);
208
    }
209
210
    /**
211
     * Confirms if the cache contains specified cache item.
212
     *
213
     * Note: This method MAY avoid retrieving the cached value for performance reasons.
214
     * This could result in a race condition with CacheItemInterface::get(). To avoid
215
     * such situation use CacheItemInterface::isHit() instead.
216
     *
217
     * @param string $key
218
     *                    The key for which to check existence.
219
     *
220
     * @throws InvalidArgumentException
221
     *                                  If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
222
     *                                  MUST be thrown.
223
     *
224
     * @return bool
225
     *              True if item exists in the cache, false otherwise.
226
     */
227
    public function hasItem($key)
228
    {
229
        return $this->pool->hasItem($key);
230
    }
231
232
    /**
233
     * Deletes all items in the pool.
234
     *
235
     * @return bool
236
     *              True if the pool was successfully cleared. False if there was an error.
237
     */
238
    public function clear()
239
    {
240
        return $this->pool->clear();
241
    }
242
243
    /**
244
     * Removes the item from the pool.
245
     *
246
     * @param string $key
247
     *                    The key for which to delete
248
     *
249
     * @throws InvalidArgumentException
250
     *                                  If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
251
     *                                  MUST be thrown.
252
     *
253
     * @return bool
254
     *              True if the item was successfully removed. False if there was an error.
255
     */
256
    public function deleteItem($key)
257
    {
258
        return $this->pool->deleteItem($key);
259
    }
260
261
    /**
262
     * Removes multiple items from the pool.
263
     *
264
     * @param  array                    $keys
265
     *                                        An array of keys that should be removed from the pool.
266
     * @throws InvalidArgumentException
267
     *                                       If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
268
     *                                       MUST be thrown.
269
     *
270
     * @return bool
271
     *              True if the items were successfully removed. False if there was an error.
272
     */
273
    public function deleteItems(array $keys)
274
    {
275
        return $this->pool->deleteItems($keys);
276
    }
277
278
    /**
279
     * Persists a cache item immediately.
280
     *
281
     * @param CacheItemInterface $item
282
     *                                 The cache item to save.
283
     *
284
     * @return bool
285
     *              True if the item was successfully persisted. False if there was an error.
286
     */
287
    public function save(CacheItemInterface $item)
288
    {
289
        return $this->pool->save($item);
290
    }
291
292
    /**
293
     * Sets a cache item to be persisted later.
294
     *
295
     * @param CacheItemInterface $item
296
     *                                 The cache item to save.
297
     *
298
     * @return bool
299
     *              False if the item could not be queued or if a commit was attempted and failed. True otherwise.
300
     */
301
    public function saveDeferred(CacheItemInterface $item)
302
    {
303
        return $this->pool->saveDeferred($item);
304
    }
305
306
    /**
307
     * Persists any deferred cache items.
308
     *
309
     * @return bool
310
     *              True if all not-yet-saved items were successfully saved or there were none. False otherwise.
311
     */
312
    public function commit()
313
    {
314
        return $this->pool->commit();
315
    }
316
317
    /**
318
     * @return CacheItemPoolInterface
319
     */
320
    public function toPsr6() : CacheItemPoolInterface
321
    {
322
        return $this;
323
    }
324
}
325