Psr16Adapter::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

150
                $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

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