GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

SinergiBrowserDetector   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 11
dl 0
loc 250
ccs 57
cts 57
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBrowserParser() 0 8 2
A getOperatingSystemParser() 0 8 2
A getDeviceParser() 0 8 2
A __construct() 0 4 1
B hasResult() 0 20 5
A hydrateBrowser() 0 5 1
A hydrateOperatingSystem() 0 5 1
A hydrateDevice() 0 8 2
B parse() 0 43 3
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use Sinergi\BrowserDetector;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for sinergi/browser-detector
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 * @see https://github.com/sinergi/php-browser-detector
15
 */
16
class SinergiBrowserDetector extends AbstractProvider
17
{
18
    /**
19
     * Name of the provider
20
     *
21
     * @var string
22
     */
23
    protected $name = 'SinergiBrowserDetector';
24
25
    /**
26
     * Homepage of the provider
27
     *
28
     * @var string
29
     */
30
    protected $homepage = 'https://github.com/sinergi/php-browser-detector';
31
32
    /**
33
     * Composer package name
34
     *
35
     * @var string
36
     */
37
    protected $packageName = 'sinergi/browser-detector';
38
39
    protected $detectionCapabilities = [
40
41
        'browser' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'renderingEngine' => [
47
            'name'    => false,
48
            'version' => false,
49
        ],
50
51
        'operatingSystem' => [
52
            'name'    => true,
53
            'version' => true,
54
        ],
55
56
        'device' => [
57
            'model'    => true,
58
            'brand'    => false,
59
            'type'     => false,
60
            'isMobile' => true,
61
            'isTouch'  => false,
62
        ],
63
64
        'bot' => [
65
            'isBot' => true,
66
            'name'  => false,
67
            'type'  => false,
68
        ],
69
    ];
70
71
    protected $defaultValues = [
72
73
        'general' => [
74
            '/^unknown$/i',
75
        ],
76
77
        'device' => [
78
            'model' => [
79
                '/^Windows Phone$/i',
80
            ],
81
        ],
82
    ];
83
84
    /**
85
     * Used for unitTests mocking
86
     *
87
     * @var BrowserDetector\Browser
88
     */
89
    private $browserParser;
90
91
    /**
92
     * Used for unitTests mocking
93
     *
94
     * @var BrowserDetector\Os
95
     */
96
    private $osParser;
97
98
    /**
99
     * Used for unitTests mocking
100
     *
101
     * @var BrowserDetector\Device
102
     */
103
    private $deviceParser;
104
105
    /**
106
     *
107
     * @throws PackageNotLoadedException
108
     */
109 17
    public function __construct()
110
    {
111 17
        $this->checkIfInstalled();
112 17
    }
113
114
    /**
115
     *
116
     * @param  string                  $userAgent
117
     * @return BrowserDetector\Browser
118
     */
119 10
    public function getBrowserParser($userAgent)
120
    {
121 10
        if ($this->browserParser !== null) {
122 9
            return $this->browserParser;
123
        }
124
125 1
        return new BrowserDetector\Browser($userAgent);
126
    }
127
128
    /**
129
     *
130
     * @param  string             $userAgent
131
     * @return BrowserDetector\Os
132
     */
133 10
    public function getOperatingSystemParser($userAgent)
134
    {
135 10
        if ($this->osParser !== null) {
136 9
            return $this->osParser;
137
        }
138
139 1
        return new BrowserDetector\Os($userAgent);
140
    }
141
142
    /**
143
     *
144
     * @param  string                 $userAgent
145
     * @return BrowserDetector\Device
146
     */
147 10
    public function getDeviceParser($userAgent)
148
    {
149 10
        if ($this->deviceParser !== null) {
150 9
            return $this->deviceParser;
151
        }
152
153 1
        return new BrowserDetector\Device($userAgent);
154
    }
155
156
    /**
157
     *
158
     * @param BrowserDetector\Browser $browserRaw
159
     * @param BrowserDetector\Os      $osRaw
160
     * @param BrowserDetector\Device  $deviceRaw
161
     *
162
     * @return boolean
163
     */
164 9
    private function hasResult(BrowserDetector\Browser $browserRaw, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
165
    {
166 9
        if ($this->isRealResult($browserRaw->getName())) {
167 2
            return true;
168
        }
169
170 7
        if ($this->isRealResult($osRaw->getName())) {
171 1
            return true;
172
        }
173
174 6
        if ($this->isRealResult($deviceRaw->getName(), 'device', 'model')) {
175 1
            return true;
176
        }
177
178 5
        if ($browserRaw->isRobot() === true) {
179 2
            return true;
180
        }
181
182 3
        return false;
183
    }
184
185
    /**
186
     *
187
     * @param Model\Browser           $browser
188
     * @param BrowserDetector\Browser $browserRaw
189
     */
190 4
    private function hydrateBrowser(Model\Browser $browser, BrowserDetector\Browser $browserRaw)
191
    {
192 4
        $browser->setName($this->getRealResult($browserRaw->getName()));
193 4
        $browser->getVersion()->setComplete($this->getRealResult($browserRaw->getVersion()));
194 4
    }
195
196
    /**
197
     *
198
     * @param Model\OperatingSystem $os
199
     * @param BrowserDetector\Os    $osRaw
200
     */
201 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, BrowserDetector\Os $osRaw)
202
    {
203 4
        $os->setName($this->getRealResult($osRaw->getName()));
204 4
        $os->getVersion()->setComplete($this->getRealResult($osRaw->getVersion()));
205 4
    }
206
207
    /**
208
     *
209
     * @param Model\UserAgent        $device
210
     * @param BrowserDetector\Os     $osRaw
211
     * @param BrowserDetector\Device $deviceRaw
212
     */
213 4
    private function hydrateDevice(Model\Device $device, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
214
    {
215 4
        $device->setModel($this->getRealResult($deviceRaw->getName(), 'device', 'model'));
216
217 4
        if ($osRaw->isMobile() === true) {
218 1
            $device->setIsMobile(true);
219
        }
220 4
    }
221
222 9
    public function parse($userAgent, array $headers = [])
223
    {
224 9
        $browserRaw = $this->getBrowserParser($userAgent);
225 9
        $osRaw      = $this->getOperatingSystemParser($userAgent);
226 9
        $deviceRaw  = $this->getDeviceParser($userAgent);
227
228
        /*
229
         * No result found?
230
         */
231 9
        if ($this->hasResult($browserRaw, $osRaw, $deviceRaw) !== true) {
232 3
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
233
        }
234
235
        /*
236
         * Hydrate the model
237
         */
238 6
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
239 6
        $result->setProviderResultRaw([
240 6
            'browser'         => $browserRaw,
241 6
            'operatingSystem' => $osRaw,
242 6
            'device'          => $deviceRaw,
243
        ]);
244
245
        /*
246
         * Bot detection
247
         */
248 6
        if ($browserRaw->isRobot() === true) {
249 2
            $bot = $result->getBot();
250 2
            $bot->setIsBot(true);
251
252 2
            return $result;
253
        }
254
255
        /*
256
         * hydrate the result
257
         */
258 4
        $this->hydrateBrowser($result->getBrowser(), $browserRaw);
259
        // renderingEngine not available
260 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $osRaw);
261 4
        $this->hydrateDevice($result->getDevice(), $osRaw, $deviceRaw);
262
263 4
        return $result;
264
    }
265
}
266