Passed
Push — v9 ( 473b89...c87345 )
by Georges
02:33
created

Psr16Adapter::getInternalCacheInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 and LICENCE files.
10
 *
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Helper;
17
18
use DateInterval;
19
use DateTime;
20
use Phpfastcache\CacheManager;
21
use Phpfastcache\Config\ConfigurationOptionInterface;
22
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
23
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
24
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException;
25
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
26
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException;
27
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
28
use Phpfastcache\Exceptions\PhpfastcacheLogicException;
29
use Phpfastcache\Exceptions\PhpfastcacheRootException;
30
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException;
31
use Psr\Cache\InvalidArgumentException;
32
use Psr\SimpleCache\CacheInterface;
33
use Traversable;
34
35
class Psr16Adapter implements CacheInterface
36
{
37
    /**
38
     * @var ExtendedCacheItemPoolInterface
39
     */
40
    protected ExtendedCacheItemPoolInterface $internalCacheInstance;
41
42
    /**
43
     * Psr16Adapter constructor.
44
     * @param $driver
45
     * @param null|ConfigurationOptionInterface $config
46
     * @throws PhpfastcacheDriverCheckException
47
     * @throws PhpfastcacheLogicException
48
     * @throws PhpfastcacheDriverException
49
     * @throws PhpfastcacheDriverNotFoundException
50
     */
51
    public function __construct($driver, ConfigurationOptionInterface $config = null)
52
    {
53
        if ($driver instanceof ExtendedCacheItemPoolInterface) {
54
            if ($config !== null) {
55
                throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter.");
56
            }
57
            $this->internalCacheInstance = $driver;
58
        } else {
59
            $this->internalCacheInstance = CacheManager::getInstance($driver, $config);
60
        }
61
    }
62
63
    /**
64
     * @param string $key
65
     * @param mixed|null $default
66
     * @return mixed|null
67
     * @throws PhpfastcacheSimpleCacheException
68
     */
69
    public function get($key, $default = null)
70
    {
71
        try {
72
            $cacheItem = $this->internalCacheInstance->getItem($key);
73
            if (!$cacheItem->isExpired() && $cacheItem->get() !== null) {
74
                return $cacheItem->get();
75
            }
76
77
            return $default;
78
        } catch (PhpfastcacheInvalidArgumentException $e) {
79
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
80
        }
81
    }
82
83
    /**
84
     * @param string $key
85
     * @param mixed $value
86
     * @param null|int|DateInterval $ttl
87
     * @return bool
88
     * @throws PhpfastcacheSimpleCacheException
89
     */
90
    public function set($key, $value, $ttl = null): bool
91
    {
92
        try {
93
            $cacheItem = $this->internalCacheInstance
94
                ->getItem($key)
95
                ->set($value);
96
            if (\is_int($ttl) && $ttl <= 0) {
97
                $cacheItem->expiresAt((new DateTime('@0')));
98
            } elseif ($ttl !== null) {
99
                $cacheItem->expiresAfter($ttl);
100
            }
101
            return $this->internalCacheInstance->save($cacheItem);
102
        } catch (PhpfastcacheInvalidArgumentException $e) {
103
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
104
        }
105
    }
106
107
    /**
108
     * @param string $key
109
     * @return bool
110
     * @throws PhpfastcacheSimpleCacheException
111
     * @throws InvalidArgumentException
112
     */
113
    public function delete($key): bool
114
    {
115
        try {
116
            return $this->internalCacheInstance->deleteItem($key);
117
        } catch (PhpfastcacheInvalidArgumentException $e) {
118
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
119
        }
120
    }
121
122
    /**
123
     * @return bool
124
     * @throws PhpfastcacheSimpleCacheException
125
     */
126
    public function clear(): bool
127
    {
128
        try {
129
            return $this->internalCacheInstance->clear();
130
        } catch (PhpfastcacheRootException $e) {
131
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
132
        }
133
    }
134
135
    /**
136
     * @param iterable $keys
137
     * @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...
138
     * @return ExtendedCacheItemInterface[]|iterable
139
     * @throws PhpfastcacheSimpleCacheException
140
     */
141
    public function getMultiple($keys, $default = null)
142
    {
143
        if ($keys instanceof Traversable) {
144
            $keys = \iterator_to_array($keys);
145
        }
146
        try {
147
            return \array_map(
148
                static fn (ExtendedCacheItemInterface $item) => $item->isHit() ? $item->get() : $default,
149
                $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

149
                $this->internalCacheInstance->getItems(/** @scrutinizer ignore-type */ $keys)
Loading history...
Bug introduced by
$this->internalCacheInstance->getItems($keys) of type iterable is incompatible with the type array expected by parameter $array of array_map(). ( Ignorable by Annotation )

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

149
                /** @scrutinizer ignore-type */ $this->internalCacheInstance->getItems($keys)
Loading history...
150
            );
151
        } catch (PhpfastcacheInvalidArgumentException $e) {
152
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
153
        }
154
    }
155
156
    /**
157
     * @param string[] $values
158
     * @param null|int|DateInterval $ttl
159
     * @return bool
160
     * @throws PhpfastcacheSimpleCacheException
161
     */
162
    public function setMultiple($values, $ttl = null): bool
163
    {
164
        try {
165
            foreach ($values as $key => $value) {
166
                $cacheItem = $this->internalCacheInstance->getItem($key)->set($value);
167
168
                if (\is_int($ttl) && $ttl <= 0) {
169
                    $cacheItem->expiresAt((new DateTime('@0')));
170
                } elseif ($ttl !== null) {
171
                    $cacheItem->expiresAfter($ttl);
172
                }
173
                $this->internalCacheInstance->saveDeferred($cacheItem);
174
                unset($cacheItem);
175
            }
176
            return $this->internalCacheInstance->commit();
177
        } catch (PhpfastcacheInvalidArgumentException $e) {
178
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
179
        }
180
    }
181
182
    /**
183
     * @param iterable|array $keys
184
     * @return bool
185
     * @throws PhpfastcacheSimpleCacheException
186
     * @throws InvalidArgumentException
187
     */
188
    public function deleteMultiple($keys): bool
189
    {
190
        try {
191
            if ($keys instanceof Traversable) {
192
                return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys));
193
            }
194
195
            if (\is_array($keys)) {
196
                return $this->internalCacheInstance->deleteItems($keys);
197
            }
198
199
            throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.');
200
        } catch (PhpfastcacheInvalidArgumentException $e) {
201
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
202
        }
203
    }
204
205
    /**
206
     * @param string $key
207
     * @return bool
208
     * @throws PhpfastcacheSimpleCacheException
209
     */
210
    public function has($key): bool
211
    {
212
        try {
213
            $cacheItem = $this->internalCacheInstance->getItem($key);
214
            return $cacheItem->isHit() && !$cacheItem->isExpired();
215
        } catch (PhpfastcacheInvalidArgumentException $e) {
216
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e);
217
        }
218
    }
219
}
220