Completed
Pull Request — master (#218)
by Thomas
05:43
created

BrowscapCache::getItem()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 13.125

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 36
ccs 9
cts 18
cp 0.5
rs 6.7272
c 1
b 1
f 0
cc 7
eloc 20
nc 10
nop 3
crap 13.125
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Cache;
5
6
use Psr\SimpleCache\CacheInterface;
7
use Psr\SimpleCache\InvalidArgumentException;
8
9
/**
10
 * A cache proxy to be able to use the cache adapters provided by the WurflCache package
11
 */
12
final class BrowscapCache implements BrowscapCacheInterface
13
{
14
    /**
15
     * Path to the cache directory
16
     *
17
     * @var \Psr\SimpleCache\CacheInterface
18
     */
19
    private $cache = null;
20
21
    /**
22
     * Detected browscap version (read from INI file)
23
     *
24
     * @var int
25
     */
26
    private $version = null;
27
28
    /**
29
     * Release date of the Browscap data (read from INI file)
30
     *
31
     * @var string
32
     */
33
    private $releaseDate;
34
35
    /**
36
     * Type of the Browscap data (read from INI file)
37
     *
38
     * @var string
39
     */
40
    private $type;
41
42
    /**
43
     * Constructor class, checks for the existence of (and loads) the cache and
44
     * if needed updated the definitions
45
     *
46
     * @param \Psr\SimpleCache\CacheInterface $adapter
47
     */
48 4
    public function __construct(CacheInterface $adapter)
49
    {
50 4
        $this->cache = $adapter;
51 4
    }
52
53
    /**
54
     * Gets the version of the Browscap data
55
     *
56
     * @return int
57
     */
58 2
    public function getVersion() : ?int
59
    {
60 2
        if ($this->version === null) {
61 2
            $success = null;
62
63 2
            $version = $this->getItem('browscap.version', false, $success);
64
65 2
            if ($version !== null && $success) {
66 1
                $this->version = (int) $version;
67
            }
68
        }
69
70 2
        return $this->version;
71
    }
72
73
    /**
74
     * Gets the release date of the Browscap data
75
     *
76
     * @return string|null
77
     */
78 1
    public function getReleaseDate() : ?string
79
    {
80 1
        if ($this->releaseDate === null) {
81 1
            $success = null;
82
83 1
            $releaseDate = $this->getItem('browscap.releaseDate', false, $success);
84
85 1
            if ($releaseDate !== null && $success) {
86 1
                $this->releaseDate = $releaseDate;
87
            }
88
        }
89
90 1
        return $this->releaseDate;
91
    }
92
93
    /**
94
     * Gets the type of the Browscap data
95
     */
96 1
    public function getType() : ?string
97
    {
98 1
        if ($this->type === null) {
99 1
            $success = null;
100
101 1
            $type = $this->getItem('browscap.type', false, $success);
102
103 1
            if ($type !== null && $success) {
104 1
                $this->type = $type;
105
            }
106
        }
107
108 1
        return $this->type;
109
    }
110
111
    /**
112
     * Get an item.
113
     *
114
     * @param string $cacheId
115
     * @param bool   $withVersion
116
     * @param bool   &$success
117
     *
118
     * @return mixed Data on success, null on failure
119
     */
120 3
    public function getItem(string $cacheId, bool $withVersion = true, ?bool &$success = null)
121
    {
122 3
        if ($withVersion) {
123
            $cacheId .= '.' . $this->getVersion();
124
        }
125
126
        try {
127 3
            if (! $this->cache->has($cacheId)) {
128 3
                $success = false;
129
130 3
                return null;
131
            }
132
        } catch (InvalidArgumentException $e) {
133
            $success = false;
134
135
            return null;
136
        }
137
138
        try {
139 3
            $data = $this->cache->get($cacheId);
140
        } catch (InvalidArgumentException $e) {
141
            $success = false;
142
143
            return null;
144
        }
145
146 3
        if (! is_array($data) || ! array_key_exists('content', $data)) {
147
            $success = false;
148
149
            return null;
150
        }
151
152 3
        $success = true;
153
154 3
        return unserialize($data['content']);
155
    }
156
157
    /**
158
     * save the content into an php file
159
     *
160
     * @param string $cacheId     The cache id
161
     * @param mixed  $content     The content to store
162
     * @param bool   $withVersion
163
     *
164
     * @return bool whether the file was correctly written to the disk
165
     */
166 3
    public function setItem(string $cacheId, $content, bool $withVersion = true) : bool
167
    {
168
        // Get the whole PHP code
169
        $data = [
170 3
            'content' => serialize($content),
171
        ];
172
173 3
        if ($withVersion) {
174
            $cacheId .= '.' . $this->getVersion();
175
        }
176
177
        try {
178
            // Save and return
179 3
            return $this->cache->set($cacheId, $data);
180
        } catch (InvalidArgumentException $e) {
181
            // do nothing here
182
        }
183
184
        return false;
185
    }
186
187
    /**
188
     * Test if an item exists.
189
     *
190
     * @param string $cacheId
191
     * @param bool   $withVersion
192
     *
193
     * @return bool
194
     */
195
    public function hasItem(string $cacheId, bool $withVersion = true) : bool
196
    {
197
        if ($withVersion) {
198
            $cacheId .= '.' . $this->getVersion();
199
        }
200
201
202
        try {
203
            return $this->cache->has($cacheId);
204
        } catch (InvalidArgumentException $e) {
205
            // do nothing here
206
        }
207
208
        return false;
209
    }
210
211
    /**
212
     * Remove an item.
213
     *
214
     * @param string $cacheId
215
     * @param bool   $withVersion
216
     *
217
     * @return bool
218
     */
219
    public function removeItem(string $cacheId, bool $withVersion = true) : bool
220
    {
221
        if ($withVersion) {
222
            $cacheId .= '.' . $this->getVersion();
223
        }
224
225
        try {
226
            return $this->cache->delete($cacheId);
227
        } catch (InvalidArgumentException $e) {
228
            // do nothing here
229
        }
230
231
        return false;
232
    }
233
234
    /**
235
     * Flush the whole storage
236
     *
237
     * @return bool
238
     */
239
    public function flush() : bool
240
    {
241
        return $this->cache->clear();
242
    }
243
}
244