Completed
Pull Request — master (#130)
by Thomas
12:04
created

BrowscapCache::getVersion()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 7
nc 3
nop 0
crap 4
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
 * @package    Cache
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Cache;
32
33
use WurflCache\Adapter\AdapterInterface;
34
35
/**
36
 * a cache proxy to be able to use the cache adapters provided by the WurflCache package
37
 *
38
 * @category   Browscap-PHP
39
 * @package    Cache
40
 * @author     Thomas Müller <[email protected]>
41
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
42
 * @version    3.0
43
 * @license    http://www.opensource.org/licenses/MIT MIT License
44
 * @link       https://github.com/browscap/browscap-php/
45
 */
46
class BrowscapCache implements BrowscapCacheInterface
47
{
48
    /**
49
     * Path to the cache directory
50
     *
51
     * @var \WurflCache\Adapter\AdapterInterface
52
     */
53
    private $cache = null;
54
55
    /**
56
     * Detected browscap version (read from INI file)
57
     *
58
     * @var int
59
     */
60
    private $version = null;
61
62
    /**
63
     * Constructor class, checks for the existence of (and loads) the cache and
64
     * if needed updated the definitions
65
     *
66
     * @param \WurflCache\Adapter\AdapterInterface $adapter
67
     * @param int                                  $updateInterval
68
     */
69 12
    public function __construct(AdapterInterface $adapter, $updateInterval = BrowscapCacheInterface::CACHE_LIVETIME)
70
    {
71 12
        $this->cache = $adapter;
72 12
        $this->cache->setExpiration((int) $updateInterval);
73 12
    }
74
75
    /**
76
     * Gets the version of the Browscap data
77
     *
78
     * @return int
79
     */
80 4
    public function getVersion()
81
    {
82 4
        if ($this->version === null) {
83 4
            $success = true;
84
85 4
            $version = $this->getItem('browscap.version', false, $success);
86
87 4
            if ($version !== null && $success) {
88 3
                $this->version = (int) $version;
89
            }
90
        }
91
92 4
        return $this->version;
93
    }
94
95
    /**
96
     * Get an item.
97
     *
98
     * @param string $cacheId
99
     * @param bool   $withVersion
100
     * @param bool   $success
101
     *
102
     * @return mixed Data on success, null on failure
103
     */
104 4
    public function getItem($cacheId, $withVersion = true, & $success = null)
105
    {
106 4
        if ($withVersion) {
107
            $cacheId .= '.'.$this->getVersion();
108
        }
109
110 4
        if (!$this->cache->hasItem($cacheId)) {
111 4
            $success = false;
112
113 4
            return null;
114
        }
115
116 3
        $success = null;
117 3
        $data    = $this->cache->getItem($cacheId, $success);
118
119 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...
120
            $success = false;
121
122
            return null;
123
        }
124
125 3
        if (!isset($data['content'])) {
126
            $success = false;
127
128
            return null;
129
        }
130
131 3
        $success = true;
132
133 3
        return unserialize($data['content']);
134
    }
135
136
    /**
137
     * save the content into an php file
138
     *
139
     * @param string $cacheId     The cache id
140
     * @param mixed  $content     The content to store
141
     * @param bool   $withVersion
142
     *
143
     * @return boolean whether the file was correctly written to the disk
144
     */
145 3
    public function setItem($cacheId, $content, $withVersion = true)
146
    {
147
        // Get the whole PHP code
148
        $data = array(
149 3
            'content' => serialize($content),
150 3
        );
151
152 3
        if ($withVersion) {
153 3
            $cacheId .= '.'.$this->getVersion();
154
        }
155
156
        // Save and return
157 3
        return $this->cache->setItem($cacheId, $data);
158
    }
159
160
    /**
161
     * Test if an item exists.
162
     *
163
     * @param string $cacheId
164
     * @param bool   $withVersion
165
     *
166
     * @return bool
167
     */
168 1
    public function hasItem($cacheId, $withVersion = true)
169
    {
170 1
        if ($withVersion) {
171 1
            $cacheId .= '.'.$this->getVersion();
172
        }
173
174 1
        return $this->cache->hasItem($cacheId);
175
    }
176
177
    /**
178
     * Remove an item.
179
     *
180
     * @param string $cacheId
181
     * @param bool   $withVersion
182
     *
183
     * @return bool
184
     */
185
    public function removeItem($cacheId, $withVersion = true)
186
    {
187
        if ($withVersion) {
188
            $cacheId .= '.'.$this->getVersion();
189
        }
190
191
        return $this->cache->removeItem($cacheId);
192
    }
193
194
    /**
195
     * Flush the whole storage
196
     *
197
     * @return bool
198
     */
199
    public function flush()
200
    {
201
        return $this->cache->flush();
202
    }
203
}
204