BrowscapCache   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 62.3%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 0
dl 0
loc 231
ccs 38
cts 61
cp 0.623
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getVersion() 0 19 5
A getReleaseDate() 0 19 5
A getType() 0 19 5
A getItem() 0 24 5
A setItem() 0 14 2
A hasItem() 0 8 2
A removeItem() 0 8 2
A flush() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace BrowscapPHP\Cache;
5
6
use Psr\Log\LoggerInterface;
7
use Psr\SimpleCache\CacheInterface;
8
use Psr\SimpleCache\InvalidArgumentException;
9
10
/**
11
 * A cache proxy to be able to use the cache adapters provided by the WurflCache package
12
 */
13
final class BrowscapCache implements BrowscapCacheInterface
14
{
15
    /**
16
     * Path to the cache directory
17
     *
18
     * @var \Psr\SimpleCache\CacheInterface
19
     */
20
    private $cache;
21
22
    /**
23
     * @var \Psr\Log\LoggerInterface
24
     */
25
    private $logger;
26
27
    /**
28
     * Detected browscap version (read from INI file)
29
     *
30
     * @var int
31
     */
32
    private $version;
33
34
    /**
35
     * Release date of the Browscap data (read from INI file)
36
     *
37
     * @var string
38
     */
39
    private $releaseDate;
40
41
    /**
42
     * Type of the Browscap data (read from INI file)
43
     *
44
     * @var string
45
     */
46
    private $type;
47
48
    /**
49
     * Constructor class, checks for the existence of (and loads) the cache and
50
     * if needed updated the definitions
51
     *
52
     * @param \Psr\SimpleCache\CacheInterface $adapter
53
     * @param LoggerInterface $logger
54
     */
55 4
    public function __construct(CacheInterface $adapter, LoggerInterface $logger)
56
    {
57 4
        $this->cache = $adapter;
58 4
        $this->logger = $logger;
59 4
    }
60
61
    /**
62
     * Gets the version of the Browscap data
63
     *
64
     * @return int
65
     */
66 2
    public function getVersion() : ?int
67
    {
68 2
        if (null === $this->version) {
69 2
            $success = null;
70
71
            try {
72 2
                $version = $this->getItem('browscap.version', false, $success);
73
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Psr\SimpleCache\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
74
                $this->logger->error(new \InvalidArgumentException('an error occured while reading the data version from the cache', 0, $e));
75
                $version = null;
76
            }
77
78 2
            if (null !== $version && $success) {
79 1
                $this->version = (int) $version;
80
            }
81
        }
82
83 2
        return $this->version;
84
    }
85
86
    /**
87
     * Gets the release date of the Browscap data
88
     *
89
     * @return string|null
90
     */
91 1
    public function getReleaseDate() : ?string
92
    {
93 1
        if (null === $this->releaseDate) {
94 1
            $success = null;
95
96
            try {
97 1
                $releaseDate = $this->getItem('browscap.releaseDate', false, $success);
98
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Psr\SimpleCache\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
99
                $this->logger->error(new \InvalidArgumentException('an error occured while reading the data release date from the cache', 0, $e));
100
                $releaseDate = null;
101
            }
102
103 1
            if (null !== $releaseDate && $success) {
104 1
                $this->releaseDate = $releaseDate;
105
            }
106
        }
107
108 1
        return $this->releaseDate;
109
    }
110
111
    /**
112
     * Gets the type of the Browscap data
113
     */
114 1
    public function getType() : ?string
115
    {
116 1
        if (null === $this->type) {
117 1
            $success = null;
118
119
            try {
120 1
                $type = $this->getItem('browscap.type', false, $success);
121
            } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Psr\SimpleCache\InvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
122
                $this->logger->error(new \InvalidArgumentException('an error occured while reading the data type from the cache', 0, $e));
123
                $type = null;
124
            }
125
126 1
            if (null !== $type && $success) {
127 1
                $this->type = $type;
128
            }
129
        }
130
131 1
        return $this->type;
132
    }
133
134
    /**
135
     * Get an item.
136
     *
137
     * @param string $cacheId
138
     * @param bool   $withVersion
139
     * @param bool   $success
140
     *
141
     * @throws \Psr\SimpleCache\InvalidArgumentException
142
     *
143
     * @return mixed Data on success, null on failure
144
     */
145 3
    public function getItem(string $cacheId, bool $withVersion = true, ?bool &$success = null)
146
    {
147 3
        if ($withVersion) {
148
            $cacheId .= '.' . $this->getVersion();
149
        }
150
151 3
        if (! $this->cache->has($cacheId)) {
152 3
            $success = false;
153
154 3
            return null;
155
        }
156
157 3
        $data = $this->cache->get($cacheId);
158
159 3
        if (! is_array($data) || ! array_key_exists('content', $data)) {
160
            $success = false;
161
162
            return null;
163
        }
164
165 3
        $success = true;
166
167 3
        return unserialize($data['content']);
168
    }
169
170
    /**
171
     * save the content into an php file
172
     *
173
     * @param string $cacheId     The cache id
174
     * @param mixed  $content     The content to store
175
     * @param bool   $withVersion
176
     *
177
     * @throws \Psr\SimpleCache\InvalidArgumentException
178
     *
179
     * @return bool whether the file was correctly written to the disk
180
     */
181 3
    public function setItem(string $cacheId, $content, bool $withVersion = true) : bool
182
    {
183
        // Get the whole PHP code
184
        $data = [
185 3
            'content' => serialize($content),
186
        ];
187
188 3
        if ($withVersion) {
189
            $cacheId .= '.' . $this->getVersion();
190
        }
191
192
        // Save and return
193 3
        return $this->cache->set($cacheId, $data);
194
    }
195
196
    /**
197
     * Test if an item exists.
198
     *
199
     * @param string $cacheId
200
     * @param bool   $withVersion
201
     *
202
     * @throws \Psr\SimpleCache\InvalidArgumentException
203
     *
204
     * @return bool
205
     */
206
    public function hasItem(string $cacheId, bool $withVersion = true) : bool
207
    {
208
        if ($withVersion) {
209
            $cacheId .= '.' . $this->getVersion();
210
        }
211
212
        return $this->cache->has($cacheId);
213
    }
214
215
    /**
216
     * Remove an item.
217
     *
218
     * @param string $cacheId
219
     * @param bool   $withVersion
220
     *
221
     * @throws \Psr\SimpleCache\InvalidArgumentException
222
     *
223
     * @return bool
224
     */
225
    public function removeItem(string $cacheId, bool $withVersion = true) : bool
226
    {
227
        if ($withVersion) {
228
            $cacheId .= '.' . $this->getVersion();
229
        }
230
231
        return $this->cache->delete($cacheId);
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