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.
Completed
Push — master ( d31fa5...89d544 )
by Martin
04:29
created

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