Completed
Push — master ( 301f3c...656cd4 )
by James
03:43
created

Converter::getFilesystem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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