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.

ItemPool::clear()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the O2System 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\Cache\Adapters\Apcu;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Cache\Item;
19
use Psr\Cache\CacheItemInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Cache\CacheItemInterface 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...
20
use O2System\Spl\Exceptions\Logic\InvalidArgumentException;
21
22
/**
23
 * Class ItemPool
24
 *
25
 * @package O2System\Cache\Adapters\Apc
26
 */
27
class ItemPool extends Adapter
28
{
29
    /**
30
     * ItemPool::getItems
31
     *
32
     * Returns a traversable set of cache items.
33
     *
34
     * @param string[] $keys
35
     *   An indexed array of keys of items to retrieve.
36
     *
37
     * @throws InvalidArgumentException
38
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
39
     *   MUST be thrown.
40
     *
41
     * @return array|\Traversable
42
     *   A traversable collection of Cache Items keyed by the cache keys of
43
     *   each item. A Cache item will be returned for each key, even if that
44
     *   key is not found. However, if no keys are specified then an empty
45
     *   traversable MUST be returned instead.
46
     */
47
    public function getItems(array $keys = [])
48
    {
49
        if ( ! is_array($keys)) {
0 ignored issues
show
introduced by
The condition is_array($keys) is always true.
Loading history...
50
            throw new InvalidArgumentException('E_INVALID_ARGUMENT_ARRAY_CACHE_EXCEPTION');
51
        }
52
        $items = [];
53
54
        if (empty($keys) AND class_exists('APCUIterator', false)) {
55
            $apcIterator = new \APCUIterator();
56
57
            foreach ($apcIterator as $item) {
58
                $items[] = new Item(str_replace($this->prefixKey, '', $item[ 'key' ]), $item[ 'value' ]);
59
            }
60
        } elseif (count($keys)) {
61
            foreach ($keys as $key) {
62
                $items[] = $this->getItem($key);
63
            }
64
        }
65
66
        return $items;
67
    }
68
69
    // ------------------------------------------------------------------------
70
71
    /**
72
     * ItemPool::getKey
73
     *
74
     * Returns a Cache Item representing the specified key.
75
     *
76
     * This method must always return a CacheItemInterface object, even in case of
77
     * a cache miss. It MUST NOT return null.
78
     *
79
     * @param string $key
80
     *   The key for which to return the corresponding Cache Item.
81
     *
82
     * @throws InvalidArgumentException
83
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
84
     *   MUST be thrown.
85
     *
86
     * @return CacheItemInterface
87
     *   The corresponding Cache Item.
88
     */
89
    public function getItem($key)
90
    {
91
        if ( ! is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always true.
Loading history...
92
            throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
93
        }
94
95
        $success = false;
96
97
        $metadata = apcu_fetch($this->prefixKey . $key, $success);
98
99
        return new Item($key, $metadata);
100
    }
101
102
    // ------------------------------------------------------------------------
103
104
    /**
105
     * ItemPool::hasItem
106
     *
107
     * Confirms if the cache contains specified cache item.
108
     *
109
     * Note: This method MAY avoid retrieving the cached value for performance reasons.
110
     * This could result in a race condition with CacheItemInterface::get(). To avoid
111
     * such situation use CacheItemInterface::isHit() instead.
112
     *
113
     * @param string $key
114
     *   The key for which to check existence.
115
     *
116
     * @throws InvalidArgumentException
117
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
118
     *   MUST be thrown.
119
     *
120
     * @return bool
121
     *   True if item exists in the cache, false otherwise.
122
     */
123
    public function hasItem($key)
124
    {
125
        if ( ! is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always true.
Loading history...
126
            throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
127
        }
128
129
        return (bool)apcu_exists($this->prefixKey . $key);
130
    }
131
132
    // ------------------------------------------------------------------------
133
134
    /**
135
     * ItemPool::clear
136
     *
137
     * Deletes all items in the pool.
138
     *
139
     * @return bool
140
     *   True if the pool was successfully cleared. False if there was an error.
141
     */
142
    public function clear()
143
    {
144
        return apcu_clear_cache();
145
    }
146
147
    // ------------------------------------------------------------------------
148
149
    /**
150
     * ItemPool::deleteItem
151
     *
152
     * Removes the item from the pool.
153
     *
154
     * @param string $key
155
     *   The key to delete.
156
     *
157
     * @throws InvalidArgumentException
158
     *   If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
159
     *   MUST be thrown.
160
     *
161
     * @return bool
162
     *   True if the item was successfully removed. False if there was an error.
163
     */
164
    public function deleteItem($key)
165
    {
166
        if ( ! is_string($key)) {
0 ignored issues
show
introduced by
The condition is_string($key) is always true.
Loading history...
167
            throw new InvalidArgumentException('E_INVALID_ARGUMENT_STRING_CACHE_EXCEPTION');
168
        }
169
170
        return apcu_delete($this->prefixKey . $key);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_delete($this->prefixKey . $key) also could return the type string[] which is incompatible with the documented return type boolean.
Loading history...
171
    }
172
173
    // ------------------------------------------------------------------------
174
175
    /**
176
     * ItemPool::save
177
     *
178
     * Persists a cache item immediately.
179
     *
180
     * @param CacheItemInterface $item
181
     *   The cache item to save.
182
     *
183
     * @return bool
184
     *   True if the item was successfully persisted. False if there was an error.
185
     */
186
    public function save(CacheItemInterface $item)
187
    {
188
        $metadata = $item->getMetadata();
189
        $metadata[ 'data' ] = $item->get();
190
191
        return apcu_store($this->prefixKey . $item->getKey(), $metadata, $metadata[ 'ttl' ]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apcu_store($this-...data, $metadata['ttl']) also could return the type array which is incompatible with the documented return type boolean.
Loading history...
192
    }
193
}