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

UAParser::__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 UAParser\Parser;
5
use UserAgentParser\Exception;
6
use UserAgentParser\Model;
7
8
class UAParser 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'    => true,
30
            'type'     => false,
31
            'isMobile' => false,
32
            'isTouch'  => false,
33
        ],
34
35
        'bot' => [
36
            'isBot' => true,
37
            'name'  => true,
38
            'type'  => false,
39
        ],
40
    ];
41
42
    protected $defaultValues = [
43
        'Other',
44
    ];
45
46
    private $parser;
47
48 1
    public function __construct()
49
    {
50 1 View Code Duplication
        if (! class_exists('UAParser\Parser', 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...
51
            throw new Exception\PackageNotLoaded('You need to install ' . $this->getComposerPackageName() . ' to use this provider');
52
        }
53 2
    }
54
55 2
    public function getName()
56
    {
57
        return 'UAParser';
58
    }
59
60
    public function getComposerPackageName()
61
    {
62 6
        return 'ua-parser/uap-php';
63
    }
64 6
65 6
    /**
66
     *
67
     * @param Parser $parser
68
     */
69
    public function setParser(Parser $parser = null)
70
    {
71 6
        $this->parser = $parser;
72
    }
73 6
74 6
    /**
75
     *
76
     * @return Parser
77 1
     */
78
    public function getParser()
79 1
    {
80
        if ($this->parser !== null) {
81
            return $this->parser;
82
        }
83
84
        $this->parser = Parser::create();
85
86
        return $this->parser;
87
    }
88 5
89
    /**
90 5
     *
91 2
     * @param \UAParser\Result\Client $resultRaw
92
     *
93
     * @return bool
94 3
     */
95 1
    private function hasResult(\UAParser\Result\Client $resultRaw)
96
    {
97
        if ($this->isRealResult($resultRaw->ua->family)) {
98 2
            return true;
99 1
        }
100
101
        if ($this->isRealResult($resultRaw->os->family)) {
102 1
            return true;
103
        }
104
105 3
        if ($this->isRealResult($resultRaw->device->model)) {
106
            return true;
107
        }
108 3
109 3
        return false;
110 3
    }
111 3
112
    private function getDeviceModelDefaultValues()
113
    {
114 3
        return [
115
            'Feature Phone',
116
            'iOS-Device',
117 3
            'Smartphone',
118 3
        ];
119 3
    }
120 3
121
    private function getDeviceBrandDefaultValues()
122
    {
123
        return [
124
            'Generic',
125
            'Generic_Android',
126
            'Generic_Inettv',
127
        ];
128
    }
129 4
130
    /**
131 4
     *
132 1
     * @param \UAParser\Result\Client $resultRaw
133
     *
134
     * @return bool
135 3
     */
136
    private function isBot(\UAParser\Result\Client $resultRaw)
137
    {
138
        if ($resultRaw->device->family === 'Spider') {
139
            return true;
140
        }
141
142
        return false;
143 1
    }
144
145 1
    /**
146
     *
147 1
     * @param Model\Bot               $bot
148 1
     * @param \UAParser\Result\Client $resultRaw
149
     */
150 1
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
151
    {
152
        $bot->setIsBot(true);
153
154
        if ($this->isRealResult($resultRaw->ua->family) === true) {
155
            $bot->setName($resultRaw->ua->family);
156
        }
157 3
    }
158
159 3
    /**
160 1
     *
161
     * @param Model\Browser              $browser
162
     * @param \UAParser\Result\UserAgent $uaRaw
163 3
     */
164 1 View Code Duplication
    private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw)
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...
165
    {
166
        if ($this->isRealResult($uaRaw->family) === true) {
167 3
            $browser->setName($uaRaw->family);
168 1
        }
169
170
        if ($this->isRealResult($uaRaw->major) === true) {
171 3
            $browser->getVersion()->setMajor($uaRaw->major);
172 1
        }
173
174 3
        if ($this->isRealResult($uaRaw->minor) === true) {
175
            $browser->getVersion()->setMinor($uaRaw->minor);
176
        }
177
178
        if ($this->isRealResult($uaRaw->patch) === true) {
179
            $browser->getVersion()->setPatch($uaRaw->patch);
180
        }
181 3
    }
182
183 3
    /**
184 1
     *
185
     * @param Model\OperatingSystem            $os
186
     * @param \UAParser\Result\OperatingSystem $osRaw
187 3
     */
188 1 View Code Duplication
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $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...
189
    {
190
        if ($this->isRealResult($osRaw->family) === true) {
191 3
            $os->setName($osRaw->family);
192 1
        }
193
194
        if ($this->isRealResult($osRaw->major) === true) {
195 3
            $os->getVersion()->setMajor($osRaw->major);
196 1
        }
197
198 3
        if ($this->isRealResult($osRaw->minor) === true) {
199
            $os->getVersion()->setMinor($osRaw->minor);
200
        }
201
202
        if ($this->isRealResult($osRaw->patch) === true) {
203
            $os->getVersion()->setPatch($osRaw->patch);
204
        }
205 3
    }
206
207 3
    /**
208 1
     *
209
     * @param Model\UserAgent         $device
210
     * @param \UAParser\Result\Device $deviceRaw
211 3
     */
212 1
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw)
213
    {
214 3
        if ($this->isRealResult($deviceRaw->model, $this->getDeviceModelDefaultValues()) === true) {
215
            $device->setModel($deviceRaw->model);
216 5
        }
217
218 5
        if ($this->isRealResult($deviceRaw->brand, $this->getDeviceBrandDefaultValues()) === true) {
219
            $device->setBrand($deviceRaw->brand);
220
        }
221 5
    }
222
223
    public function parse($userAgent, array $headers = [])
224
    {
225
        $parser = $this->getParser();
226 5
227 1
        /* @var $resultRaw \UAParser\Result\Client */
228
        $resultRaw = $parser->parse($userAgent);
229
230
        /*
231
         * No result found?
232
         */
233 4
        if ($this->hasResult($resultRaw) !== true) {
234 4
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
235
        }
236
237
        /*
238
         * Hydrate the model
239 4
         */
240 1
        $result = new Model\UserAgent();
241
        $result->setProviderResultRaw($resultRaw);
242 1
243
        /*
244
         * Bot detection
245
         */
246
        if ($this->isBot($resultRaw) === true) {
247
            $this->hydrateBot($result->getBot(), $resultRaw);
248 3
249
            return $result;
250 3
        }
251 3
252
        /*
253 3
         * hydrate the result
254
         */
255
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->ua);
256
        // renderingEngine not available
257
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os);
258
        $this->hydrateDevice($result->getDevice(), $resultRaw->device);
259
260
        return $result;
261
    }
262
}
263