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

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