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

Converter::getIniReleaseDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3.072
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\Helper;
13
14
use BrowscapPHP\Cache\BrowscapCacheInterface;
15
use BrowscapPHP\Exception\FileNotFoundException;
16
use BrowscapPHP\IniParser\IniParser;
17
use Psr\Log\LoggerInterface;
18
use Psr\SimpleCache\InvalidArgumentException;
19
20
/**
21
 * patternHelper to convert the ini data, parses the data and stores them into the cache
22
 */
23
final class Converter implements ConverterInterface
24
{
25
    /**
26
     * The key to search for in the INI file to find the browscap settings
27
     */
28
    const BROWSCAP_VERSION_KEY = 'GJK_Browscap_Version';
29
30
    /**
31
     * @var \Psr\Log\LoggerInterface
32
     */
33
    private $logger = null;
34
35
    /**
36
     * The cache instance
37
     *
38
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
39
     */
40
    private $cache = null;
41
42
    /**
43
     * a filesystem patternHelper instance
44
     *
45
     * @var Filesystem
46
     */
47
    private $filessystem = null;
48
49
    /**
50
     * version of the ini file
51
     *
52
     * @var int
53
     */
54
    private $iniVersion = 0;
55
56
    /**
57
     * Converter constructor.
58
     *
59
     * @param LoggerInterface        $logger
60
     * @param BrowscapCacheInterface $cache
61
     */
62 6
    public function __construct(LoggerInterface $logger, BrowscapCacheInterface $cache)
63
    {
64 6
        $this->logger = $logger;
65 6
        $this->cache = $cache;
66 6
    }
67
68
    /**
69
     * Sets a filesystem instance
70
     *
71
     * @param Filesystem $file
72
     */
73 6
    public function setFilesystem(Filesystem $file) : void
74
    {
75 6
        $this->filessystem = $file;
76 6
    }
77
78
    /**
79
     * Returns a filesystem instance
80
     *
81
     * @return Filesystem
82
     */
83 3
    public function getFilesystem() : Filesystem
84
    {
85 3
        if (null === $this->filessystem) {
86 1
            $this->filessystem = new Filesystem();
87
        }
88
89 3
        return $this->filessystem;
90
    }
91
92
    /**
93
     * converts a file
94
     *
95
     * @param string $iniFile
96
     *
97
     * @throws FileNotFoundException
98
     */
99 2
    public function convertFile(string $iniFile) : void
100
    {
101 2
        if (! $this->getFilesystem()->exists($iniFile)) {
102 2
            throw FileNotFoundException::fileNotFound($iniFile);
103
        }
104
105
        $this->logger->info('start reading file');
106
107
        $iniString = file_get_contents($iniFile);
108
109
        $this->logger->info('finished reading file');
110
111
        $this->convertString($iniString);
112
    }
113
114
    /**
115
     * converts the string content
116
     *
117
     * @param string $iniString
118
     */
119 2
    public function convertString(string $iniString) : void
120
    {
121 2
        $iniParser = new IniParser();
122
123 2
        $this->logger->info('start creating patterns from the ini data');
124
125 2
        foreach ($iniParser->createPatterns($iniString) as $subkey => $content) {
126 2
            if ('' === $subkey) {
127
                continue;
128
            }
129
130
            try {
131 2
                if (! $this->cache->setItem('browscap.patterns.' . $subkey, $content, true)) {
132
                    $this->logger->error('could not write pattern data "' . $subkey . '" to the cache');
133
                }
134
            } catch (InvalidArgumentException $e) {
135
                $this->logger->error(new \InvalidArgumentException('an error occured while writing pattern data into the cache', 0, $e));
136
            }
137
        }
138
139 2
        $this->logger->info('finished creating patterns from the ini data');
140
141 2
        $this->logger->info('start creating data from the ini data');
142
143 2
        foreach ($iniParser->createIniParts($iniString) as $subkey => $content) {
144 2
            if ('' === $subkey) {
145
                continue;
146
            }
147
148
            try {
149 2
                if (! $this->cache->setItem('browscap.iniparts.' . $subkey, $content, true)) {
150
                    $this->logger->error('could not write property data "' . $subkey . '" to the cache');
151
                }
152
            } catch (InvalidArgumentException $e) {
153
                $this->logger->error(new \InvalidArgumentException('an error occured while writing property data into the cache', 0, $e));
154
            }
155
        }
156
157
        try {
158 2
            $this->cache->setItem('browscap.releaseDate', $this->getIniReleaseDate($iniString), false);
159
        } catch (InvalidArgumentException $e) {
160
            $this->logger->error(new \InvalidArgumentException('an error occured while writing data release date into the cache', 0, $e));
161
        }
162
163
        try {
164 2
            $this->cache->setItem('browscap.type', $this->getIniType($iniString), false);
165
        } catch (InvalidArgumentException $e) {
166
            $this->logger->error(new \InvalidArgumentException('an error occured while writing the data type into the cache', 0, $e));
167
        }
168
169 2
        $this->logger->info('finished creating data from the ini data');
170 2
    }
171
172
    /**
173
     * Parses the ini data to get the version of loaded ini file
174
     *
175
     * @param string $iniString The loaded ini data
176
     *
177
     * @return int
178
     */
179 1
    public function getIniVersion(string $iniString) : int
180
    {
181 1
        $quoterHelper = new Quoter();
182 1
        $key = $quoterHelper->pregQuote(self::BROWSCAP_VERSION_KEY);
183
184 1
        if (preg_match('/\.*\[' . $key . '\][^\[]*Version=(\d+)\D.*/', $iniString, $matches)) {
185 1
            if (isset($matches[1])) {
186 1
                $this->iniVersion = (int) $matches[1];
187
            }
188
        }
189
190 1
        return $this->iniVersion;
191
    }
192
193
    /**
194
     * sets the version
195
     *
196
     * @param int $version
197
     */
198
    public function setVersion(int $version) : void
199
    {
200
        $this->iniVersion = $version;
201
    }
202
203
    /**
204
     * stores the version of the ini file into cache
205
     */
206
    public function storeVersion() : void
207
    {
208
        try {
209
            $this->cache->setItem('browscap.version', $this->iniVersion, false);
210
        } catch (InvalidArgumentException $e) {
211
            $this->logger->error(new \InvalidArgumentException('an error occured while writing the data version into the cache', 0, $e));
212
        }
213
    }
214
215
    /**
216
     * Parses the ini data to get the releaseDate of loaded ini file
217
     *
218
     * @param string $iniString The loaded ini data
219
     *
220
     * @return string|null
221
     */
222 2
    private function getIniReleaseDate(string $iniString) : ?string
223
    {
224 2
        if (preg_match('/Released=(.*)/', $iniString, $matches)) {
225 2
            if (isset($matches[1])) {
226 2
                return $matches[1];
227
            }
228
        }
229
230
        return null;
231
    }
232
233
    /**
234
     * Parses the ini data to get the releaseDate of loaded ini file
235
     *
236
     * @param string $iniString The loaded ini data
237
     *
238
     * @return string|null
239
     */
240 2
    private function getIniType(string $iniString) : ?string
241
    {
242 2
        if (preg_match('/Type=(.*)/', $iniString, $matches)) {
243 2
            if (isset($matches[1])) {
244 2
                return $matches[1];
245
            }
246
        }
247
248
        return null;
249
    }
250
}
251