MemoryCacheItemPool   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.37%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 2
dl 0
loc 108
ccs 37
cts 38
cp 0.9737
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteItem() 0 10 3
A deleteItems() 0 9 3
A save() 0 6 1
A saveDeferred() 0 6 1
A commit() 0 8 2
A hasItem() 0 4 1
A getItem() 0 7 2
A getItems() 0 10 3
A clear() 0 6 1
1
<?php
2
3
namespace PhpAbac\Cache\Pool;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Psr\Cache\CacheItemInterface;
7
8
use PhpAbac\Cache\Item\MemoryCacheItem;
9
10
class MemoryCacheItemPool implements CacheItemPoolInterface
11
{
12
    /** @var array **/
13
    protected $items;
14
    /** @var array **/
15
    protected $deferredItems;
16
    /**
17
     * {@inheritdoc}
18
     */
19 2
    public function deleteItem($key)
20
    {
21 2
        if (isset($this->items[$key])) {
22 2
            unset($this->items[$key]);
23
        }
24 2
        if (isset($this->deferredItems[$key])) {
25
            unset($this->deferredItems[$key]);
26
        }
27 2
        return true;
28
    }
29
    
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function deleteItems(array $keys)
34
    {
35 1
        foreach ($keys as $key) {
36 1
            if (!$this->deleteItem($key)) {
37 1
                return false;
38
            }
39
        }
40 1
        return true;
41
    }
42
    
43
    /**
44
     * {@inheritdoc}
45
     */
46 9
    public function save(CacheItemInterface $item)
47
    {
48 9
        $this->items[$item->getKey()] = $item;
49
        
50 9
        return true;
51
    }
52
    
53
    /**
54
     * {@inheritdoc}
55
     */
56 2
    public function saveDeferred(CacheItemInterface $item)
57
    {
58 2
        $this->deferredItems[$item->getKey()] = $item;
59
        
60 2
        return true;
61
    }
62
    
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function commit()
67
    {
68 2
        foreach ($this->deferredItems as $key => $item) {
69 2
            $this->items[$key] = $item;
70 2
            unset($this->deferredItems[$key]);
71
        }
72 2
        return true;
73
    }
74
    
75
    /**
76
     * {@inheritdoc}
77
     */
78 13
    public function hasItem($key)
79
    {
80 13
        return isset($this->items[$key]);
81
    }
82
    
83
    /**
84
     * {@inheritdoc}
85
     */
86 9
    public function getItem($key)
87
    {
88 9
        if (!$this->hasItem($key)) {
89 4
            return new MemoryCacheItem($key);
90
        }
91 7
        return $this->items[$key];
92
    }
93
    
94
    /**
95
     * {@inheritdoc}
96
     */
97 3
    public function getItems(array $keys = array())
98
    {
99 3
        $items = [];
100 3
        foreach ($keys as $key) {
101 3
            if ($this->hasItem($key)) {
102 3
                $items[$key] = $this->getItem($key);
103
            }
104
        }
105 3
        return $items;
106
    }
107
    
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function clear()
112
    {
113 1
        $this->items = [];
114 1
        $this->deferredItems = [];
115 1
        return true;
116
    }
117
}
118