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
Pull Request — master (#42)
by Martin
10:53 queued 03:22
created

SinergiBrowserDetector::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 3
Ratio 50 %

Code Coverage

Tests 3
CRAP Score 2

Importance

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