Passed
Push — master ( 2c97eb...c298b6 )
by Petr
02:20
created

MultiCacheAdapter::getMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace kalanis\kw_cache_psr\Adapters\SimpleCache;
4
5
6
use DateInterval;
7
use kalanis\kw_cache\CacheException;
8
use kalanis\kw_cache\Interfaces\ICache;
9
use kalanis\kw_cache\Interfaces\IFormat;
10
use kalanis\kw_cache_psr\InvalidArgumentException;
11
use kalanis\kw_cache_psr\Traits\TCheckKey;
12
use Psr\SimpleCache\CacheInterface;
13
14
15
/**
16
 * Class MultiCacheAdapter
17
 * @package kalanis\kw_cache_psr\Adapters\SimpleCache
18
 * Cache adapter for PSR Cache Interface
19
 */
20
class MultiCacheAdapter implements CacheInterface
21
{
22
    use TCheckKey;
23
24
    protected ICache $baseCache;
25
    protected IFormat $format;
26
    /** @var array<string, ICache> */
27
    protected array $caches = [];
28
29 9
    public function __construct(ICache $cache, IFormat $format)
30
    {
31 9
        $this->baseCache = $cache;
32 9
        $this->format = $format;
33 9
    }
34
35
    /**
36
     * @param string $key
37
     * @param mixed $default
38
     * @throws InvalidArgumentException
39
     * @return mixed|null
40
     */
41 4
    public function get($key, $default = null)
42
    {
43
        try {
44 4
            $usedKey = $this->checkKey($key);
45 4
            if (isset($this->caches[$usedKey])) {
46 3
                return $this->format->unpack($this->caches[$usedKey]->get());
47
            }
48 2
            return $default;
49 1
        } catch (CacheException $ex) {
50 1
            return $default;
51
        }
52
    }
53
54
    /**
55
     * @param string $key
56
     * @param mixed $value
57
     * @param DateInterval|int|null $ttl
58
     * @throws InvalidArgumentException
59
     * @return bool
60
     */
61 5
    public function set($key, $value, $ttl = null): bool
62
    {
63
        try {
64 5
            $usedKey = $this->checkKey($key);
65 5
            if (!isset($this->caches[$usedKey])) {
66 5
                $cache = clone $this->baseCache;
67 5
                $cache->init([$usedKey]);
68
            } else {
69 1
                $cache = $this->caches[$usedKey];
70
            }
71 5
            $cache->set(strval($this->format->pack($value)));
72 4
            $this->caches[$usedKey] = $cache;
73 4
            return true;
74 1
        } catch (CacheException $ex) {
75 1
            return false;
76
        }
77
    }
78
79
    /**
80
     * @param string $key
81
     * @throws InvalidArgumentException
82
     * @return bool
83
     */
84 3
    public function delete($key): bool
85
    {
86 3
        $usedKey = $this->checkKey($key);
87 3
        if (isset($this->caches[$usedKey])) {
88 2
            unset($this->caches[$usedKey]);
89
        }
90 3
        return true;
91
    }
92
93 3
    public function clear(): bool
94
    {
95 3
        $this->caches = [];
96 3
        return true;
97
    }
98
99
    /**
100
     * @param iterable<string|int, string> $keys
101
     * @param mixed $default
102
     * @throws InvalidArgumentException
103
     * @return iterable<string, mixed>
104
     */
105 1
    public function getMultiple($keys, $default = null): iterable
106
    {
107 1
        $results = [];
108 1
        foreach ($keys as $key) {
109 1
            $results[$key] = $this->get($key, $default);
110
        }
111 1
        return $results;
112
    }
113
114
    /**
115
     * @param iterable<string, mixed> $values
116
     * @param null|int|DateInterval $ttl
117
     * @throws InvalidArgumentException
118
     * @return bool
119
     */
120 1
    public function setMultiple($values, $ttl = null): bool
121
    {
122 1
        $result = true;
123 1
        foreach ($values as $key => $value) {
124 1
            $result = $result && $this->set($key, $value, $ttl);
125
        }
126 1
        return $result;
127
    }
128
129
    /**
130
     * @param iterable<string|int, string> $keys
131
     * @throws InvalidArgumentException
132
     * @return bool
133
     */
134 1
    public function deleteMultiple($keys): bool
135
    {
136 1
        $result = true;
137 1
        foreach ($keys as $key) {
138 1
            $result = $result && $this->delete($key);
139
        }
140 1
        return $result;
141
    }
142
143 4
    public function has($key): bool
144
    {
145 4
        $usedKey = $this->checkKey($key);
146 4
        return isset($this->caches[$usedKey]);
147
    }
148
}
149