Passed
Push — v7 ( 38cfdc...9a759d )
by Georges
01:33
created

Psr16Adapter::setMultiple()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 9
nop 2
dl 0
loc 17
rs 8.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Helper;
17
18
use Phpfastcache\CacheManager;
19
use Phpfastcache\Core\Item\ExtendedCacheItemInterface;
20
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
21
use Phpfastcache\Exceptions\{
22
  PhpfastcacheDriverCheckException, PhpfastcacheInvalidArgumentException, PhpfastcacheLogicException, PhpfastcacheRootException, PhpfastcacheSimpleCacheException
23
};
24
use Psr\SimpleCache\CacheInterface;
25
26
/**
27
 * Class Psr16Adapter
28
 * @package phpFastCache\Helper
29
 */
30
class Psr16Adapter implements CacheInterface
31
{
32
    /**
33
     * @var ExtendedCacheItemPoolInterface
34
     */
35
    protected $internalCacheInstance;
36
37
    /**
38
     * Psr16Adapter constructor.
39
     * @param string|ExtendedCacheItemPoolInterface $driver
40
     * @param array|\Phpfastcache\Config\ConfigurationOption|null $config
41
     * @throws PhpfastcacheDriverCheckException
42
     * @throws PhpfastcacheLogicException
43
     */
44
    public function __construct($driver, $config = null)
45
    {
46
        if($driver instanceof ExtendedCacheItemPoolInterface){
47
            if($config !== null){
48
                throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter.");
49
            }
50
            $this->internalCacheInstance = $driver;
51
        }else{
52
            $this->internalCacheInstance = CacheManager::getInstance($driver, $config);
53
        }
54
    }
55
56
    /**
57
     * @param string $key
58
     * @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...
59
     * @return mixed|null
60
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
61
     */
62
    public function get($key, $default = null)
63
    {
64
        try {
65
            $cacheItem = $this->internalCacheInstance->getItem($key);
66
            if (!$cacheItem->isExpired() && $cacheItem->get() !== null) {
67
                return $cacheItem->get();
68
            }
69
70
            return $default;
71
        } catch (PhpfastcacheInvalidArgumentException $e) {
72
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
73
        }
74
    }
75
76
    /**
77
     * @param string $key
78
     * @param mixed $value
79
     * @param null $ttl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ttl is correct as it would always require null to be passed?
Loading history...
80
     * @return bool
81
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
82
     */
83
    public function set($key, $value, $ttl = null): bool
84
    {
85
        try {
86
            $cacheItem = $this->internalCacheInstance
87
              ->getItem($key)
88
              ->set($value);
89
            if (\is_int($ttl) && $ttl <= 0) {
90
                $cacheItem->expiresAt((new \DateTime('@0')));
91
            } elseif (\is_int($ttl) || $ttl instanceof \DateInterval) {
92
                $cacheItem->expiresAfter($ttl);
93
            }
94
            return $this->internalCacheInstance->save($cacheItem);
95
        } catch (PhpfastcacheInvalidArgumentException $e) {
96
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
97
        }
98
    }
99
100
    /**
101
     * @param string $key
102
     * @return bool
103
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
104
     */
105
    public function delete($key): bool
106
    {
107
        try {
108
            return $this->internalCacheInstance->deleteItem($key);
109
        } catch (PhpfastcacheInvalidArgumentException $e) {
110
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
111
        }
112
    }
113
114
    /**
115
     * @return bool
116
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
117
     */
118
    public function clear(): bool
119
    {
120
        try {
121
            return $this->internalCacheInstance->clear();
122
        } catch (PhpfastcacheRootException $e) {
123
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
124
        }
125
    }
126
127
    /**
128
     * @param string[] $keys
129
     * @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...
130
     * @return \iterable
131
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
132
     */
133
    public function getMultiple($keys, $default = null)
134
    {
135
        try {
136
            return array_map(function (ExtendedCacheItemInterface $item) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_map(functio...tance->getItems($keys)) returns the type array which is incompatible with the documented return type iterable.
Loading history...
137
                return $item->get();
138
            }, $this->internalCacheInstance->getItems($keys));
139
        } catch (PhpfastcacheInvalidArgumentException $e) {
140
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
141
        }
142
    }
143
144
    /**
145
     * @param string[] $values
146
     * @param null|int|\DateInterval $ttl
147
     * @return bool
148
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
149
     */
150
    public function setMultiple($values, $ttl = null): bool
151
    {
152
        try {
153
            foreach ($values as $key => $value) {
154
                $cacheItem = $this->internalCacheInstance->getItem($key)->set($value);
155
156
                if (\is_int($ttl) && $ttl <= 0) {
157
                    $cacheItem->expiresAt((new \DateTime('@0')));
158
                } elseif (\is_int($ttl) || $ttl instanceof \DateInterval) {
159
                    $cacheItem->expiresAfter($ttl);
160
                }
161
                $this->internalCacheInstance->saveDeferred($cacheItem);
162
                unset($cacheItem);
163
            }
164
            return $this->internalCacheInstance->commit();
165
        } catch (PhpfastcacheInvalidArgumentException $e) {
166
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
167
        }
168
    }
169
170
    /**
171
     * @param string[] $keys
172
     * @return bool
173
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
174
     */
175
    public function deleteMultiple($keys): bool
176
    {
177
        try {
178
            return $this->internalCacheInstance->deleteItems($keys);
179
        } catch (PhpfastcacheInvalidArgumentException $e) {
180
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
181
        }
182
    }
183
184
    /**
185
     * @param string $key
186
     * @return bool
187
     * @throws \Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException
188
     */
189
    public function has($key): bool
190
    {
191
        try {
192
            $cacheItem = $this->internalCacheInstance->getItem($key);
193
            return $cacheItem->isHit() && !$cacheItem->isExpired();
194
        } catch (PhpfastcacheInvalidArgumentException $e) {
195
            throw new PhpfastcacheSimpleCacheException($e->getMessage(), null, $e);
196
        }
197
    }
198
199
    /**
200
     * Extra methods that are not part of
201
     * psr16 specifications
202
     */
203
204
    /**
205
     * @return \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface
206
     */
207
    public function getInternalCacheInstance(): ExtendedCacheItemPoolInterface
208
    {
209
        return $this->internalCacheInstance;
210
    }
211
}
212