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 (#65)
by Martin
14:36
created

JenssegersAgent::hydrateDevice()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 5
nc 4
nop 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
    public function __construct()
76
    {
77
        if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) {
78
            throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider');
79
        }
80
    }
81
82
    /**
83
     *
84
     * @return Agent
85
     */
86
    public function getParser()
87
    {
88
        if ($this->parser !== null) {
89
            return $this->parser;
90
        }
91
92
        return new Agent();
93
    }
94
95
    /**
96
     *
97
     * @param array $resultRaw
98
     *
99
     * @return bool
100
     */
101
    private function hasResult(array $resultRaw)
102
    {
103
        if ($resultRaw['isMobile'] === true || $resultRaw['isRobot'] === true) {
104
            return true;
105
        }
106
107
        if ($this->isRealResult($resultRaw['browserName']) === true || $this->isRealResult($resultRaw['osName']) === true || $this->isRealResult($resultRaw['deviceModel']) === true || $this->isRealResult($resultRaw['botName']) === true) {
108
            return true;
109
        }
110
111
        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
    private function hydrateBot(Model\Bot $bot, array $resultRaw)
120
    {
121
        $bot->setIsBot(true);
122
123
        if ($this->isRealResult($resultRaw['botName']) === true) {
124
            $bot->setName($resultRaw['botName']);
125
        }
126
    }
127
128
    /**
129
     *
130
     * @param Model\Browser $browser
131
     * @param array         $resultRaw
132
     */
133
    private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
134
    {
135
        if ($this->isRealResult($resultRaw['browserName']) === true) {
136
            $browser->setName($resultRaw['browserName']);
137
138
            if ($this->isRealResult($resultRaw['browserVersion']) === true) {
139
                $browser->getVersion()->setComplete($resultRaw['browserVersion']);
140
            }
141
        }
142
    }
143
144
    /**
145
     *
146
     * @param Model\OperatingSystem $os
147
     * @param array                 $resultRaw
148
     */
149
    private function hydrateOperatingSystem(Model\OperatingSystem $os, array $resultRaw)
150
    {
151
        if ($this->isRealResult($resultRaw['osName']) === true) {
152
            $os->setName($resultRaw['osName']);
153
154
            if ($this->isRealResult($resultRaw['osVersion']) === true) {
155
                $os->getVersion()->setComplete($resultRaw['osVersion']);
156
            }
157
        }
158
    }
159
160
    /**
161
     *
162
     * @param Model\Device $device
163
     * @param array        $resultRaw
164
     */
165
    private function hydrateDevice(Model\Device $device, array $resultRaw)
166
    {
167
        if ($this->isRealResult($resultRaw['deviceModel']) === true) {
168
            $device->setModel($resultRaw['deviceModel']);
169
        }
170
171
        if ($resultRaw['isMobile'] === true) {
172
            $device->setIsMobile(true);
173
        }
174
    }
175
176
    public function parse($userAgent, array $headers = [])
177
    {
178
        $parser = $this->getParser();
179
        $parser->setHttpHeaders($headers);
180
        $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
        $browserName = $parser->browser();
187
        $osName      = $parser->platform();
188
189
        $resultCache = [
190
            'browserName'    => $browserName,
191
            'browserVersion' => $parser->version($browserName),
192
193
            'osName'    => $osName,
194
            'osVersion' => $parser->version($osName),
195
196
            'deviceModel' => $parser->device(),
197
            'isMobile'    => $parser->isMobile(),
198
199
            'isRobot' => $parser->isRobot(),
200
            'botName' => $parser->robot(),
201
        ];
202
203
        /*
204
         * No result found?
205
         */
206
        if ($this->hasResult($resultCache) !== true) {
207
            throw new NoResultFoundException('No result found for user agent: ' . $userAgent);
208
        }
209
210
        /*
211
         * Hydrate the model
212
         */
213
        $result = new Model\UserAgent();
214
        $result->setProviderResultRaw($resultCache);
215
216
        /*
217
         * Bot detection
218
         */
219
        if ($resultCache['isRobot'] === true) {
220
            $this->hydrateBot($result->getBot(), $resultCache);
221
222
            return $result;
223
        }
224
225
        /*
226
         * hydrate the result
227
         */
228
        $this->hydrateBrowser($result->getBrowser(), $resultCache);
229
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultCache);
230
        $this->hydrateDevice($result->getDevice(), $resultCache);
231
232
        return $result;
233
    }
234
}
235