Passed
Push — develop ( 2893dc...c61bef )
by Michał
02:37
created

Cache::commitSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.864

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 2
cts 5
cp 0.4
crap 2.864
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Allows to manage cache
4
 * cache structure (module_code/cache_code.cache)
5
 *
6
 * @package     Blue
7
 * @subpackage  Cache
8
 * @author      chajr <[email protected]>
9
 */
10
namespace BlueCache;
11
12
use Psr\Cache\CacheItemPoolInterface;
13
use Psr\Cache\CacheItemInterface;
14
15
class Cache implements CacheItemPoolInterface
16
{
17
    use Common;
18
19
    /**
20
     * @var array
21
     */
22
    protected $deferred = [];
23
24
    /**
25
     * @var array
26
     */
27
    protected $cacheCommitExceptions = [];
28
29
    /**
30
     * @param string $name
31
     * @return null|CacheItemInterface
32
     */
33 1
    public function getItem($name)
34
    {
35 1
        if ($this->hasItem($name)) {
36 1
            return $this->storage->restore($name);
37
        }
38
39
        return null;
40
    }
41
42
    /**
43
     * @param array $names
44
     * @return array
45
     */
46 4
    public function getItems(array $names = [])
47
    {
48 4
        $list = [];
49
50 4
        foreach ($names as $name) {
51 1
            $list[$name] = $this->getItem($name);
52 4
        }
53
54 4
        return $list;
55
    }
56
57
    /**
58
     * @param string $name
59
     * @return bool
60
     */
61 6
    public function hasItem($name)
62
    {
63 6
        return $this->storage->exists($name);
64
    }
65
66
    /**
67
     * @return bool
68
     * @throws CacheException
69
     */
70 1
    public function clear()
71
    {
72 1
        return $this->storage->clear();
73
    }
74
75
    /**
76
     * @param string $name
77
     * @return bool
78
     * @throws CacheException
79
     */
80 1
    public function deleteItem($name)
81
    {
82 1
        return $this->storage->clear($name);
83
    }
84
85
    /**
86
     * @param array $names
87
     * @return bool
88
     * @throws CacheException
89
     */
90 1
    public function deleteItems(array $names)
91
    {
92 1
        return $this->storage->clear($names);
0 ignored issues
show
Bug introduced by
$names of type array is incompatible with the type null|string expected by parameter $name of BlueCache\Storage\StorageInterface::clear(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
        return $this->storage->clear(/** @scrutinizer ignore-type */ $names);
Loading history...
93
    }
94
95
    /**
96
     * @param CacheItemInterface $item
97
     * @return bool
98
     * @throws CacheException
99
     */
100 6
    public function save(CacheItemInterface $item)
101
    {
102 6
        return $this->storage->store($item);
103
    }
104
105
    /**
106
     * Keep cache items to store it later
107
     *
108
     * @param CacheItemInterface $item
109
     * @return bool
110
     */
111 2
    public function saveDeferred(CacheItemInterface $item)
112
    {
113 2
        $this->deferred[] = $item;
114 2
        return true;
115
    }
116
117
    /**
118
     * Store all cache items added by saveDeferred
119
     *
120
     * @return bool
121
     * @throws CacheException
122
     */
123 10
    public function commit()
124
    {
125 10
        $cacheExceptions = [];
126 10
        $this->cacheCommitExceptions = [];
127 10
        $flag = true;
128
129 10
        foreach ($this->deferred as $item) {
130 2
            if (!$this->commitSave($item)) {
131
                $flag = false;
132
            }
133 10
        }
134
135 10
        if (!empty($this->cacheCommitExceptions)) {
136
            throw new CacheException('Error on saving cache items: ' . implode('; ', $cacheExceptions));
137
        }
138
139 10
        $this->deferred = [];
140
141 10
        return $flag;
142
    }
143
144
    /**
145
     * @param CacheItemInterface $item
146
     * @return bool
147
     */
148 2
    protected function commitSave(CacheItemInterface $item)
149
    {
150
        try {
151 2
            return $this->save($item);
152
        } catch (CacheException $exception) {
153
            $this->cacheCommitExceptions[] = $exception->getMessage();
154
155
            return false;
156
        }
157
    }
158
159
    /**
160
     * @throws CacheException
161
     */
162 10
    public function __destruct()
163
    {
164 10
        $this->commit();
165 10
    }
166
}
167