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::save()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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