Passed
Pull Request — master (#770)
by Georges
02:08
created

Psr16Adapter::setMultiple()   A

Complexity

Conditions 6
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 9
nop 2
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *
5
 * This file is part of phpFastCache.
6
 *
7
 * @license MIT License (MIT)
8
 *
9
 * For full copyright and license information, please see the docs/CREDITS.txt file.
10
 *
11
 * @author Khoa Bui (khoaofgod)  <[email protected]> https://www.phpfastcache.com
12
 * @author Georges.L (Geolim4)  <[email protected]>
13
 *
14
 */
15
declare(strict_types=1);
16
17
namespace Phpfastcache\Helper;
18
19
use DateInterval;
20
use DateTime;
21
use Phpfastcache\CacheManager;
22
use Phpfastcache\Config\ConfigurationOptionInterface;
23
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
24
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
25
use Phpfastcache\Exceptions\{PhpfastcacheDriverCheckException,
26
    PhpfastcacheInvalidArgumentException,
27
    PhpfastcacheLogicException,
28
    PhpfastcacheRootException,
29
    PhpfastcacheSimpleCacheException};
30
use Psr\SimpleCache\CacheInterface;
31
use Traversable;
32
33
/**
34
 * Class Psr16Adapter
35
 * @package phpFastCache\Helper
36
 */
37
class Psr16Adapter implements CacheInterface
38
{
39
    /**
40
     * @var ExtendedCacheItemPoolInterface
41
     */
42
    protected $internalCacheInstance;
43
44
    /**
45
     * Psr16Adapter constructor.
46
     * @param $driver
47
     * @param null|ConfigurationOptionInterface $config
48
     * @throws PhpfastcacheDriverCheckException
49
     * @throws PhpfastcacheInvalidArgumentException
50
     * @throws PhpfastcacheLogicException
51
     * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverException
52
     * @throws \Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException
53
     * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException
54
     * @throws \ReflectionException
55
     */
56
    public function __construct($driver, ConfigurationOptionInterface $config = null)
57
    {
58
        if ($driver instanceof ExtendedCacheItemPoolInterface) {
59
            if ($config !== null) {
60
                throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter.");
61
            }
62
            $this->internalCacheInstance = $driver;
63
        } else {
64
            $this->internalCacheInstance = CacheManager::getInstance($driver, $config);
65
        }
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param mixed|null $default
71
     * @return mixed|null
72
     * @throws PhpfastcacheSimpleCacheException
73
     * @throws \Psr\Cache\InvalidArgumentException
74
     */
75
    public function get($key, $default = null)
76
    {
77
        try {
78
            $cacheItem = $this->internalCacheInstance->getItem($key);
79
            if (!$cacheItem->isExpired() && $cacheItem->get() !== null) {
80
                return $cacheItem->get();
81
            }
82
83
            return $default;
84
        } catch (PhpfastcacheInvalidArgumentException $e) {
85
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
86
        }
87
    }
88
89
    /**
90
     * @param string $key
91
     * @param mixed $value
92
     * @param null|int|DateInterval $ttl
93
     * @return bool
94
     * @throws PhpfastcacheSimpleCacheException
95
     * @throws \Psr\Cache\InvalidArgumentException
96
     */
97
    public function set($key, $value, $ttl = null): bool
98
    {
99
        try {
100
            $cacheItem = $this->internalCacheInstance
101
                ->getItem($key)
102
                ->set($value);
103
            if (\is_int($ttl) && $ttl <= 0) {
104
                $cacheItem->expiresAt((new DateTime('@0')));
105
            } elseif ($ttl !== null) {
106
                $cacheItem->expiresAfter($ttl);
107
            }
108
            return $this->internalCacheInstance->save($cacheItem);
109
        } catch (PhpfastcacheInvalidArgumentException $e) {
110
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
111
        }
112
    }
113
114
    /**
115
     * @param string $key
116
     * @return bool
117
     * @throws PhpfastcacheSimpleCacheException
118
     */
119
    public function delete($key): bool
120
    {
121
        try {
122
            return $this->internalCacheInstance->deleteItem($key);
123
        } catch (PhpfastcacheInvalidArgumentException $e) {
124
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
125
        }
126
    }
127
128
    /**
129
     * @return bool
130
     * @throws PhpfastcacheSimpleCacheException
131
     */
132
    public function clear(): bool
133
    {
134
        try {
135
            return $this->internalCacheInstance->clear();
136
        } catch (PhpfastcacheRootException $e) {
137
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
138
        }
139
    }
140
141
    /**
142
     * @param iterable $keys
143
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
144
     * @return ExtendedCacheItemInterface[]|iterable
145
     * @throws PhpfastcacheSimpleCacheException
146
     * @throws \Psr\Cache\InvalidArgumentException
147
     */
148
    public function getMultiple($keys, $default = null)
149
    {
150
        if ($keys instanceof Traversable) {
151
            $keys = \iterator_to_array($keys);
152
        }
153
        try {
154
            return \array_map(
155
                static function (ExtendedCacheItemInterface $item) {
156
                    return $item->get();
157
                },
158
                $this->internalCacheInstance->getItems($keys)
0 ignored issues
show
Bug introduced by
It seems like $keys can also be of type iterable; however, parameter $keys of Phpfastcache\Core\Pool\E...olInterface::getItems() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

158
                $this->internalCacheInstance->getItems(/** @scrutinizer ignore-type */ $keys)
Loading history...
159
            );
160
        } catch (PhpfastcacheInvalidArgumentException $e) {
161
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
162
        }
163
    }
164
165
    /**
166
     * @param string[] $values
167
     * @param null|int|DateInterval $ttl
168
     * @return bool
169
     * @throws PhpfastcacheSimpleCacheException
170
     * @throws \Psr\Cache\InvalidArgumentException
171
     */
172
    public function setMultiple($values, $ttl = null): bool
173
    {
174
        try {
175
            foreach ($values as $key => $value) {
176
                $cacheItem = $this->internalCacheInstance->getItem($key)->set($value);
177
178
                if (\is_int($ttl) && $ttl <= 0) {
179
                    $cacheItem->expiresAt((new DateTime('@0')));
180
                } elseif ($ttl !== null) {
181
                    $cacheItem->expiresAfter($ttl);
182
                }
183
                $this->internalCacheInstance->saveDeferred($cacheItem);
184
                unset($cacheItem);
185
            }
186
            return $this->internalCacheInstance->commit();
187
        } catch (PhpfastcacheInvalidArgumentException $e) {
188
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
189
        }
190
    }
191
192
    /**
193
     * @param iterable|array $keys
194
     * @return bool
195
     * @throws PhpfastcacheSimpleCacheException
196
     * @throws \Psr\Cache\InvalidArgumentException
197
     */
198
    public function deleteMultiple($keys): bool
199
    {
200
        try {
201
            if ($keys instanceof Traversable) {
202
                return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys));
203
            } elseif (\is_array($keys)) {
204
                return $this->internalCacheInstance->deleteItems($keys);
205
            } else {
206
                throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.');
207
            }
208
        } catch (PhpfastcacheInvalidArgumentException $e) {
209
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
210
        }
211
    }
212
213
    /**
214
     * @param string $key
215
     * @return bool
216
     * @throws PhpfastcacheSimpleCacheException
217
     */
218
    public function has($key): bool
219
    {
220
        try {
221
            $cacheItem = $this->internalCacheInstance->getItem($key);
222
            return $cacheItem->isHit() && !$cacheItem->isExpired();
223
        } catch (PhpfastcacheInvalidArgumentException $e) {
224
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
225
        }
226
    }
227
228
    /**
229
     * Extra methods that are not part of
230
     * psr16 specifications
231
     */
232
233
    /**
234
     * @return ExtendedCacheItemPoolInterface
235
     */
236
    public function getInternalCacheInstance(): ExtendedCacheItemPoolInterface
237
    {
238
        return $this->internalCacheInstance;
239
    }
240
}
241