Completed
Push — master ( b2545f...e44389 )
by Divine Niiquaye
02:25
created

SimpleCache::clear()   A

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 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of BiuradPHP opensource projects.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\Cache;
19
20
use BadMethodCallException;
21
use Biurad\Cache\Exceptions\InvalidArgumentException;
22
use DateInterval;
23
use Doctrine\Common\Cache\Cache as DoctrineCache;
24
use Doctrine\Common\Cache\CacheProvider;
25
use Doctrine\Common\Cache\MultiOperationCache;
26
use Psr\SimpleCache\CacheInterface;
27
use Throwable;
28
use Traversable;
29
30
class SimpleCache implements CacheInterface
31
{
32
    /** @var DoctrineCache */
33
    protected $instance;
34
35
    /**
36
     * PSR-16 Cache Constructor.
37
     *
38
     * @param DoctrineCache $instance
39
     */
40
    public function __construct(DoctrineCache $instance)
41
    {
42
        $this->instance = $instance;
43
    }
44
45
    public function __sleep(): void
46
    {
47
        throw new BadMethodCallException('Cannot serialize ' . __CLASS__);
48
    }
49
50
    public function __wakeup(): void
51
    {
52
        throw new BadMethodCallException('Cannot unserialize ' . __CLASS__);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function has($key): bool
59
    {
60
        return $this->instance->contains($key);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function get($key, $default = null)
67
    {
68
        if ($this->has($key)) {
69
            return $this->instance->fetch($key);
70
        }
71
72
        return $default;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function set($key, $value, $ttl = null): bool
79
    {
80
        if ($ttl instanceof DateInterval) {
81
            throw new InvalidArgumentException('Using \'DataInterval\' will be implemented in v1.0');
82
        }
83
84
        return $this->instance->save($key, $value, $ttl ?? 0);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function delete($key): bool
91
    {
92
        return $this->instance->delete($key);
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function clear(): bool
99
    {
100
        $driver = $this->instance;
101
102
        if (!$driver instanceof CacheProvider) {
103
            return false;
104
        }
105
106
        try {
107
            return $driver->flushAll();
108
        } catch (Throwable $e) {
109
            return $driver->deleteAll();
110
        }
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getMultiple($keys, $default = null)
117
    {
118
        if ($keys instanceof Traversable) {
119
            $keys = \iterator_to_array($keys, false);
120
        }
121
122
        if (!\is_array($keys)) {
123
            throw new InvalidArgumentException('Cache keys must be array or Traversable.');
124
        }
125
126
        if ($this->instance instanceof MultiOperationCache) {
127
            return $this->instance->fetchMultiple($keys);
128
        }
129
130
        foreach ($keys as $key) {
131
            yield from [$key => $this->get($key, $default)];
132
        }
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function setMultiple($values, $ttl = null): bool
139
    {
140
        if ($ttl instanceof DateInterval) {
141
            throw new InvalidArgumentException('Using \'DataInterval\' will be implemented in v1.0');
142
        }
143
144
        if ($values instanceof Traversable) {
145
            $values = \iterator_to_array($values);
146
        }
147
148
        if ($this->instance instanceof MultiOperationCache) {
149
            return $this->instance->saveMultiple($values, $ttl ?? 0);
150
        }
151
152
        foreach ($values as $key => $value) {
153
            $this->set($key, $value, $ttl);
154
        }
155
156
        return true;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function deleteMultiple($keys): bool
163
    {
164
        if ($keys instanceof Traversable) {
165
            $keys = \iterator_to_array($keys, false);
166
        }
167
168
        if ($this->instance instanceof MultiOperationCache) {
169
            return $this->instance->deleteMultiple((array) $keys);
170
        }
171
172
        foreach ($keys as $key) {
173
            if ($this->delete($key)) {
174
                continue;
175
            }
176
177
            return false;
178
        }
179
180
        return true;
181
    }
182
}
183