Completed
Pull Request — master (#186)
by Jay
05:38
created

Ini   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 46.67%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 92
ccs 14
cts 30
cp 0.4667
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C getBrowser() 0 48 8
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\Parser;
31
32
use BrowscapPHP\Formatter\FormatterInterface;
33
use BrowscapPHP\Parser\Helper\GetDataInterface;
34
use BrowscapPHP\Parser\Helper\GetPatternInterface;
35
36
/**
37
 * Ini parser class (compatible with PHP 5.3+)
38
 *
39
 * @category   Browscap-PHP
40
 * @author     Christoph Ziegenberg <[email protected]>
41
 * @author     Thomas Müller <[email protected]>
42
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
43
 * @version    3.0
44
 * @license    http://www.opensource.org/licenses/MIT MIT License
45
 * @link       https://github.com/browscap/browscap-php/
46
 */
47
class Ini implements ParserInterface
48
{
49
    /**
50
     * @var Helper\GetPatternInterface
51
     */
52
    private $patternHelper = null;
53
54
    /**
55
     * @var Helper\GetDataInterface
56
     */
57
    private $dataHelper = null;
58
59
    /**
60
     * Formatter to use
61
     *
62
     * @var \BrowscapPHP\Formatter\FormatterInterface
63
     */
64
    private $formatter = null;
65
66
    /**
67
     * class constructor
68
     *
69
     * @param \BrowscapPHP\Parser\Helper\GetPatternInterface $patternHelper
70
     * @param \BrowscapPHP\Parser\Helper\GetDataInterface    $dataHelper
71
     * @param \BrowscapPHP\Formatter\FormatterInterface      $formatter
72
     */
73 2
    public function __construct(
74
        GetPatternInterface $patternHelper,
75
        GetDataInterface $dataHelper,
76
        FormatterInterface $formatter
77
    ) {
78 2
        $this->patternHelper = $patternHelper;
79 2
        $this->dataHelper    = $dataHelper;
80 2
        $this->formatter     = $formatter;
81 2
    }
82
83
    /**
84
     * Gets the browser data formatr for the given user agent
85
     * (or null if no data avaailble, no even the default browser)
86
     *
87
     * @param  string                  $userAgent
88
     * @return FormatterInterface|null
89
     */
90 1
    public function getBrowser($userAgent)
91
    {
92 1
        $userAgent = strtolower($userAgent);
93 1
        $formatter = null;
94
95 1
        foreach ($this->patternHelper->getPatterns($userAgent) as $patterns) {
96 1
            $patternToMatch = '/^(?:' . str_replace("\t", ')|(?:', $patterns) . ')$/i';
97
98 1
            if (!preg_match($patternToMatch, $userAgent)) {
99
                continue;
100
            }
101
102
            // strtok() requires less memory than explode()
103 1
            $pattern = strtok($patterns, "\t");
104
105 1
            while ($pattern !== false) {
106
                $pattern       = str_replace('[\d]', '(\d)', $pattern);
107
                $quotedPattern = '/^' . $pattern . '$/i';
108
                $matches       = [];
109
110
                if (preg_match($quotedPattern, $userAgent, $matches)) {
111
                    // Insert the digits back into the pattern, so that we can search the settings for it
112
                    if (count($matches) > 1) {
113
                        array_shift($matches);
114
                        foreach ($matches as $oneMatch) {
115
                            $numPos  = strpos($pattern, '(\d)');
116
                            $pattern = substr_replace($pattern, $oneMatch, $numPos, 4);
117
                        }
118
                    }
119
120
                    // Try to get settings - as digits have been replaced to speed up the pattern search (up to 90 faster),
121
                    // we won't always find the data in the first step - so check if settings have been found and if not,
122
                    // search for the next pattern.
123
                    $settings = $this->dataHelper->getSettings($pattern);
124
125
                    if (count($settings) > 0) {
126
                        $formatter = $this->formatter;
127
                        $formatter->setData($settings);
128
                        break 2;
129
                    }
130
                }
131
132
                $pattern = strtok("\t");
133
            }
134
        }
135
136 1
        return $formatter;
137
    }
138
}
139