MemoryPoolAdapter   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 40
c 1
b 0
f 1
dl 0
loc 92
ccs 43
cts 43
cp 1
rs 10
wmc 20

9 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteItems() 0 7 3
A hasItem() 0 4 2
A getItems() 0 10 3
A deleteItem() 0 7 2
A getItem() 0 5 2
A commit() 0 7 3
A save() 0 7 2
A saveDeferred() 0 7 2
A clear() 0 5 1
1
<?php
2
3
namespace kalanis\kw_cache_psr\Adapters\PoolCache;
4
5
6
use kalanis\kw_cache_psr\InvalidArgumentException;
7
use kalanis\kw_cache_psr\Traits\TCheckKey;
8
use Psr\Cache\CacheItemInterface;
9
use Psr\Cache\CacheItemPoolInterface;
10
11
12
/**
13
 * Class MemoryPoolAdapter
14
 * @package kalanis\kw_cache_psr\Adapters\PoolCache
15
 * Memory pool adapter for PSR Cache Interface
16
 */
17
class MemoryPoolAdapter implements CacheItemPoolInterface
18
{
19
    use TCheckKey;
20
21
    /** @var CacheItemInterface[] */
22
    protected array $items = [];
23
    /** @var array<string, CacheItemInterface> */
24
    protected array $toSave = [];
25
26 1
    public function getItem($key)
27
    {
28 1
        $useKey = $this->checkKey($key);
29 1
        $data = $this->hasItem($key) ? $this->items[$useKey] : new ItemAdapter($useKey);
30 1
        return $data;
31
    }
32
33
    /**
34
     * @param array<string> $keys
35
     * @throws InvalidArgumentException
36
     * @-throws \Psr\Cache\InvalidArgumentException
37
     * @return array<string, CacheItemInterface>|\Traversable<string, CacheItemInterface>
38
     */
39 1
    public function getItems(array $keys = array())
40
    {
41 1
        $result = [];
42 1
        foreach ($keys as $key) {
43 1
            if ($this->hasItem($key)) {
44 1
                $useKey = $this->checkKey($key);
45 1
                $result[$useKey] = $this->getItem($useKey);
46
            }
47
        }
48 1
        return $result;
49
    }
50
51 2
    public function hasItem($key)
52
    {
53 2
        $useKey = $this->checkKey($key);
54 2
        return isset($this->items[$useKey]) && $this->items[$useKey]->isHit();
55
    }
56
57 1
    public function clear()
58
    {
59 1
        $this->items = [];
60 1
        $this->toSave = [];
61 1
        return true;
62
    }
63
64 1
    public function deleteItem($key)
65
    {
66 1
        $useKey = $this->checkKey($key);
67 1
        if (isset($this->items[$useKey])) {
68 1
            unset($this->items[$useKey]);
69
        }
70 1
        return true;
71
    }
72
73 1
    public function deleteItems(array $keys)
74
    {
75 1
        $result = true;
76 1
        foreach ($keys as $key) {
77 1
            $result = $result && $this->deleteItem($key);
78
        }
79 1
        return $result;
80
    }
81
82 2
    public function save(CacheItemInterface $item)
83
    {
84
        try {
85 2
            $this->items[$this->checkKey($item->getKey())] = $item;
86 2
            return true;
87 1
        } catch (InvalidArgumentException $ex) {
88 1
            return false;
89
        }
90
    }
91
92 2
    public function saveDeferred(CacheItemInterface $item)
93
    {
94
        try {
95 2
            $this->toSave[$this->checkKey($item->getKey())] = $item;
96 1
            return true;
97 1
        } catch (InvalidArgumentException $ex) {
98 1
            return false;
99
        }
100
    }
101
102 1
    public function commit()
103
    {
104 1
        $result = true;
105 1
        foreach ($this->toSave as $item) {
106 1
            $result = $result && $this->save($item);
107
        }
108 1
        return $result;
109
    }
110
}
111