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.

UdgerCom::hydrateBot()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
namespace UserAgentParser\Provider\Http;
3
4
use GuzzleHttp\Client;
5
use GuzzleHttp\Psr7\Request;
6
use stdClass;
7
use UserAgentParser\Exception;
8
use UserAgentParser\Model;
9
10
/**
11
 * Abstraction of udger.com
12
 *
13
 * @author Martin Keckeis <[email protected]>
14
 * @license MIT
15
 * @see https://udger.com/support/documentation/?doc=38
16
 */
17
class UdgerCom extends AbstractHttpProvider
18
{
19
    /**
20
     * Name of the provider
21
     *
22
     * @var string
23
     */
24
    protected $name = 'UdgerCom';
25
26
    /**
27
     * Homepage of the provider
28
     *
29
     * @var string
30
     */
31
    protected $homepage = 'https://udger.com/';
32
33
    protected $detectionCapabilities = [
34
35
        'browser' => [
36
            'name'    => true,
37
            'version' => true,
38
        ],
39
40
        'renderingEngine' => [
41
            'name'    => true,
42
            'version' => false,
43
        ],
44
45
        'operatingSystem' => [
46
            'name'    => true,
47
            'version' => false,
48
        ],
49
50
        'device' => [
51
            'model'    => false,
52
            'brand'    => false,
53
            'type'     => true,
54
            'isMobile' => false,
55
            'isTouch'  => false,
56
        ],
57
58
        'bot' => [
59
            'isBot' => true,
60
            'name'  => false,
61
            'type'  => false,
62
        ],
63
    ];
64
65
    protected $defaultValues = [
66
        'general' => [
67
            '/^unknown$/i',
68
        ],
69
    ];
70
71
    private static $uri = 'http://api.udger.com/parse';
72
73
    private $apiKey;
74
75 21
    public function __construct(Client $client, $apiKey)
76
    {
77 21
        parent::__construct($client);
78
79 21
        $this->apiKey = $apiKey;
80 21
    }
81
82 7
    public function getVersion()
83
    {
84 7
        return;
85
    }
86
87
    /**
88
     *
89
     * @param  string                     $userAgent
90
     * @param  array                      $headers
91
     * @return stdClass
92
     * @throws Exception\RequestException
93
     */
94 14
    protected function getResult($userAgent, array $headers)
0 ignored issues
show
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
95
    {
96
        /*
97
         * an empty UserAgent makes no sense
98
         */
99 14
        if ($userAgent == '') {
100 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
101
        }
102
103
        $params = [
104 13
            'accesskey' => $this->apiKey,
105 13
            'uastrig'   => $userAgent,
106
        ];
107
108 13
        $body = http_build_query($params, null, '&');
109
110 13
        $request = new Request('POST', self::$uri, [
111 13
            'Content-Type' => 'application/x-www-form-urlencoded',
112
        ], $body);
113
114 13
        $response = $this->getResponse($request);
115
116
        /*
117
         * no json returned?
118
         */
119 12
        $contentType = $response->getHeader('Content-Type');
120 12
        if (! isset($contentType[0]) || $contentType[0] != 'application/json') {
121 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
122
        }
123
124 11
        $content = json_decode($response->getBody()->getContents());
125
126
        /*
127
         * No result found?
128
         */
129 11
        if (isset($content->flag) && $content->flag == 3) {
130 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
131
        }
132
133
        /*
134
         * Errors
135
         */
136 10
        if (isset($content->flag) && $content->flag == 4) {
137 1
            throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName());
138
        }
139
140 9
        if (isset($content->flag) && $content->flag == 6) {
141 1
            throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName());
142
        }
143
144 8
        if (isset($content->flag) && $content->flag > 3) {
145 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
146
        }
147
148
        /*
149
         * Missing data?
150
         */
151 7
        if (! $content instanceof stdClass || ! isset($content->info)) {
152 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
153
        }
154
155 6
        return $content;
156
    }
157
158
    /**
159
     *
160
     * @param  stdClass $resultRaw
161
     * @return boolean
162
     */
163 6
    private function isBot(stdClass $resultRaw)
164
    {
165 6
        if (isset($resultRaw->type) && $resultRaw->type === 'Robot') {
166 2
            return true;
167
        }
168
169 4
        return false;
170
    }
171
172
    /**
173
     *
174
     * @param Model\Bot $bot
175
     * @param stdClass  $resultRaw
176
     */
177 2
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
178
    {
179 2
        $bot->setIsBot(true);
180
181 2
        if (isset($resultRaw->ua_family)) {
182 2
            $bot->setName($this->getRealResult($resultRaw->ua_family));
183
        }
184 2
    }
185
186
    /**
187
     *
188
     * @param Model\Browser $browser
189
     * @param stdClass      $resultRaw
190
     */
191 4
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
192
    {
193 4
        if (isset($resultRaw->ua_family)) {
194 1
            $browser->setName($this->getRealResult($resultRaw->ua_family, 'browser', 'name'));
195
        }
196
197 4
        if (isset($resultRaw->ua_ver)) {
198 1
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw->ua_ver));
199
        }
200 4
    }
201
202
    /**
203
     *
204
     * @param Model\RenderingEngine $engine
205
     * @param stdClass              $resultRaw
206
     */
207 4
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
208
    {
209 4
        if (isset($resultRaw->ua_engine)) {
210 1
            $engine->setName($this->getRealResult($resultRaw->ua_engine));
211
        }
212 4
    }
213
214
    /**
215
     *
216
     * @param Model\OperatingSystem $os
217
     * @param stdClass              $resultRaw
218
     */
219 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
220
    {
221 4
        if (isset($resultRaw->os_family)) {
222 1
            $os->setName($this->getRealResult($resultRaw->os_family));
223
        }
224 4
    }
225
226
    /**
227
     *
228
     * @param Model\Device $device
229
     * @param stdClass     $resultRaw
230
     */
231 4
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
232
    {
233 4
        if (isset($resultRaw->device_name)) {
234 1
            $device->setType($this->getRealResult($resultRaw->device_name));
235
        }
236 4
    }
237
238 14
    public function parse($userAgent, array $headers = [])
239
    {
240 14
        $resultRaw = $this->getResult($userAgent, $headers);
241
242
        /*
243
         * Hydrate the model
244
         */
245 6
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
246 6
        $result->setProviderResultRaw($resultRaw);
247
248
        /*
249
         * Bot detection
250
         */
251 6
        if ($this->isBot($resultRaw->info) === true) {
252 2
            $this->hydrateBot($result->getBot(), $resultRaw->info);
253
254 2
            return $result;
255
        }
256
257
        /*
258
         * hydrate the result
259
         */
260 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->info);
261 4
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw->info);
262 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->info);
263 4
        $this->hydrateDevice($result->getDevice(), $resultRaw->info);
264
265 4
        return $result;
266
    }
267
}
268