File::clear()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BlueCache\Storage;
4
5
use Psr\Cache\CacheItemInterface;
6
use BlueCache\CacheException;
7
8
class File implements StorageInterface
9
{
10
    const CACHE_EXTENSION = '.cache';
11
12
    /**
13
     * @var array
14
     */
15
    protected $params = [
16
        'cache_path' => './var/cache',
17
    ];
18
19
    /**
20
     * @var array
21
     */
22
    protected $currentCache = [];
23
24
    /**
25
     * @param array $params
26
     */
27 39
    public function __construct(array $params = [])
28
    {
29 39
        $this->params = array_merge($this->params, $params);
30 39
    }
31
32
    /**
33
     * @param CacheItemInterface $item
34
     * @return bool
35
     * @throws \BlueCache\CacheException
36
     */
37 30
    public function store(CacheItemInterface $item)
38
    {
39 30
        $data = serialize($item);
40 30
        $key = $item->getKey();
41
42 30
        $cacheFile = $this->getFilePath($key);
43 30
        $dir = $this->params['cache_path'];
44
45 30
        if (!file_exists($dir) && !is_dir($dir)) {
46 1
            throw new CacheException('Unable to create cache directory: ' . $this->params['cache_path']);
47
        }
48
49 29
        if (!@file_put_contents($cacheFile, $data)) {
50 3
            throw new CacheException('Unable to save log file: ' . $cacheFile);
51
        }
52
53 27
        return true;
54
    }
55
56
    /**
57
     * @param array|string $names
58
     * @return array|null|CacheItemInterface
59
     */
60 11
    public function restore($names)
61
    {
62 11
        if (\is_array($names)) {
63 5
            return $this->processNames($names);
64
        }
65
66 6
        return $this->getItem($names);
67
    }
68
69
    /**
70
     * @param array $names
71
     * @return array
72
     */
73 5
    protected function processNames(array $names)
74
    {
75 5
        $list = [];
76
77 5
        foreach ($names as $name) {
78 5
            $list[$name] = $this->getItem($name);
79 5
        }
80
81 5
        return $list;
82
    }
83
84
    /**
85
     * @param string|null $names
86
     * @return bool
87
     * @throws \BlueCache\CacheException
88
     */
89 6
    public function clear($names = null)
90
    {
91 6
        switch (true) {
92 6
            case \is_null($names):
93 3
                $cacheDir = $this->params['cache_path'] . DIRECTORY_SEPARATOR;
94 3
                $this->currentCache = [];
95
96 3
                return $this->clearMany(glob($cacheDir . '*.cache'), false);
97
98 4
            case \is_string($names):
99 3
                return $this->delete($names);
100
101 1
            default:
102 1
                throw new CacheException('Invalid type: ' . $names);
103
                break;
104 1
        }
105
    }
106
107
    /**
108
     * @param string $key
109
     * @return bool
110
     */
111 27
    public function exists($key)
112
    {
113
        /** @var CacheItemInterface|null $item */
114 27
        $item = $this->getCacheItem($key);
115
116 27
        if (\is_null($item)) {
117 21
            return false;
118
        }
119
120 23
        return $item->isHit();
121
    }
122
123
    /**
124
     * @param string $key
125
     * @return CacheItemInterface|null
126
     */
127 11
    protected function getItem($key)
128
    {
129 11
        if ($this->exists($key)) {
130 9
            return $this->currentCache[$key];
131
        }
132
133 2
        return null;
134
    }
135
136
    /**
137
     * @param string $key
138
     * @return CacheItemInterface|null
139
     */
140 27
    protected function getCacheItem($key)
141
    {
142 27
        if (!isset($this->currentCache[$key])) {
143 27
            if (file_exists($this->getFilePath($key))) {
144 24
                return $this->getUnserializedCacheItem($key);
145
            }
146
147 20
            return null;
148
        }
149
150 6
        return $this->currentCache[$key];
151
    }
152
153
    /**
154
     * @param string $key
155
     * @return null|CacheItemInterface
156
     */
157 24
    protected function getUnserializedCacheItem($key)
158
    {
159
        /** @var CacheItemInterface $item */
160 24
        $item = unserialize($this->getCacheContent($key));
161
162 24
        if (!$item->isHit()) {
163 1
            $this->delete($key);
164 1
            return null;
165
        }
166
167 23
        $this->currentCache[$key] = $item;
168 23
        return $item;
169
    }
170
171
    /**
172
     * @param string $key
173
     * @return bool|string
174
     */
175 24
    protected function getCacheContent($key)
176
    {
177 24
        return file_get_contents($this->getFilePath($key));
178
    }
179
180
    /**
181
     * @param array $list
182
     * @param bool $isKey
183
     * @return bool
184
     */
185 7
    public function clearMany(array $list, $isKey = true)
186
    {
187 7
        $flag = true;
188
189 7
        foreach ($list as $name) {
190 7
            $deleted = $this->delete($name, $isKey);
191
192 7
            if (!$deleted) {
193 1
                $flag = false;
194 1
            }
195 7
        }
196
197 7
        return $flag;
198
    }
199
200
    /**
201
     * @param string $key
202
     * @param bool $isKey
203
     * @return bool
204
     */
205 9
    protected function delete($key, $isKey = true)
206
    {
207 9
        if ($isKey) {
208 7
            unset($this->currentCache[$key]);
209 7
            $key = $this->getFilePath($key);
210 7
        }
211
212 9
        return @unlink($key);
213
    }
214
215
    /**
216
     * @param string $key
217
     * @return string
218
     */
219 31
    protected function getFilePath($key)
220
    {
221 31
        return $this->params['cache_path'] . DIRECTORY_SEPARATOR . $key . self::CACHE_EXTENSION;
222
    }
223
}
224