Completed
Pull Request — master (#185)
by
unknown
12:19
created

BrowscapCache   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 209
Duplicated Lines 13.4 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 25
lcom 1
cbo 1
dl 28
loc 209
ccs 40
cts 55
cp 0.7272
rs 10
c 1
b 1
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getVersion() 14 14 4
A getReleaseDate() 0 14 4
A getType() 14 14 4
B getItem() 0 30 5
A setItem() 0 14 2
A hasItem() 0 8 2
A removeItem() 0 8 2
A flush() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Psr\SimpleCache\CacheInterface;
33
34
/**
35
 * a cache proxy to be able to use the cache adapters that implement the PSR-16 SimpleCache Cache interface
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 \Psr\SimpleCache\CacheInterface
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 \Psr\SimpleCache\CacheInterface $adapter
79
     */
80 17
    public function __construct(CacheInterface $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->has($cacheId)) {
161 7
            $success = false;
162
163 7
            return null;
164
        }
165
166 6
        $data = $this->cache->get($cacheId);
167
168 6
        if (null === $data) {
169
            $success = false;
170
171
            return null;
172
        }
173
174 6
        if (!isset($data['content'])) {
175
            $success = false;
176
177
            return null;
178
        }
179
180 6
        $success = true;
181
182 6
        return unserialize($data['content']);
183
    }
184
185
    /**
186
     * save the content into an php file
187
     *
188
     * @param string $cacheId     The cache id
189
     * @param mixed  $content     The content to store
190
     * @param bool   $withVersion
191
     *
192
     * @return bool whether the file was correctly written to the disk
193
     */
194 6
    public function setItem($cacheId, $content, $withVersion = true)
195
    {
196
        // Get the whole PHP code
197
        $data = [
198 6
            'content' => serialize($content),
199 6
        ];
200
201 6
        if ($withVersion) {
202 3
            $cacheId .= '.' . $this->getVersion();
203
        }
204
205
        // Save and return
206 6
        return $this->cache->set($cacheId, $data);
207
    }
208
209
    /**
210
     * Test if an item exists.
211
     *
212
     * @param string $cacheId
213
     * @param bool   $withVersion
214
     *
215
     * @return bool
216
     */
217
    public function hasItem($cacheId, $withVersion = true)
218
    {
219
        if ($withVersion) {
220
            $cacheId .= '.' . $this->getVersion();
221
        }
222
223
        return $this->cache->has($cacheId);
224
    }
225
226
    /**
227
     * Remove an item.
228
     *
229
     * @param string $cacheId
230
     * @param bool   $withVersion
231
     *
232
     * @return bool
233
     */
234
    public function removeItem($cacheId, $withVersion = true)
235
    {
236
        if ($withVersion) {
237
            $cacheId .= '.' . $this->getVersion();
238
        }
239
240
        return $this->cache->delete($cacheId);
241
    }
242
243
    /**
244
     * Flush the whole storage
245
     *
246
     * @return bool
247
     */
248
    public function flush()
249
    {
250
        return $this->cache->clear();
251
    }
252
}
253