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
04:00
created

SinergiBrowserDetector::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 3
Ratio 50 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 3
loc 6
ccs 4
cts 4
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 11
    public function __construct()
68
    {
69 11 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 1
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
71
        }
72 10
    }
73
74 1
    public function getName()
75
    {
76 1
        return 'SinergiBrowserDetector';
77
    }
78
79 3
    public function getComposerPackageName()
80
    {
81 3
        return 'sinergi/browser-detector';
82
    }
83
84
    /**
85
     *
86
     * @param  string                  $userAgent
87
     * @return BrowserDetector\Browser
88
     */
89 6
    public function getBrowserParser($userAgent)
90
    {
91 6
        if ($this->browserParser !== null) {
92 5
            return $this->browserParser;
93
        }
94
95 1
        return new BrowserDetector\Browser($userAgent);
96
    }
97
98
    /**
99
     *
100
     * @param  string             $userAgent
101
     * @return BrowserDetector\Os
102
     */
103 6
    public function getOperatingSystemParser($userAgent)
104
    {
105 6
        if ($this->osParser !== null) {
106 5
            return $this->osParser;
107
        }
108
109 1
        return new BrowserDetector\Os($userAgent);
110
    }
111
112
    /**
113
     *
114
     * @param  string                 $userAgent
115
     * @return BrowserDetector\Device
116
     */
117 6
    public function getDeviceParser($userAgent)
118
    {
119 6
        if ($this->deviceParser !== null) {
120 5
            return $this->deviceParser;
121
        }
122
123 1
        return new BrowserDetector\Device($userAgent);
124
    }
125
126
    /**
127
     *
128
     * @param BrowserDetector\Browser $browserRaw
129
     * @param BrowserDetector\Os      $osRaw
130
     * @param BrowserDetector\Device  $deviceRaw
131
     *
132
     * @return boolean
133
     */
134 5
    private function hasResult(BrowserDetector\Browser $browserRaw, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
135
    {
136 5
        if ($this->isRealResult($browserRaw->getName())) {
137 1
            return true;
138
        }
139
140 4
        if ($this->isRealResult($osRaw->getName())) {
141 1
            return true;
142
        }
143
144 3
        if ($this->isRealResult($deviceRaw->getName())) {
145 1
            return true;
146
        }
147
148 2
        if ($browserRaw->isRobot() === true) {
149 1
            return true;
150
        }
151
152 1
        return false;
153
    }
154
155
    /**
156
     *
157
     * @param Model\Browser           $browser
158
     * @param BrowserDetector\Browser $browserRaw
159
     */
160 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...
161
    {
162 3
        if ($this->isRealResult($browserRaw->getName()) === true) {
163 1
            $browser->setName($browserRaw->getName());
164
        }
165
166 3
        if ($this->isRealResult($browserRaw->getVersion()) === true) {
167 1
            $browser->getVersion()->setComplete($browserRaw->getVersion());
168
        }
169 3
    }
170
171
    /**
172
     *
173
     * @param Model\OperatingSystem $os
174
     * @param BrowserDetector\Os    $osRaw
175
     */
176 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...
177
    {
178 3
        if ($this->isRealResult($osRaw->getName()) === true) {
179 1
            $os->setName($osRaw->getName());
180
        }
181
182 3
        if ($this->isRealResult($osRaw->getVersion()) === true) {
183 1
            $os->getVersion()->setComplete($osRaw->getVersion());
184
        }
185 3
    }
186
187
    /**
188
     *
189
     * @param Model\UserAgent        $device
190
     * @param BrowserDetector\Os     $osRaw
191
     * @param BrowserDetector\Device $deviceRaw
192
     */
193 3
    private function hydrateDevice(Model\Device $device, BrowserDetector\Os $osRaw, BrowserDetector\Device $deviceRaw)
194
    {
195 3
        if ($this->isRealResult($deviceRaw->getName()) === true) {
196 1
            $device->setModel($deviceRaw->getName());
197
        }
198
199 3
        if ($osRaw->isMobile() === true) {
200 1
            $device->setIsMobile(true);
201
        }
202 3
    }
203
204 5
    public function parse($userAgent, array $headers = [])
205
    {
206 5
        $browserRaw = $this->getBrowserParser($userAgent);
207 5
        $osRaw      = $this->getOperatingSystemParser($userAgent);
208 5
        $deviceRaw  = $this->getDeviceParser($userAgent);
209
210
        /*
211
         * No result found?
212
         */
213 5
        if ($this->hasResult($browserRaw, $osRaw, $deviceRaw) !== true) {
214 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
215
        }
216
217
        /*
218
         * Hydrate the model
219
         */
220 4
        $result = new Model\UserAgent();
221 4
        $result->setProviderResultRaw([
222 4
            'browser'         => $browserRaw,
223 4
            'operatingSystem' => $osRaw,
224 4
            'device'          => $deviceRaw,
225 4
        ]);
226
227
        /*
228
         * Bot detection
229
         */
230 4
        if ($browserRaw->isRobot() === true) {
231 1
            $bot = $result->getBot();
232 1
            $bot->setIsBot(true);
233
234 1
            return $result;
235
        }
236
237
        /*
238
         * hydrate the result
239
         */
240 3
        $this->hydrateBrowser($result->getBrowser(), $browserRaw);
241
        // renderingEngine not available
242 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $osRaw);
243 3
        $this->hydrateDevice($result->getDevice(), $osRaw, $deviceRaw);
244
245 3
        return $result;
246
    }
247
}
248