SoloCacheAdapter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 105
ccs 34
cts 34
cp 1
rs 10
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMultiple() 0 7 2
A has() 0 6 2
A __construct() 0 4 1
A setMultiple() 0 3 1
A deleteMultiple() 0 3 1
A set() 0 6 2
A get() 0 9 3
A clear() 0 7 2
A delete() 0 3 1
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 Psr\SimpleCache\CacheInterface;
11
12
13
/**
14
 * Class SoloCacheAdapter
15
 * @package kalanis\kw_cache_psr\Adapters\SimpleCache
16
 * Cache adapter for PSR Cache Interface
17
 * You probably need another one, not this one
18
 */
19
class SoloCacheAdapter implements CacheInterface
20
{
21
    protected ICache $cache;
22
    protected IFormat $format;
23
24 9
    public function __construct(ICache $cache, IFormat $format)
25
    {
26 9
        $this->cache = $cache;
27 9
        $this->format = $format;
28 9
    }
29
30
    /**
31
     * @param string $key
32
     * @param mixed $default
33
     * @return mixed|null
34
     */
35 4
    public function get($key, $default = null)
36
    {
37
        try {
38 4
            if ($this->cache->exists()) {
39 2
                return $this->format->unpack($this->cache->get());
40
            }
41 2
            return $default;
42 1
        } catch (CacheException $ex) {
43 1
            return $default;
44
        }
45
    }
46
47
    /**
48
     * @param string $key
49
     * @param mixed $value
50
     * @param DateInterval|int|null $ttl
51
     * @return bool
52
     */
53 4
    public function set($key, $value, $ttl = null): bool
54
    {
55
        try {
56 4
            return $this->cache->set(strval($this->format->pack($value)));
57 1
        } catch (CacheException $ex) {
58 1
            return false;
59
        }
60
    }
61
62
    /**
63
     * @param string $key
64
     * @return bool
65
     */
66 2
    public function delete($key): bool
67
    {
68 2
        return $this->clear();
69
    }
70
71 5
    public function clear(): bool
72
    {
73
        try {
74 5
            $this->cache->clear();
75 3
            return true;
76 2
        } catch (CacheException $ex) {
77 2
            return false;
78
        }
79
    }
80
81
    /**
82
     * @param iterable<string|int, string> $keys
83
     * @param mixed $default
84
     * @return iterable<string, mixed>
85
     */
86 1
    public function getMultiple($keys, $default = null): iterable
87
    {
88 1
        $results = [];
89 1
        foreach ($keys as $key) {
90 1
            $results[$key] = $this->get($key, $default);
91
        }
92 1
        return $results;
93
    }
94
95
    /**
96
     * @param iterable<string, mixed> $values
97
     * @param null|int|DateInterval $ttl
98
     * @return bool
99
     */
100 1
    public function setMultiple($values, $ttl = null): bool
101
    {
102 1
        return false;
103
    }
104
105
    /**
106
     * @param iterable<string|int, string> $keys
107
     * @return bool
108
     */
109 1
    public function deleteMultiple($keys): bool
110
    {
111 1
        return $this->clear();
112
    }
113
114
    /**
115
     * @param string $key
116
     * @return bool
117
     */
118 4
    public function has($key): bool
119
    {
120
        try {
121 4
            return $this->cache->exists();
122 1
        } catch (CacheException $ex) {
123 1
            return false;
124
        }
125
    }
126
}
127