Completed
Pull Request — master (#177)
by Thomas
13:10 queued 01:03
created

BrowscapCache::setItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
crap 2
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @copyright  1998-2015 Browser Capabilities Project
25
 * @license    http://www.opensource.org/licenses/MIT MIT License
26
 * @link       https://github.com/browscap/browscap-php/
27
 * @since      added with version 3.0
28
 */
29
30
namespace BrowscapPHP\Cache;
31
32
use WurflCache\Adapter\AdapterInterface;
33
34
/**
35
 * a cache proxy to be able to use the cache adapters provided by the WurflCache package
36
 *
37
 * @category   Browscap-PHP
38
 * @author     Thomas Müller <[email protected]>
39
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
40
 * @version    3.0
41
 * @license    http://www.opensource.org/licenses/MIT MIT License
42
 * @link       https://github.com/browscap/browscap-php/
43
 */
44
class BrowscapCache implements BrowscapCacheInterface
45
{
46
    /**
47
     * Path to the cache directory
48
     *
49
     * @var \WurflCache\Adapter\AdapterInterface
50
     */
51
    private $cache = null;
52
53
    /**
54
     * Detected browscap version (read from INI file)
55
     *
56
     * @var int
57
     */
58
    private $version = null;
59
60
    /**
61
     * Release date of the Browscap data (read from INI file)
62
     *
63
     * @var string
64
     */
65
    private $releaseDate;
66
67
    /**
68
     * Type of the Browscap data (read from INI file)
69
     *
70
     * @var string
71
     */
72
    private $type;
73
74
    /**
75
     * Constructor class, checks for the existence of (and loads) the cache and
76
     * if needed updated the definitions
77
     *
78
     * @param \WurflCache\Adapter\AdapterInterface $adapter
79
     */
80 17
    public function __construct(AdapterInterface $adapter)
81
    {
82 17
        $this->cache = $adapter;
83 17
    }
84
85
    /**
86
     * Gets the version of the Browscap data
87
     *
88
     * @return int
89
     */
90 6 View Code Duplication
    public function getVersion()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92 6
        if ($this->version === null) {
93 6
            $success = true;
94
95 6
            $version = $this->getItem('browscap.version', false, $success);
96
97 6
            if ($version !== null && $success) {
98 4
                $this->version = (int) $version;
99
            }
100
        }
101
102 6
        return $this->version;
103
    }
104
105
    /**
106
     * Gets the release date of the Browscap data
107
     *
108
     * @return string
109
     */
110 1
    public function getReleaseDate()
111
    {
112 1
        if ($this->releaseDate === null) {
113 1
            $success = true;
114
115 1
            $releaseDate = $this->getItem('browscap.releaseDate', false, $success);
116
117 1
            if ($releaseDate !== null && $success) {
118 1
                $this->releaseDate = $releaseDate;
119
            }
120
        }
121
122 1
        return $this->releaseDate;
123
    }
124
125
    /**
126
     * Gets the type of the Browscap data
127
     *
128
     * @return string
129
     */
130 1 View Code Duplication
    public function getType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
131
    {
132 1
        if ($this->type === null) {
133 1
            $success = true;
134
135 1
            $type = $this->getItem('browscap.type', false, $success);
136
137 1
            if ($type !== null && $success) {
138 1
                $this->type = $type;
139
            }
140
        }
141
142 1
        return $this->type;
143
    }
144
145
    /**
146
     * Get an item.
147
     *
148
     * @param string $cacheId
149
     * @param bool   $withVersion
150
     * @param bool   $success
151
     *
152
     * @return mixed Data on success, null on failure
153
     */
154 7
    public function getItem($cacheId, $withVersion = true, & $success = null)
155
    {
156 7
        if ($withVersion) {
157
            $cacheId .= '.' . $this->getVersion();
158
        }
159
160 7
        if (!$this->cache->hasItem($cacheId)) {
161 7
            $success = false;
162
163 7
            return null;
164
        }
165
166 6
        $success = null;
167 6
        $data    = $this->cache->getItem($cacheId, $success);
168
169 6
        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...
170
            $success = false;
171
172
            return null;
173
        }
174
175 6
        if (!isset($data['content'])) {
176
            $success = false;
177
178
            return null;
179
        }
180
181 6
        $success = true;
182
183 6
        return unserialize($data['content']);
184
    }
185
186
    /**
187
     * save the content into an php file
188
     *
189
     * @param string $cacheId     The cache id
190
     * @param mixed  $content     The content to store
191
     * @param bool   $withVersion
192
     *
193
     * @return bool whether the file was correctly written to the disk
194
     */
195 6
    public function setItem($cacheId, $content, $withVersion = true)
196
    {
197
        // Get the whole PHP code
198
        $data = [
199 6
            'content' => serialize($content),
200 6
        ];
201
202 6
        if ($withVersion) {
203 3
            $cacheId .= '.' . $this->getVersion();
204
        }
205
206
        // Save and return
207 6
        return $this->cache->setItem($cacheId, $data);
208
    }
209
210
    /**
211
     * Test if an item exists.
212
     *
213
     * @param string $cacheId
214
     * @param bool   $withVersion
215
     *
216
     * @return bool
217
     */
218
    public function hasItem($cacheId, $withVersion = true)
219
    {
220
        if ($withVersion) {
221
            $cacheId .= '.' . $this->getVersion();
222
        }
223
224
        return $this->cache->hasItem($cacheId);
225
    }
226
227
    /**
228
     * Remove an item.
229
     *
230
     * @param string $cacheId
231
     * @param bool   $withVersion
232
     *
233
     * @return bool
234
     */
235
    public function removeItem($cacheId, $withVersion = true)
236
    {
237
        if ($withVersion) {
238
            $cacheId .= '.' . $this->getVersion();
239
        }
240
241
        return $this->cache->removeItem($cacheId);
242
    }
243
244
    /**
245
     * Flush the whole storage
246
     *
247
     * @return bool
248
     */
249
    public function flush()
250
    {
251
        return $this->cache->flush();
252
    }
253
}
254