Chain::set()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cache package.
4
 *
5
 * Copyright (c) Daniel González
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author Daniel González <[email protected]>
11
 * @author Arnold Daniels <[email protected]>
12
 */
13
14
namespace Desarrolla2\Cache;
15
16
use Desarrolla2\Cache\Packer\NopPacker;
17
use Desarrolla2\Cache\Packer\PackerInterface;
18
use Desarrolla2\Cache\Exception\InvalidArgumentException;
19
20
/**
21
 * Use multiple cache adapters.
22
 */
23
class Chain extends AbstractCache
24
{
25
    /**
26
     * @var CacheInterface[]
27
     */
28
    protected $adapters;
29
30
    /**
31
     * Create the default packer for this cache implementation
32
     *
33
     * @return PackerInterface
34
     */
35
    protected static function createDefaultPacker(): PackerInterface
36
    {
37
        return new NopPacker();
38
    }
39
40
41
    /**
42
     * Chain constructor.
43
     *
44
     * @param CacheInterface[] $adapters  Fastest to slowest
45
     */
46 211
    public function __construct(array $adapters)
47
    {
48 211
        foreach ($adapters as $adapter) {
49 211
            if (!$adapter instanceof CacheInterface) {
50
                throw new InvalidArgumentException("All adapters should be a cache implementation");
51
            }
52
        }
53
54 211
        $this->adapters = $adapters;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 59
    public function set($key, $value, $ttl = null)
61
    {
62 59
        $success = true;
63
64 59
        foreach ($this->adapters as $adapter) {
65 59
            $success = $adapter->set($key, $value, $ttl) && $success;
66
        }
67
68 31
        return $success;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 43
    public function setMultiple($values, $ttl = null)
75
    {
76 43
        $success = true;
77
78 43
        foreach ($this->adapters as $adapter) {
79 43
            $success = $adapter->setMultiple($values, $ttl) && $success;
80
        }
81
82 15
        return $success;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 55
    public function get($key, $default = null)
89
    {
90 55
        foreach ($this->adapters as $adapter) {
91 55
            $result = $adapter->get($key); // Not using $default as we want to get null if the adapter doesn't have it
92
93 37
            if (isset($result)) {
94 27
                return $result;
95
            }
96
        }
97
98 14
        return $default;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 32
    public function getMultiple($keys, $default = null)
105
    {
106 32
        $this->assertIterable($keys, 'keys are not iterable');
107
108 31
        $missing = [];
109 31
        $values = [];
110
111 31
        foreach ($keys as $key) {
112 31
            $this->assertKey($key);
113
114 31
            $missing[] = $key;
115 31
            $values[$key] = $default;
116
        }
117
118 13
        foreach ($this->adapters as $adapter) {
119 13
            if (empty($missing)) {
120 1
                break;
121
            }
122
123 13
            $found = [];
124 13
            foreach ($adapter->getMultiple($missing) as $key => $value) {
125 13
                if (isset($value)) {
126 12
                    $found[$key] = $value;
127
                }
128
            }
129
130 13
            $values = array_merge($values, $found);
131 13
            $missing = array_values(array_diff($missing, array_keys($found)));
132
        }
133
134 13
        return $values;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 25
    public function has($key)
141
    {
142 25
        foreach ($this->adapters as $adapter) {
143 25
            if ($adapter->has($key)) {
144 5
                return true;
145
            }
146
        }
147
148 4
        return false;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 21
    public function delete($key)
155
    {
156 21
        $success = true;
157
158 21
        foreach ($this->adapters as $adapter) {
159 21
            $success = $adapter->delete($key) && $success;
160
        }
161
162 3
        return $success;
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 22
    public function deleteMultiple($keys)
169
    {
170 22
        $success = true;
171
172 22
        foreach ($this->adapters as $adapter) {
173 22
            $success = $adapter->deleteMultiple($keys) && $success;
174
        }
175
176 3
        return $success;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182 211
    public function clear()
183
    {
184 211
        $success = true;
185
186 211
        foreach ($this->adapters as $adapter) {
187 211
            $success = $adapter->clear() && $success;
188
        }
189
190 211
        return $success;
191
    }
192
}
193