KwPoolAdapter   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 63
c 1
b 0
f 1
dl 0
loc 123
ccs 62
cts 62
cp 1
rs 10
wmc 23

10 Methods

Rating   Name   Duplication   Size   Complexity  
A save() 0 11 3
A clear() 0 8 2
A __construct() 0 4 1
A saveDeferred() 0 7 2
A getItem() 0 10 2
A hasItem() 0 8 2
A deleteItems() 0 7 3
A deleteItem() 0 10 2
A commit() 0 7 3
A getItems() 0 10 3
1
<?php
2
3
namespace kalanis\kw_cache_psr\Adapters\PoolCache;
4
5
6
use kalanis\kw_cache\CacheException;
7
use kalanis\kw_cache\Interfaces\ICache;
8
use kalanis\kw_cache\Interfaces\IFormat;
9
use kalanis\kw_cache_psr\InvalidArgumentException;
10
use kalanis\kw_cache_psr\Traits\TCheckKey;
11
use Psr\Cache\CacheItemInterface;
12
use Psr\Cache\CacheItemPoolInterface;
13
use Traversable;
14
15
16
/**
17
 * Class ItemPoolAdapter
18
 * @package kalanis\kw_cache_psr\Adapters\PoolCache
19
 * KwCache pool adapter for PSR Cache Interface
20
 *
21
 * OK, what kind of idiot has that brilliant idea about pool/item combo? Expire is known by storage, but not by item - volume. redis, memcache.
22
 * The Hit over item dependent on expire is also dumb, because that information is on storage, not that item itself.
23
 */
24
class KwPoolAdapter implements CacheItemPoolInterface
25
{
26
    use TCheckKey;
27
28
    protected ICache $cache;
29
    protected IFormat $format;
30
    /** @var array<string, CacheItemInterface> */
31
    protected array $toSave = [];
32
    /** @var array<string> Only known to the run, others will stay! */
33
    protected array $keys = [];
34
35 8
    public function __construct(ICache $cache, IFormat $format)
36
    {
37 8
        $this->cache = $cache;
38 8
        $this->format = $format;
39 8
    }
40
41 2
    public function getItem($key)
42
    {
43
        try {
44 2
            $useKey = $this->checkKey($key);
45 2
            $this->cache->init([$useKey]);
46 1
            $data = new ItemAdapter($useKey);
47 1
            $data->set($this->format->unpack($this->cache->get()));
48 1
            return $data;
49 1
        } catch (CacheException $ex) {
50 1
            return new ItemAdapter($useKey);
51
        }
52
    }
53
54
    /**
55
     * @param array<string> $keys
56
     * @throws InvalidArgumentException
57
     * @-throws \Psr\Cache\InvalidArgumentException
58
     * @return array<string, CacheItemInterface>|Traversable<string, CacheItemInterface>
59
     */
60 1
    public function getItems(array $keys = array())
61
    {
62 1
        $result = [];
63 1
        foreach ($keys as $key) {
64 1
            if ($this->hasItem($key)) {
65 1
                $useKey = $this->checkKey($key);
66 1
                $result[$useKey] = $this->getItem($useKey);
67
            }
68
        }
69 1
        return $result;
70
    }
71
72 2
    public function hasItem($key)
73
    {
74
        try {
75 2
            $useKey = $this->checkKey($key);
76 2
            $this->cache->init([$useKey]);
77 1
            return $this->cache->exists();
78 1
        } catch (CacheException $ex) {
79 1
            return false;
80
        }
81
    }
82
83 2
    public function clear()
84
    {
85
        // clear things which can be saved later
86 2
        $this->toSave = [];
87
        try {
88 2
            return $this->deleteItems($this->keys);
89 1
        } catch (\Psr\Cache\InvalidArgumentException $e) {
90 1
            return false;
91
        }
92
    }
93
94 3
    public function deleteItem($key)
95
    {
96
        try {
97 3
            $useKey = $this->checkKey($key);
98 2
            $this->cache->init([$useKey]);
99 1
            $this->cache->clear();
100 1
            unset($this->keys[$useKey]);
101 1
            return true;
102 2
        } catch (CacheException $ex) {
103 1
            return false;
104
        }
105
    }
106
107 2
    public function deleteItems(array $keys)
108
    {
109 2
        $result = true;
110 2
        foreach ($keys as $key) {
111 2
            $result = $result && $this->deleteItem($key);
112
        }
113 1
        return $result;
114
    }
115
116 3
    public function save(CacheItemInterface $item)
117
    {
118
        try {
119 3
            $useKey = $this->checkKey($item->getKey());
120 2
            $this->keys[$useKey] = $useKey;
121 2
            $this->cache->init([$useKey]);
122 1
            return $this->cache->set(strval($this->format->pack($item->get())));
123 3
        } catch (CacheException $ex) {
124 1
            return false;
125 2
        } catch (InvalidArgumentException $ex) {
126 2
            return false;
127
        }
128
    }
129
130 2
    public function saveDeferred(CacheItemInterface $item)
131
    {
132
        try {
133 2
            $this->toSave[$this->checkKey($item->getKey())] = $item;
134 1
            return true;
135 1
        } catch (InvalidArgumentException $ex) {
136 1
            return false;
137
        }
138
    }
139
140 1
    public function commit()
141
    {
142 1
        $result = true;
143 1
        foreach ($this->toSave as $item) {
144 1
            $result = $result && $this->save($item);
145
        }
146 1
        return $result;
147
    }
148
}
149