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
Push — master ( 57cbc1...b45bb0 )
by Martin
10s
created

JenssegersAgent::getParser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
namespace UserAgentParser\Provider;
3
4
use Jenssegers\Agent\Agent;
5
use UserAgentParser\Exception\NoResultFoundException;
6
use UserAgentParser\Exception\PackageNotLoadedException;
7
use UserAgentParser\Model;
8
9
class JenssegersAgent extends AbstractProvider
10
{
11
    /**
12
     * Name of the provider
13
     *
14
     * @var string
15
     */
16
    protected $name = 'JenssegersAgent';
17
18
    /**
19
     * Homepage of the provider
20
     *
21
     * @var string
22
     */
23
    protected $homepage = 'https://github.com/jenssegers/agent';
24
25
    /**
26
     * Composer package name
27
     *
28
     * @var string
29
     */
30
    protected $packageName = 'jenssegers/agent';
31
32
    protected $detectionCapabilities = [
33
34
        'browser' => [
35
            'name'    => true,
36
            'version' => true,
37
        ],
38
39
        'renderingEngine' => [
40
            'name'    => false,
41
            'version' => false,
42
        ],
43
44
        'operatingSystem' => [
45
            'name'    => true,
46
            'version' => true,
47
        ],
48
49
        'device' => [
50
            'model'    => true,
51
            'brand'    => false,
52
            'type'     => false,
53
            'isMobile' => true,
54
            'isTouch'  => false,
55
        ],
56
57
        'bot' => [
58
            'isBot' => true,
59
            'name'  => true,
60
            'type'  => false,
61
        ],
62
    ];
63
64
    /**
65
     * Used for unitTests mocking
66
     *
67
     * @var Agent
68
     */
69
    private $parser;
70
71
    /**
72
     *
73
     * @throws PackageNotLoadedException
74
     */
75 12
    public function __construct()
76
    {
77 12
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
78 1
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
79
        }
80 11
    }
81
82
    /**
83
     *
84
     * @return Agent
85
     */
86 6
    public function getParser()
87
    {
88 6
        if ($this->parser !== null) {
89 5
            return $this->parser;
90
        }
91
92 1
        return new Agent();
93
    }
94
95
    /**
96
     *
97
     * @param array $resultRaw
98
     *
99
     * @return bool
100
     */
101 5
    private function hasResult(array $resultRaw)
102
    {
103 5
        if ($resultRaw['isMobile'] === true || $resultRaw['isRobot'] === true) {
104 2
            return true;
105
        }
106
107 3
        if ($this->isRealResult($resultRaw['browserName']) === true || $this->isRealResult($resultRaw['osName']) === true || $this->isRealResult($resultRaw['deviceModel']) === true || $this->isRealResult($resultRaw['botName']) === true) {
108 2
            return true;
109
        }
110
111 1
        return false;
112
    }
113
114
    /**
115
     *
116
     * @param Model\Bot $bot
117
     * @param array     $browserRaw
0 ignored issues
show
Bug introduced by
There is no parameter named $browserRaw. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
118
     */
119 1
    private function hydrateBot(Model\Bot $bot, array $resultRaw)
120
    {
121 1
        $bot->setIsBot(true);
122
123 1
        if ($this->isRealResult($resultRaw['botName']) === true) {
124 1
            $bot->setName($resultRaw['botName']);
125 1
        }
126 1
    }
127
128
    /**
129
     *
130
     * @param Model\Browser $browser
131
     * @param array         $resultRaw
132
     */
133 3
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
134
    {
135 3
        if ($this->isRealResult($resultRaw['browserName']) === true) {
136 1
            $browser->setName($resultRaw['browserName']);
137
138 1
            if ($this->isRealResult($resultRaw['browserVersion']) === true) {
139 1
                $browser->getVersion()->setComplete($resultRaw['browserVersion']);
140 1
            }
141 1
        }
142 3
    }
143
144
    /**
145
     *
146
     * @param Model\OperatingSystem $os
147
     * @param array                 $resultRaw
148
     */
149 3
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw)
150
    {
151 3
        if ($this->isRealResult($resultRaw['osName']) === true) {
152 1
            $os->setName($resultRaw['osName']);
153
154 1
            if ($this->isRealResult($resultRaw['osVersion']) === true) {
155 1
                $os->getVersion()->setComplete($resultRaw['osVersion']);
156 1
            }
157 1
        }
158 3
    }
159
160
    /**
161
     *
162
     * @param Model\Device $device
163
     * @param array        $resultRaw
164
     */
165 3
    private function hydrateDevice(Model\Device $device, array $resultRaw)
166
    {
167 3
        if ($this->isRealResult($resultRaw['deviceModel']) === true) {
168 1
            $device->setModel($resultRaw['deviceModel']);
169 1
        }
170
171 3
        if ($resultRaw['isMobile'] === true) {
172 1
            $device->setIsMobile(true);
173 1
        }
174 3
    }
175
176 5
    public function parse($userAgent, array $headers = [])
177
    {
178 5
        $parser = $this->getParser();
179 5
        $parser->setHttpHeaders($headers);
180 5
        $parser->setUserAgent($userAgent);
181
182
        /*
183
         * Since Mobile_Detect to a regex comparison on every call
184
         * We cache it here for all checks and hydration
185
         */
186 5
        $browserName = $parser->browser();
187 5
        $osName      = $parser->platform();
188
189
        $resultCache = [
190 5
            'browserName'    => $browserName,
191 5
            'browserVersion' => $parser->version($browserName),
192
193 5
            'osName'    => $osName,
194 5
            'osVersion' => $parser->version($osName),
195
196 5
            'deviceModel' => $parser->device(),
197 5
            'isMobile'    => $parser->isMobile(),
198
199 5
            'isRobot' => $parser->isRobot(),
200 5
            'botName' => $parser->robot(),
201 5
        ];
202
203
        /*
204
         * No result found?
205
         */
206 5
        if ($this->hasResult($resultCache) !== true) {
207 1
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
208
        }
209
210
        /*
211
         * Hydrate the model
212
         */
213 4
        $result = new Model\UserAgent();
214 4
        $result->setProviderResultRaw($resultCache);
215
216
        /*
217
         * Bot detection
218
         */
219 4
        if ($resultCache['isRobot'] === true) {
220 1
            $this->hydrateBot($result->getBot(), $resultCache);
221
222 1
            return $result;
223
        }
224
225
        /*
226
         * hydrate the result
227
         */
228 3
        $this->hydrateBrowser($result->getBrowser(), $resultCache);
229 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultCache);
230 3
        $this->hydrateDevice($result->getDevice(), $resultCache);
231
232 3
        return $result;
233
    }
234
}
235