Passed
Push — master ( b0ec1a...8763d4 )
by Divine Niiquaye
01:43
created

src/SimpleCache.php (2 issues)

Labels
Severity
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 BiuradPHP\Cache;
19
20
use BadMethodCallException;
21
use BiuradPHP\Cache\Exceptions\InvalidArgumentException;
22
use DateInterval;
23
use Doctrine\Common\Cache\Cache as DoctrineCache;
24
use Doctrine\Common\Cache\ClearableCache;
25
use Doctrine\Common\Cache\FlushableCache;
26
use Doctrine\Common\Cache\MultiOperationCache;
27
use Psr\SimpleCache\CacheInterface;
28
use Throwable;
29
use Traversable;
30
31
class SimpleCache implements CacheInterface
32
{
33
    /** @var DoctrineCache */
34
    protected $instance;
35
36
    /**
37
     * PSR-16 Cache Constructor.
38
     *
39
     * @param DoctrineCache $instance
40
     */
41
    public function __construct(DoctrineCache $instance)
42
    {
43
        $this->instance = $instance;
44
    }
45
46
    public function __sleep(): void
47
    {
48
        throw new BadMethodCallException('Cannot serialize ' . __CLASS__);
49
    }
50
51
    public function __wakeup(): void
52
    {
53
        throw new BadMethodCallException('Cannot unserialize ' . __CLASS__);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function has($key): bool
60
    {
61
        return $this->instance->contains($key);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function get($key, $default = null)
68
    {
69
        if ($this->has($key)) {
70
            return $this->instance->fetch($key);
71
        }
72
73
        return $default;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function set($key, $value, $ttl = null): bool
80
    {
81
        if ($ttl instanceof DateInterval) {
82
            throw new InvalidArgumentException('Using \'DataInterval\' will be implemented in v1.0');
83
        }
84
85
        return $this->instance->save($key, $value, $ttl ?: 0);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function delete($key): bool
92
    {
93
        return $this->instance->delete($key);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function clear(): bool
100
    {
101
        $driver = $this->instance;
102
103
        if (!($driver instanceof FlushableCache || $driver instanceof ClearableCache)) {
104
            return false;
105
        }
106
107
        try {
108
            return $driver->deleteAll();
0 ignored issues
show
The method deleteAll() does not exist on Doctrine\Common\Cache\Cache. Did you maybe mean delete()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            return $driver->/** @scrutinizer ignore-call */ deleteAll();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        } catch (Throwable $e) {
110
            return $driver->flushAll();
0 ignored issues
show
The method flushAll() does not exist on Doctrine\Common\Cache\Cache. It seems like you code against a sub-type of said class. However, the method does not exist in BiuradPHP\Cache\Interfaces\CacheAdapterInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

110
            return $driver->/** @scrutinizer ignore-call */ flushAll();
Loading history...
111
        }
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function getMultiple($keys, $default = null)
118
    {
119
        if ($keys instanceof Traversable) {
120
            $keys = \iterator_to_array($keys, false);
121
        } elseif (!\is_array($keys)) {
122
            throw new InvalidArgumentException('Cache keys must be array or Traversable.');
123
        }
124
125
        if ($this->instance instanceof MultiOperationCache) {
126
            return $this->instance->fetchMultiple((array) $keys);
127
        }
128
129
        foreach ($keys as $key) {
130
            yield from [$key => $this->get($key, $default)];
131
        }
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function setMultiple($values, $ttl = null): bool
138
    {
139
        if ($values instanceof Traversable) {
140
            $values = \iterator_to_array($values);
141
        }
142
143
        if ($this->instance instanceof MultiOperationCache) {
144
            return $this->instance->saveMultiple((array) $values, $ttl);
145
        }
146
147
        foreach ($values as $key => $value) {
148
            $this->set($key, $value, $ttl);
149
        }
150
151
        return true;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function deleteMultiple($keys): bool
158
    {
159
        if ($keys instanceof Traversable) {
160
            $keys = \iterator_to_array($keys, false);
161
        }
162
163
        if ($this->instance instanceof MultiOperationCache) {
164
            return $this->instance->deleteMultiple((array) $keys);
165
        }
166
167
        foreach ($keys as $key) {
168
            if ($this->delete($key)) {
169
                continue;
170
            }
171
172
            return false;
173
        }
174
175
        return true;
176
    }
177
}
178