1 | <?php |
||
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 |
|
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) |
|
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 |
|
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 |
||
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 |
||
241 | |||
242 | /** |
||
243 | * Flush the whole storage |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function flush() : bool |
||
251 | } |
||
252 |