GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Cache::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * This file is part of the O2System PHP Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Reactor\Services;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Cache\Abstracts\AbstractItemPool;
19
use O2System\Cache\Adapters;
20
use O2System\Cache\Item;
21
use Psr\Cache\CacheItemInterface;
22
use Psr\Cache\CacheItemPoolInterface;
23
use Psr\SimpleCache\CacheInterface;
24
use O2System\Spl\Iterators\ArrayIterator;
25
26
/**
27
 * Class Cache
28
 * @package O2System\Reactor\Services
29
 */
30
class Cache extends Adapters implements CacheItemPoolInterface, CacheInterface
31
{
32
    private $poolOffset = 'default';
33
34
    // ------------------------------------------------------------------------
35
36
    public function setItemPool($poolOffset)
37
    {
38
        if ($this->exists($poolOffset)) {
39
            $this->poolOffset = $poolOffset;
40
        }
41
42
        return $this;
43
    }
44
45
    // ------------------------------------------------------------------------
46
47
    /**
48
     * Adapters::getItems
49
     *
50
     * Returns a traversable set of cache items.
51
     *
52
     * @param string[] $keys
53
     *   An indexed array of keys of items to retrieve.
54
     *
55
     * @throws InvalidArgumentException
56
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
57
     *   MUST be thrown.
58
     *
59
     * @return array|\Traversable
60
     *   A traversable collection of Cache Items keyed by the cache keys of
61
     *   each item. A Cache item will be returned for each key, even if that
62
     *   key is not found. However, if no keys are specified then an empty
63
     *   traversable MUST be returned instead.
64
     */
65
    public function getItems(array $keys = [])
66
    {
67
        if (false !== ($cacheItem = $this->callPoolOffset('getItems', [$keys]))) {
68
            return $cacheItem;
69
        }
70
71
        return [];
72
    }
73
74
    // ------------------------------------------------------------------------
75
76
    public function callPoolOffset($method, array $args = [])
77
    {
78
        $poolOffset = $this->poolOffset;
79
80
        if ( ! $this->exists($poolOffset)) {
81
            $poolOffset = 'default';
82
        }
83
84
        $itemPool = $this->getItemPool($poolOffset);
85
86
        if ($itemPool instanceof AbstractItemPool) {
87
            if (method_exists($itemPool, $method)) {
88
                return call_user_func_array([&$itemPool, $method], $args);
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    // ------------------------------------------------------------------------
96
97
    /**
98
     * Adapters::clear
99
     *
100
     * Deletes all items in the pool.
101
     *
102
     * @return bool
103
     *   True if the pool was successfully cleared. False if there was an error.
104
     */
105
    public function clear()
106
    {
107
        return $this->callPoolOffset('clear');
108
    }
109
110
    // ------------------------------------------------------------------------
111
112
    /**
113
     * Adapters::saveDeferred
114
     *
115
     * Sets a cache item to be persisted later.
116
     *
117
     * @param CacheItemInterface $item
118
     *   The cache item to save.
119
     *
120
     * @return bool
121
     *   False if the item could not be queued or if a commit was attempted and failed. True otherwise.
122
     */
123
    public function saveDeferred(CacheItemInterface $item)
124
    {
125
        return $this->callPoolOffset('saveDeferred', [$item]);
126
    }
127
128
    // ------------------------------------------------------------------------
129
130
    /**
131
     * Adapters::commit
132
     *
133
     * Persists any deferred cache items.
134
     *
135
     * @return bool
136
     *   True if all not-yet-saved items were successfully saved or there were none. False otherwise.
137
     */
138
    public function commit()
139
    {
140
        return $this->callPoolOffset('commit');
141
    }
142
143
    // ------------------------------------------------------------------------
144
145
    /**
146
     * Delete an item from the cache by its unique key.
147
     *
148
     * @param string $key The unique cache key of the item to delete.
149
     *
150
     * @return bool True if the item was successfully removed. False if there was an error.
151
     *
152
     * @throws \Psr\SimpleCache\InvalidArgumentException
153
     *   MUST be thrown if the $key string is not a legal value.
154
     */
155
    public function delete($key)
156
    {
157
        return (bool)$this->deleteItem($key);
158
    }
159
160
    // ------------------------------------------------------------------------
161
162
    /**
163
     * Adapters::deleteItem
164
     *
165
     * Removes the item from the pool.
166
     *
167
     * @param string $key
168
     *   The key to delete.
169
     *
170
     * @throws InvalidArgumentException
171
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
172
     *   MUST be thrown.
173
     *
174
     * @return bool
175
     *   True if the item was successfully removed. False if there was an error.
176
     */
177
    public function deleteItem($key)
178
    {
179
        return $this->callPoolOffset('deleteItem', [$key]);
180
    }
181
182
    // ------------------------------------------------------------------------
183
184
    /**
185
     * Obtains multiple cache items by their unique keys.
186
     *
187
     * @param iterable $keys    A list of keys that can obtained in a single operation.
188
     * @param mixed    $default Default value to return for keys that do not exist.
189
     *
190
     * @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as
191
     *                  value.
192
     *
193
     * @throws \Psr\SimpleCache\InvalidArgumentException
194
     *   MUST be thrown if $keys is neither an array nor a Traversable,
195
     *   or if any of the $keys are not a legal value.
196
     */
197
    public function getMultiple($keys, $default = null)
198
    {
199
        $result = new ArrayIterator();
200
201
        foreach ($keys as $key) {
202
            if ($this->exists($key)) {
203
                $result[ $key ] = $this->get($key, $default);
204
            }
205
        }
206
207
        return $result;
208
    }
209
210
    // ------------------------------------------------------------------------
211
212
    /**
213
     * Fetches a value from the cache.
214
     *
215
     * @param string $key     The unique key of this item in the cache.
216
     * @param mixed  $default Default value to return if the key does not exist.
217
     *
218
     * @return mixed The value of the item from the cache, or $default in case of cache miss.
219
     *
220
     * @throws \Psr\SimpleCache\InvalidArgumentException
221
     *   MUST be thrown if the $key string is not a legal value.
222
     */
223
    public function get($key, $default = null)
224
    {
225
        if ($this->hasItem($key)) {
226
            $item = $this->getItem($key);
227
228
            return $item->get();
229
        }
230
231
        return $default;
232
    }
233
234
    // ------------------------------------------------------------------------
235
236
    /**
237
     * Adapters::hasItem
238
     *
239
     * Confirms if the cache contains specified cache item.
240
     *
241
     * Note: This method MAY avoid retrieving the cached value for performance reasons.
242
     * This could result in a race condition with CacheItemInterface::get(). To avoid
243
     * such situation use CacheItemInterface::isHit() instead.
244
     *
245
     * @param string $key
246
     *   The key for which to check existence.
247
     *
248
     * @throws InvalidArgumentException
249
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
250
     *   MUST be thrown.
251
     *
252
     * @return bool
253
     *   True if item exists in the cache, false otherwise.
254
     */
255
    public function hasItem($key)
256
    {
257
        return $this->callPoolOffset('hasItem', [$key]);
258
    }
259
260
    // ------------------------------------------------------------------------
261
262
    /**
263
     * Cache::getItem
264
     *
265
     * Returns a Cache Item representing the specified key.
266
     *
267
     * This method must always return a CacheItemInterface object, even in case of
268
     * a cache miss. It MUST NOT return null.
269
     *
270
     * @param string $key
271
     *   The key for which to return the corresponding Cache Item.
272
     *
273
     * @throws InvalidArgumentException
274
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
275
     *   MUST be thrown.
276
     *
277
     * @return CacheItemInterface
278
     *   The corresponding Cache Item.
279
     */
280
    public function getItem($key)
281
    {
282
        if (false !== ($cacheItem = $this->callPoolOffset('getItem', [$key]))) {
283
            return $cacheItem;
284
        }
285
286
        return new Item(null, null);
287
    }
288
289
    // ------------------------------------------------------------------------
290
291
    /**
292
     * Persists a set of key => value pairs in the cache, with an optional TTL.
293
     *
294
     * @param iterable               $values A list of key => value pairs for a multiple-set operation.
295
     * @param null|int|\DateInterval $ttl    Optional. The TTL value of this item. If no value is sent and
296
     *                                       the driver supports TTL then the library may set a default value
297
     *                                       for it or let the driver take care of that.
298
     *
299
     * @return bool True on success and false on failure.
300
     *
301
     * @throws \Psr\SimpleCache\InvalidArgumentException
302
     *   MUST be thrown if $values is neither an array nor a Traversable,
303
     *   or if any of the $values are not a legal value.
304
     */
305
    public function setMultiple($values, $ttl = null)
306
    {
307
        $result = [];
308
309
        foreach ($values as $key => $value) {
310
            if ($this->set($key, $value, $ttl)) {
311
                $result[ $key ] = true;
312
            }
313
        }
314
315
        return (bool)count($result) == count($values);
316
    }
317
318
    // ------------------------------------------------------------------------
319
320
    /**
321
     * Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
322
     *
323
     * @param string                 $key   The key of the item to store.
324
     * @param mixed                  $value The value of the item to store, must be serializable.
325
     * @param null|int|\DateInterval $ttl   Optional. The TTL value of this item. If no value is sent and
326
     *                                      the driver supports TTL then the library may set a default value
327
     *                                      for it or let the driver take care of that.
328
     *
329
     * @return bool True on success and false on failure.
330
     *
331
     * @throws \Psr\SimpleCache\InvalidArgumentException
332
     *   MUST be thrown if the $key string is not a legal value.
333
     */
334
    public function set($key, $value, $ttl = null)
335
    {
336
        return $this->save(new Item($key, $value, $ttl));
337
    }
338
339
    // ------------------------------------------------------------------------
340
341
    /**
342
     * Adapters::save
343
     *
344
     * Persists a cache item immediately.
345
     *
346
     * @param CacheItemInterface $item
347
     *   The cache item to save.
348
     *
349
     * @return bool
350
     *   True if the item was successfully persisted. False if there was an error.
351
     */
352
    public function save(CacheItemInterface $item)
353
    {
354
        return $this->callPoolOffset('save', [$item]);
355
    }
356
357
    // ------------------------------------------------------------------------
358
359
    /**
360
     * Deletes multiple cache items in a single operation.
361
     *
362
     * @param iterable $keys A list of string-based keys to be deleted.
363
     *
364
     * @return bool True if the items were successfully removed. False if there was an error.
365
     *
366
     * @throws \Psr\SimpleCache\InvalidArgumentException
367
     *   MUST be thrown if $keys is neither an array nor a Traversable,
368
     *   or if any of the $keys are not a legal value.
369
     */
370
    public function deleteMultiple($keys)
371
    {
372
        return (bool)$this->deleteItems($keys);
0 ignored issues
show
Bug introduced by
$keys of type iterable is incompatible with the type array expected by parameter $keys of O2System\Reactor\Services\Cache::deleteItems(). ( Ignorable by Annotation )

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

372
        return (bool)$this->deleteItems(/** @scrutinizer ignore-type */ $keys);
Loading history...
373
    }
374
375
    // ------------------------------------------------------------------------
376
377
    /**
378
     * Adapters::deleteItems
379
     *
380
     * Removes multiple items from the pool.
381
     *
382
     * @param string[] $keys
383
     *   An array of keys that should be removed from the pool.
384
     *
385
     * @throws InvalidArgumentException
386
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
387
     *   MUST be thrown.
388
     *
389
     * @return bool
390
     *   True if the items were successfully removed. False if there was an error.
391
     */
392
    public function deleteItems(array $keys = [])
393
    {
394
        return $this->callPoolOffset('deleteItems', [$keys]);
395
    }
396
397
    // ------------------------------------------------------------------------
398
399
    /**
400
     * Determines whether an item is present in the cache.
401
     *
402
     * NOTE: It is recommended that has() is only to be used for cache warming type purposes
403
     * and not to be used within your live applications operations for get/set, as this method
404
     * is subject to a race condition where your has() will return true and immediately after,
405
     * another script can remove it making the state of your app out of date.
406
     *
407
     * @param string $key The cache item key.
408
     *
409
     * @return bool
410
     *
411
     * @throws \Psr\SimpleCache\InvalidArgumentException
412
     *   MUST be thrown if the $key string is not a legal value.
413
     */
414
    public function has($key)
415
    {
416
        return $this->hasItem($key);
417
    }
418
}