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.

UAParser::hasResult()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 8.8571
cc 5
eloc 10
nc 5
nop 1
crap 5
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use UAParser\Parser;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
/**
10
 * Abstraction for ua-parser/uap-php
11
 *
12
 * @author Martin Keckeis <[email protected]>
13
 * @license MIT
14
 * @see https://github.com/ua-parser/uap-php
15
 */
16
class UAParser extends AbstractProvider
17
{
18
    /**
19
     * Name of the provider
20
     *
21
     * @var string
22
     */
23
    protected $name = 'UAParser';
24
25
    /**
26
     * Homepage of the provider
27
     *
28
     * @var string
29
     */
30
    protected $homepage = 'https://github.com/ua-parser/uap-php';
31
32
    /**
33
     * Composer package name
34
     *
35
     * @var string
36
     */
37
    protected $packageName = 'ua-parser/uap-php';
38
39
    protected $detectionCapabilities = [
40
41
        'browser' => [
42
            'name'    => true,
43
            'version' => true,
44
        ],
45
46
        'renderingEngine' => [
47
            'name'    => false,
48
            'version' => false,
49
        ],
50
51
        'operatingSystem' => [
52
            'name'    => true,
53
            'version' => true,
54
        ],
55
56
        'device' => [
57
            'model'    => true,
58
            'brand'    => true,
59
            'type'     => false,
60
            'isMobile' => false,
61
            'isTouch'  => false,
62
        ],
63
64
        'bot' => [
65
            'isBot' => true,
66
            'name'  => true,
67
            'type'  => false,
68
        ],
69
    ];
70
71
    protected $defaultValues = [
72
73
        'general' => [
74
            '/^Other$/i',
75
76
        ],
77
78
        'device' => [
79
80
            'brand' => [
81
                '/^Generic/i',
82
                '/^unknown$/i',
83
            ],
84
85
            'model' => [
86
                '/^generic$/i',
87
                '/^Smartphone$/i',
88
                '/^Feature Phone$/i',
89
                '/^iOS-Device$/i',
90
                '/^Tablet$/i',
91
                '/^Touch$/i',
92
                '/^Windows$/i',
93
                '/^Windows Phone$/i',
94
                '/^Android$/i',
95
            ],
96
        ],
97
98
        'bot' => [
99
            'name' => [
100
                '/^Other$/i',
101
                '/^crawler$/i',
102
                '/^robot$/i',
103
                '/^crawl$/i',
104
                '/^Spider$/i',
105
            ],
106
        ],
107
    ];
108
109
    private $parser;
110
111
    /**
112
     *
113
     * @param  Parser                    $parser
114
     * @throws PackageNotLoadedException
115
     */
116 18
    public function __construct(Parser $parser = null)
117
    {
118 18
        if ($parser === null) {
119 8
            $this->checkIfInstalled();
120
        }
121
122 18
        $this->parser = $parser;
123 18
    }
124
125
    /**
126
     *
127
     * @return Parser
128
     */
129 11
    public function getParser()
130
    {
131 11
        if ($this->parser !== null) {
132 11
            return $this->parser;
133
        }
134
135 1
        $this->parser = Parser::create();
136
137 1
        return $this->parser;
138
    }
139
140
    /**
141
     *
142
     * @param \UAParser\Result\Client $resultRaw
143
     *
144
     * @return bool
145
     */
146 10
    private function hasResult(\UAParser\Result\Client $resultRaw)
147
    {
148 10
        if ($this->isBot($resultRaw) === true) {
149 3
            return true;
150
        }
151
152 7
        if ($this->isRealResult($resultRaw->ua->family)) {
153 1
            return true;
154
        }
155
156 6
        if ($this->isRealResult($resultRaw->os->family)) {
157 2
            return true;
158
        }
159
160 4
        if ($this->isRealResult($resultRaw->device->model, 'device', 'model')) {
161 1
            return true;
162
        }
163
164 3
        return false;
165
    }
166
167
    /**
168
     *
169
     * @param \UAParser\Result\Client $resultRaw
170
     *
171
     * @return bool
172
     */
173 10
    private function isBot(\UAParser\Result\Client $resultRaw)
174
    {
175 10
        if ($resultRaw->device->family === 'Spider') {
176 3
            return true;
177
        }
178
179 7
        return false;
180
    }
181
182
    /**
183
     *
184
     * @param Model\Bot               $bot
185
     * @param \UAParser\Result\Client $resultRaw
186
     */
187 3
    private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw)
188
    {
189 3
        $bot->setIsBot(true);
190 3
        $bot->setName($this->getRealResult($resultRaw->ua->family, 'bot', 'name'));
191 3
    }
192
193
    /**
194
     *
195
     * @param Model\Browser              $browser
196
     * @param \UAParser\Result\UserAgent $uaRaw
197
     */
198 4
    private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw)
199
    {
200 4
        $browser->setName($this->getRealResult($uaRaw->family));
201
202 4
        $browser->getVersion()->setMajor($this->getRealResult($uaRaw->major));
203 4
        $browser->getVersion()->setMinor($this->getRealResult($uaRaw->minor));
204 4
        $browser->getVersion()->setPatch($this->getRealResult($uaRaw->patch));
205 4
    }
206
207
    /**
208
     *
209
     * @param Model\OperatingSystem            $os
210
     * @param \UAParser\Result\OperatingSystem $osRaw
211
     */
212 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw)
213
    {
214 4
        $os->setName($this->getRealResult($osRaw->family));
215
216 4
        $os->getVersion()->setMajor($this->getRealResult($osRaw->major));
217 4
        $os->getVersion()->setMinor($this->getRealResult($osRaw->minor));
218 4
        $os->getVersion()->setPatch($this->getRealResult($osRaw->patch));
219 4
    }
220
221
    /**
222
     *
223
     * @param Model\UserAgent         $device
224
     * @param \UAParser\Result\Device $deviceRaw
225
     */
226 4
    private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw)
227
    {
228 4
        $device->setModel($this->getRealResult($deviceRaw->model, 'device', 'model'));
229 4
        $device->setBrand($this->getRealResult($deviceRaw->brand, 'device', 'brand'));
230 4
    }
231
232 10
    public function parse($userAgent, array $headers = [])
233
    {
234 10
        $parser = $this->getParser();
235
236
        /* @var $resultRaw \UAParser\Result\Client */
237 10
        $resultRaw = $parser->parse($userAgent);
238
239
        /*
240
         * No result found?
241
         */
242 10
        if ($this->hasResult($resultRaw) !== true) {
243 3
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
244
        }
245
246
        /*
247
         * Hydrate the model
248
         */
249 7
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
250 7
        $result->setProviderResultRaw($resultRaw);
251
252
        /*
253
         * Bot detection
254
         */
255 7
        if ($this->isBot($resultRaw) === true) {
256 3
            $this->hydrateBot($result->getBot(), $resultRaw);
257
258 3
            return $result;
259
        }
260
261
        /*
262
         * hydrate the result
263
         */
264 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->ua);
265
        // renderingEngine not available
266 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os);
267 4
        $this->hydrateDevice($result->getDevice(), $resultRaw->device);
268
269 4
        return $result;
270
    }
271
}
272