Completed
Pull Request — master (#164)
by Thomas
13:11
created

Converter::getIniVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3
1
<?php
2
/**
3
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @copyright  1998-2015 Browser Capabilities Project
25
 * @license    http://www.opensource.org/licenses/MIT MIT License
26
 * @link       https://github.com/browscap/browscap-php/
27
 * @since      added with version 3.0
28
 */
29
30
namespace BrowscapPHP\Helper;
31
32
use BrowscapPHP\Cache\BrowscapCacheInterface;
33
use BrowscapPHP\Exception\FileNotFoundException;
34
use BrowscapPHP\IniParser\IniParser;
35
use Psr\Log\LoggerInterface;
36
37
/**
38
 * patternHelper to convert the ini data, parses the data and stores them into the cache
39
 *
40
 * @category   Browscap-PHP
41
 * @author     Christoph Ziegenberg <[email protected]>
42
 * @author     Thomas Müller <[email protected]>
43
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
44
 * @version    3.0
45
 * @license    http://www.opensource.org/licenses/MIT MIT License
46
 * @link       https://github.com/browscap/browscap-php/
47
 */
48
class Converter
49
{
50
    /**
51
     * Options for regex patterns.
52
     *
53
     * REGEX_DELIMITER: Delimiter of all the regex patterns in the whole class.
54
     * REGEX_MODIFIERS: Regex modifiers.
55
     */
56
    const REGEX_DELIMITER               = '@';
57
    const REGEX_MODIFIERS               = 'i';
58
    const COMPRESSION_PATTERN_START     = '@';
59
    const COMPRESSION_PATTERN_DELIMITER = '|';
60
61
    /**
62
     * The key to search for in the INI file to find the browscap settings
63
     */
64
    const BROWSCAP_VERSION_KEY = 'GJK_Browscap_Version';
65
66
    /** @var \Psr\Log\LoggerInterface */
67
    private $logger = null;
68
69
    /**
70
     * The cache instance
71
     *
72
     * @var \BrowscapPHP\Cache\BrowscapCacheInterface
73
     */
74
    private $cache = null;
75
76
    /**
77
     * a filesystem patternHelper instance
78
     *
79
     * @var \BrowscapPHP\Helper\Filesystem
80
     */
81
    private $filessystem = null;
82
83
    /**
84
     * version of the ini file
85
     *
86
     * @var int
87
     */
88
    private $iniVersion = 0;
89
90
    /**
91
     * class constructor
92
     *
93
     * @param \Psr\Log\LoggerInterface                  $logger
94
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
95
     */
96 12
    public function __construct(LoggerInterface $logger, BrowscapCacheInterface $cache)
97
    {
98 12
        $this->logger = $logger;
99 12
        $this->cache  = $cache;
100 12
    }
101
102
    /**
103
     * Sets a filesystem instance
104
     *
105
     * @param \BrowscapPHP\Helper\Filesystem $file
106
     *
107
     * @return \BrowscapPHP\Helper\Converter
108
     */
109 6
    public function setFilesystem(Filesystem $file)
110
    {
111 6
        $this->filessystem = $file;
112
113 6
        return $this;
114
    }
115
116
    /**
117
     * Returns a filesystem instance
118
     *
119
     * @return \BrowscapPHP\Helper\Filesystem
120
     */
121 3
    public function getFilesystem()
122
    {
123 3
        if (null === $this->filessystem) {
124 1
            $this->filessystem = new Filesystem();
125
        }
126
127 3
        return $this->filessystem;
128
    }
129
130
    /**
131
     * @param  string                                       $iniFile
132
     * @throws \BrowscapPHP\Exception\FileNotFoundException
133
     */
134 2
    public function convertFile($iniFile)
135
    {
136 2
        if (!$this->getFilesystem()->exists($iniFile)) {
137 2
            throw FileNotFoundException::fileNotFound($iniFile);
138
        }
139
140
        $this->logger->info('start reading file');
141
142
        $iniString = file_get_contents($iniFile);
143
144
        $this->logger->info('finished reading file');
145
146
        $this->convertString($iniString);
147
    }
148
149
    /**
150
     * @param string $iniString
151
     */
152 6
    public function convertString($iniString)
153
    {
154 6
        $iniParser = new IniParser();
155
156 6
        $this->logger->info('start creating patterns from the ini data');
157
158 6 View Code Duplication
        foreach ($iniParser->createPatterns($iniString) as $patternsHashList) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159 6
            foreach ($patternsHashList as $subkey => $content) {
160 6
                if (!$this->cache->setItem('browscap.patterns.' . $subkey, $content, true)) {
161 1
                    $this->logger->error('could not write pattern data "' . $subkey . '" to the cache');
162
                }
163 6
            }
164 6
        }
165
166 6
        $this->logger->info('finished creating patterns from the ini data');
167
168 6
        $this->logger->info('start creating data from the ini data');
169
170 6 View Code Duplication
        foreach ($iniParser->createIniParts($iniString) as $patternsContentList) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171 6
            foreach ($patternsContentList as $subkey => $content) {
172 6
                if (!$this->cache->setItem('browscap.iniparts.' . $subkey, $content, true)) {
173 1
                    $this->logger->error('could not write property data "' . $subkey . '" to the cache');
174
                }
175 6
            }
176 6
        }
177
178 6
        $this->cache->setItem('browscap.releaseDate', $this->getIniReleaseDate($iniString), false);
179 6
        $this->cache->setItem('browscap.type', $this->getIniType($iniString), false);
180
181 6
        $this->logger->info('finished creating data from the ini data');
182 6
    }
183
184
    /**
185
     * Parses the ini data to get the version of loaded ini file
186
     *
187
     * @param string $iniString The loaded ini data
188
     *
189
     * @return int
190
     */
191 7
    public function getIniVersion($iniString)
192
    {
193 7
        $quoterHelper = new Quoter();
194 7
        $key          = $quoterHelper->pregQuote(self::BROWSCAP_VERSION_KEY);
195
196 7
        if (preg_match('/\.*\[' . $key . '\][^\[]*Version=(\d+)\D.*/', $iniString, $matches)) {
197 7
            if (isset($matches[1])) {
198 7
                $this->iniVersion = (int) $matches[1];
199
            }
200
        }
201
202 7
        return $this->iniVersion;
203
    }
204
205
    /**
206
     * sets the version
207
     *
208
     * @param int $version
209
     *
210
     * @return \BrowscapPHP\Helper\Converter
211
     */
212
    public function setVersion($version)
213
    {
214
        $this->iniVersion = $version;
215
216
        return $this;
217
    }
218
219
    /**
220
     * stores the version of the ini file into cache
221
     *
222
     * @return \BrowscapPHP\Helper\Converter
223
     */
224 5
    public function storeVersion()
225
    {
226 5
        $this->cache->setItem('browscap.version', $this->iniVersion, false);
227
228 5
        return $this;
229
    }
230
231
    /**
232
     * Parses the ini data to get the releaseDate of loaded ini file
233
     *
234
     * @param string $iniString The loaded ini data
235
     *
236
     * @return string|null
237
     */
238 6 View Code Duplication
    private function getIniReleaseDate($iniString)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
    {
240 6
        if (preg_match('/Released=(.*)/', $iniString, $matches)) {
241 6
            if (isset($matches[1])) {
242 6
                return $matches[1];
243
            }
244
        }
245
246
        return null;
247
    }
248
249
    /**
250
     * Parses the ini data to get the releaseDate of loaded ini file
251
     *
252
     * @param string $iniString The loaded ini data
253
     *
254
     * @return string|null
255
     */
256 6 View Code Duplication
    private function getIniType($iniString)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
    {
258 6
        if (preg_match('/Type=(.*)/', $iniString, $matches)) {
259 6
            if (isset($matches[1])) {
260 6
                return $matches[1];
261
            }
262
        }
263
264
        return null;
265
    }
266
}
267