Psr16Cache   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Test Coverage

Coverage 18.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 156
ccs 5
cts 27
cp 0.1852
rs 10
wmc 12

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getMultiple() 0 9 2
A delete() 0 3 1
A __construct() 0 5 1
A set() 0 3 1
A has() 0 3 1
A setMultiple() 0 5 2
A computeTtl() 0 3 1
A clear() 0 3 1
A deleteMultiple() 0 3 1
A get() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Laravel GitLab.
7
 *
8
 * (c) Graham Campbell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace GrahamCampbell\GitLab\Http;
15
16
use Illuminate\Contracts\Cache\Repository;
17
use Psr\SimpleCache\CacheInterface;
18
19
/**
20
 * This is the PSR-16 cache class.
21
 *
22
 * The purpose of this class is to work around bugs present in Laravel
23
 * 5.5.0-5.5.47, 5.6.x and 5.7.x, and 5.8.0-5.8.32, and also to enforce a
24
 * maximum TTL on cache items.
25
 *
26
 * @author Graham Campbell <[email protected]>
27
 */
28
class Psr16Cache implements CacheInterface
29
{
30
    /**
31
     * The underlying cache instance.
32
     *
33
     * @var \Illuminate\Contracts\Cache\Repository
34
     */
35
    protected $cache;
36
37
    /**
38
     * Create a PSR-16 cache instance.
39
     *
40
     * @param \Illuminate\Contracts\Cache\Repository $cache
41
     * @param int                                    $min
42
     * @param int                                    $max
43
     *
44
     * @return void
45
     */
46 24
    public function __construct(Repository $cache, int $min, int $max)
47
    {
48 24
        $this->cache = $cache;
49 24
        $this->min = $min;
0 ignored issues
show
Bug Best Practice introduced by Graham Campbell
The property min does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50 24
        $this->max = $max;
0 ignored issues
show
Bug Best Practice introduced by Graham Campbell
The property max does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
51 24
    }
52
53
    /**
54
     * Computes the TTL to use.
55
     *
56
     * @param null|int|\DateInterval $ttl
57
     *
58
     * @return int
59
     */
60
    protected function computeTtl($ttl)
61
    {
62
        return TtlHelper::computeTtl($this->min, $this->max, $ttl);
63
    }
64
65
    /**
66
     * Fetches a value from the cache.
67
     *
68
     * @param string $key
69
     * @param mixed  $default
70
     *
71
     * @throws \Psr\SimpleCache\InvalidArgumentException
72
     *
73
     * @return mixed
74
     */
75
    public function get($key, $default = null)
76
    {
77
        return $this->cache->get($key, $default);
78
    }
79
80
    /**
81
     * Persists data in the cache, uniquely referenced by a key.
82
     *
83
     * @param string                 $key
84
     * @param mixed                  $value
85
     * @param null|int|\DateInterval $ttl
86
     *
87
     * @throws \Psr\SimpleCache\InvalidArgumentException
88
     *
89
     * @return bool
90
     */
91
    public function set($key, $value, $ttl = null)
92
    {
93
        return $this->cache->put($key, $value, $this->computeTtl($ttl));
94
    }
95
96
    /**
97
     * Delete an item from the cache by its unique key.
98
     *
99
     * @param string $key
100
     *
101
     * @throws \Psr\SimpleCache\InvalidArgumentException
102
     *
103
     * @return bool
104
     */
105
    public function delete($key)
106
    {
107
        return $this->cache->forget($key);
108
    }
109
110
    /**
111
     * Wipes clean the entire cache's keys.
112
     *
113
     * @return bool
114
     */
115
    public function clear()
116
    {
117
        return $this->cache->clear();
118
    }
119
120
    /**
121
     * Obtains multiple cache items by their unique keys.
122
     *
123
     * @param iterable $keys
124
     * @param mixed    $default
125
     *
126
     * @throws \Psr\SimpleCache\InvalidArgumentException
127
     *
128
     * @return iterable
129
     */
130
    public function getMultiple($keys, $default = null)
131
    {
132
        $defaults = [];
133
134
        foreach ($keys as $key) {
135
            $defaults[$key] = $default;
136
        }
137
138
        return $this->cache->many($defaults);
139
    }
140
141
    /**
142
     * Persists a set of key => value pairs in the cache.
143
     *
144
     * @param iterable               $values
145
     * @param null|int|\DateInterval $ttl
146
     *
147
     * @throws \Psr\SimpleCache\InvalidArgumentException
148
     *
149
     * @return bool
150
     */
151
    public function setMultiple($values, $ttl = null)
152
    {
153
        $values = is_array($values) ? $values : iterator_to_array($values);
154
155
        return $this->cache->putMany($values, $this->computeTtl($ttl));
156
    }
157
158
    /**
159
     * Deletes multiple cache items in a single operation.
160
     *
161
     * @param iterable $keys
162
     *
163
     * @throws \Psr\SimpleCache\InvalidArgumentException
164
     *
165
     * @return bool
166
     */
167
    public function deleteMultiple($keys)
168
    {
169
        return $this->cache->deleteMultiple($keys);
170
    }
171
172
    /**
173
     * Determines whether an item is present in the cache.
174
     *
175
     * @param string $key
176
     *
177
     * @throws \Psr\SimpleCache\InvalidArgumentException
178
     *
179
     * @return bool
180
     */
181
    public function has($key)
182
    {
183
        return $this->cache->has($key);
184
    }
185
}
186