Completed
Push — min-php71 ( f5a25a )
by James
05:53
created

BrowscapCache::setItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
crap 2.032
1
<?php
2
declare(strict_types=1);
3
4
namespace BrowscapPHP\Cache;
5
6
use WurflCache\Adapter\AdapterInterface;
7
8
/**
9
 * A cache proxy to be able to use the cache adapters provided by the WurflCache package
10
 */
11
final class BrowscapCache implements BrowscapCacheInterface
12
{
13
    /**
14
     * Path to the cache directory
15
     *
16
     * @var \WurflCache\Adapter\AdapterInterface
17
     */
18
    private $cache = null;
19
20
    /**
21
     * Detected browscap version (read from INI file)
22
     *
23
     * @var int
24
     */
25
    private $version = null;
26
27
    /**
28
     * Release date of the Browscap data (read from INI file)
29
     *
30
     * @var string
31
     */
32
    private $releaseDate;
33
34
    /**
35
     * Type of the Browscap data (read from INI file)
36
     *
37
     * @var string
38
     */
39
    private $type;
40
41
    /**
42
     * Constructor class, checks for the existence of (and loads) the cache and
43
     * if needed updated the definitions
44
     *
45
     * @param \WurflCache\Adapter\AdapterInterface $adapter
46
     */
47 5
    public function __construct(AdapterInterface $adapter)
48
    {
49 5
        $this->cache = $adapter;
50 5
    }
51
52
    /**
53
     * Gets the version of the Browscap data
54
     *
55
     * @return int
56
     */
57 2
    public function getVersion() : ?int
58
    {
59 2
        if ($this->version === null) {
60 2
            $success = true;
61
62 2
            $version = $this->getItem('browscap.version', false, $success);
63
64 2
            if ($version !== null && $success) {
65 1
                $this->version = (int) $version;
66
            }
67
        }
68
69 2
        return $this->version;
70
    }
71
72
    /**
73
     * Gets the release date of the Browscap data
74
     *
75
     * @return string
76
     */
77 1
    public function getReleaseDate() : string
78
    {
79 1
        if ($this->releaseDate === null) {
80 1
            $success = true;
81
82 1
            $releaseDate = $this->getItem('browscap.releaseDate', false, $success);
83
84 1
            if ($releaseDate !== null && $success) {
85 1
                $this->releaseDate = $releaseDate;
86
            }
87
        }
88
89 1
        return $this->releaseDate;
90
    }
91
92
    /**
93
     * Gets the type of the Browscap data
94
     */
95 1
    public function getType() : ?string
96
    {
97 1
        if ($this->type === null) {
98 1
            $success = true;
99
100 1
            $type = $this->getItem('browscap.type', false, $success);
101
102 1
            if ($type !== null && $success) {
103 1
                $this->type = $type;
104
            }
105
        }
106
107 1
        return $this->type;
108
    }
109
110
    /**
111
     * Get an item.
112
     *
113
     * @param string $cacheId
114
     * @param bool   $withVersion
115
     * @param bool   $success
116
     *
117
     * @return mixed Data on success, null on failure
118
     */
119 3
    public function getItem(string $cacheId, bool $withVersion = true, ?bool & $success = null)
120
    {
121 3
        if ($withVersion) {
122
            $cacheId .= '.' . $this->getVersion();
123
        }
124
125 3
        if (!$this->cache->hasItem($cacheId)) {
126 3
            $success = false;
127
128 3
            return null;
129
        }
130
131 3
        $success = null;
132 3
        $data    = $this->cache->getItem($cacheId, $success);
133
134 3
        if (!$success) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $success of type boolean|null is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
135
            $success = false;
136
137
            return null;
138
        }
139
140 3
        if (!isset($data['content'])) {
141
            $success = false;
142
143
            return null;
144
        }
145
146 3
        $success = true;
147
148 3
        return unserialize($data['content']);
149
    }
150
151
    /**
152
     * save the content into an php file
153
     *
154
     * @param string $cacheId     The cache id
155
     * @param mixed  $content     The content to store
156
     * @param bool   $withVersion
157
     *
158
     * @return bool whether the file was correctly written to the disk
159
     */
160 3
    public function setItem(string $cacheId, $content, bool $withVersion = true) : bool
161
    {
162
        // Get the whole PHP code
163
        $data = [
164 3
            'content' => serialize($content),
165
        ];
166
167 3
        if ($withVersion) {
168
            $cacheId .= '.' . $this->getVersion();
169
        }
170
171
        // Save and return
172 3
        return $this->cache->setItem($cacheId, $data);
173
    }
174
175
    /**
176
     * Test if an item exists.
177
     *
178
     * @param string $cacheId
179
     * @param bool   $withVersion
180
     *
181
     * @return bool
182
     */
183
    public function hasItem(string $cacheId, bool $withVersion = true) : bool
184
    {
185
        if ($withVersion) {
186
            $cacheId .= '.' . $this->getVersion();
187
        }
188
189
        return $this->cache->hasItem($cacheId);
190
    }
191
192
    /**
193
     * Remove an item.
194
     *
195
     * @param string $cacheId
196
     * @param bool   $withVersion
197
     *
198
     * @return bool
199
     */
200
    public function removeItem(string $cacheId, bool $withVersion = true) : bool
201
    {
202
        if ($withVersion) {
203
            $cacheId .= '.' . $this->getVersion();
204
        }
205
206
        return $this->cache->removeItem($cacheId);
207
    }
208
209
    /**
210
     * Flush the whole storage
211
     *
212
     * @return bool
213
     */
214
    public function flush() : bool
215
    {
216
        return $this->cache->flush();
217
    }
218
}
219