Completed
Pull Request — master (#221)
by Thomas
28:24 queued 26:59
created

BrowscapCache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * This file is part of the browscap-php package.
4
 *
5
 * Copyright (c) 1998-2017, Browser Capabilities Project
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types = 1);
12
namespace BrowscapPHP\Cache;
13
14
use Psr\Log\LoggerInterface;
15
use Psr\SimpleCache\CacheInterface;
16
use Psr\SimpleCache\InvalidArgumentException;
17
18
/**
19
 * A cache proxy to be able to use the cache adapters provided by the WurflCache package
20
 */
21
final class BrowscapCache implements BrowscapCacheInterface
22
{
23
    /**
24
     * Path to the cache directory
25
     *
26
     * @var \Psr\SimpleCache\CacheInterface
27
     */
28
    private $cache = null;
29
30
    /**
31
     * @var \Psr\Log\LoggerInterface|null
32
     */
33
    private $logger;
34
35
    /**
36
     * Detected browscap version (read from INI file)
37
     *
38
     * @var int
39
     */
40
    private $version = null;
41
42
    /**
43
     * Release date of the Browscap data (read from INI file)
44
     *
45
     * @var string
46
     */
47
    private $releaseDate;
48
49
    /**
50
     * Type of the Browscap data (read from INI file)
51
     *
52
     * @var string
53
     */
54
    private $type;
55
56
    /**
57
     * Constructor class, checks for the existence of (and loads) the cache and
58
     * if needed updated the definitions
59
     *
60
     * @param \Psr\SimpleCache\CacheInterface $adapter
61
     * @param LoggerInterface                 $logger
62
     */
63 4
    public function __construct(CacheInterface $adapter, LoggerInterface $logger)
64
    {
65 4
        $this->cache = $adapter;
66 4
        $this->logger = $logger;
67 4
    }
68
69
    /**
70
     * Gets the version of the Browscap data
71
     *
72
     * @return int
73
     */
74 2
    public function getVersion() : ?int
75
    {
76 2
        if ($this->version === null) {
77 2
            $success = null;
78
79
            try {
80 2
                $version = $this->getItem('browscap.version', false, $success);
81
            } catch (InvalidArgumentException $e) {
82
                $this->logger->error(new \InvalidArgumentException('an error occured while reading the data version from the cache', 0, $e));
83
                $version = null;
84
            }
85
86 2
            if ($version !== null && $success) {
87 1
                $this->version = (int) $version;
88
            }
89
        }
90
91 2
        return $this->version;
92
    }
93
94
    /**
95
     * Gets the release date of the Browscap data
96
     *
97
     * @return string|null
98
     */
99 1
    public function getReleaseDate() : ?string
100
    {
101 1
        if ($this->releaseDate === null) {
102 1
            $success = null;
103
104
            try {
105 1
                $releaseDate = $this->getItem('browscap.releaseDate', false, $success);
106
            } catch (InvalidArgumentException $e) {
107
                $this->logger->error(new \InvalidArgumentException('an error occured while reading the data release date from the cache', 0, $e));
108
                $releaseDate = null;
109
            }
110
111 1
            if ($releaseDate !== null && $success) {
112 1
                $this->releaseDate = $releaseDate;
113
            }
114
        }
115
116 1
        return $this->releaseDate;
117
    }
118
119
    /**
120
     * Gets the type of the Browscap data
121
     */
122 1
    public function getType() : ?string
123
    {
124 1
        if ($this->type === null) {
125 1
            $success = null;
126
127
            try {
128 1
                $type = $this->getItem('browscap.type', false, $success);
129
            } catch (InvalidArgumentException $e) {
130
                $this->logger->error(new \InvalidArgumentException('an error occured while reading the data type from the cache', 0, $e));
131
                $type = null;
132
            }
133
134 1
            if ($type !== null && $success) {
135 1
                $this->type = $type;
136
            }
137
        }
138
139 1
        return $this->type;
140
    }
141
142
    /**
143
     * Get an item.
144
     *
145
     * @param string $cacheId
146
     * @param bool   $withVersion
147
     * @param bool   &$success
148
     *
149
     * @throws \Psr\SimpleCache\InvalidArgumentException
150
     *
151
     * @return mixed Data on success, null on failure
152
     */
153 3
    public function getItem(string $cacheId, bool $withVersion = true, ?bool &$success = null)
154
    {
155 3
        if ($withVersion) {
156
            $cacheId .= '.' . $this->getVersion();
157
        }
158
159 3
        if (! $this->cache->has($cacheId)) {
160 3
            $success = false;
161
162 3
            return null;
163
        }
164
165 3
        $data = $this->cache->get($cacheId);
166
167 3
        if (! is_array($data) || ! array_key_exists('content', $data)) {
168
            $success = false;
169
170
            return null;
171
        }
172
173 3
        $success = true;
174
175 3
        return unserialize($data['content']);
176
    }
177
178
    /**
179
     * save the content into an php file
180
     *
181
     * @param string $cacheId     The cache id
182
     * @param mixed  $content     The content to store
183
     * @param bool   $withVersion
184
     *
185
     * @throws \Psr\SimpleCache\InvalidArgumentException
186
     *
187
     * @return bool whether the file was correctly written to the disk
188
     */
189 3
    public function setItem(string $cacheId, $content, bool $withVersion = true) : bool
190
    {
191
        // Get the whole PHP code
192
        $data = [
193 3
            'content' => serialize($content),
194
        ];
195
196 3
        if ($withVersion) {
197
            $cacheId .= '.' . $this->getVersion();
198
        }
199
200
        // Save and return
201 3
        return $this->cache->set($cacheId, $data);
202
    }
203
204
    /**
205
     * Test if an item exists.
206
     *
207
     * @param string $cacheId
208
     * @param bool   $withVersion
209
     *
210
     * @throws \Psr\SimpleCache\InvalidArgumentException
211
     *
212
     * @return bool
213
     */
214
    public function hasItem(string $cacheId, bool $withVersion = true) : bool
215
    {
216
        if ($withVersion) {
217
            $cacheId .= '.' . $this->getVersion();
218
        }
219
220
        return $this->cache->has($cacheId);
221
    }
222
223
    /**
224
     * Remove an item.
225
     *
226
     * @param string $cacheId
227
     * @param bool   $withVersion
228
     *
229
     * @throws \Psr\SimpleCache\InvalidArgumentException
230
     *
231
     * @return bool
232
     */
233
    public function removeItem(string $cacheId, bool $withVersion = true) : bool
234
    {
235
        if ($withVersion) {
236
            $cacheId .= '.' . $this->getVersion();
237
        }
238
239
        return $this->cache->delete($cacheId);
240
    }
241
242
    /**
243
     * Flush the whole storage
244
     *
245
     * @return bool
246
     */
247
    public function flush() : bool
248
    {
249
        return $this->cache->clear();
250
    }
251
}
252