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 ( e50d76...32d930 )
by Martin
06:12
created

UdgerCom::hydrateBot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 3
eloc 4
nc 2
nop 2
crap 3
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
 *
12
 * @see https://udger.com/support/documentation/?doc=38
13
 *
14
 */
15
class UdgerCom extends AbstractHttpProvider
16
{
17
    /**
18
     * Name of the provider
19
     *
20
     * @var string
21
     */
22
    protected $name = 'UdgerCom';
23
24
    /**
25
     * Homepage of the provider
26
     *
27
     * @var string
28
     */
29
    protected $homepage = 'https://udger.com/';
30
31
    protected $detectionCapabilities = [
32
33
        'browser' => [
34
            'name'    => true,
35
            'version' => true,
36
        ],
37
38
        'renderingEngine' => [
39
            'name'    => true,
40
            'version' => false,
41
        ],
42
43
        'operatingSystem' => [
44
            'name'    => true,
45
            'version' => false,
46
        ],
47
48
        'device' => [
49
            'model'    => false,
50
            'brand'    => false,
51
            'type'     => true,
52
            'isMobile' => false,
53
            'isTouch'  => false,
54
        ],
55
56
        'bot' => [
57
            'isBot' => true,
58
            'name'  => false,
59
            'type'  => false,
60
        ],
61
    ];
62
63
    protected $defaultValues = [
64
        'unknown',
65
    ];
66
67
    private static $uri = 'http://api.udger.com/parse';
68
69
    private $apiKey;
70
71 17
    public function __construct(Client $client, $apiKey)
72
    {
73 17
        parent::__construct($client);
74
75 17
        $this->apiKey = $apiKey;
76 17
    }
77
78 1
    public function getVersion()
79
    {
80 1
        return;
81
    }
82
83
    /**
84
     *
85
     * @param  string                     $userAgent
86
     * @param  array                      $headers
87
     * @return stdClass
88
     * @throws Exception\RequestException
89
     */
90 12
    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...
91
    {
92
        /*
93
         * an empty UserAgent makes no sense
94
         */
95 12
        if ($userAgent == '') {
96 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
97
        }
98
99
        $params = [
100 11
            'accesskey' => $this->apiKey,
101 11
            'uastrig'   => $userAgent,
102 11
        ];
103
104 11
        $body = http_build_query($params, null, '&');
105
106 11
        $request = new Request('POST', self::$uri, [
107 11
            'Content-Type' => 'application/x-www-form-urlencoded',
108 11
        ], $body);
109
110
        try {
111 11
            $response = $this->getResponse($request);
112 11
        } catch (Exception\RequestException $ex) {
113
            /* @var $prevEx \GuzzleHttp\Exception\ClientException */
114 3
            $prevEx = $ex->getPrevious();
115
116 3
            if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 400) {
117 2
                $content = $prevEx->getResponse()
118 2
                    ->getBody()
119 2
                    ->getContents();
120 2
                $content = json_decode($content);
121
122 2
                if (isset($content->flag) && $content->flag == 4) {
123 1
                    throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex);
124
                }
125
126 1 View Code Duplication
                if (isset($content->flag) && $content->flag == 6) {
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...
127 1
                    throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName(), null, $ex);
128
                }
129
            }
130
131 1
            throw $ex;
132
        }
133
134
        /*
135
         * no json returned?
136
         */
137 8
        $contentType = $response->getHeader('Content-Type');
138 8 View Code Duplication
        if (! isset($contentType[0]) || $contentType[0] != 'application/json') {
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...
139 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
140
        }
141
142 7
        $content = json_decode($response->getBody()->getContents());
143
144
        /*
145
         * No result found?
146
         */
147 7
        if (isset($content->flag) && $content->flag == 3) {
148 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
149
        }
150
151
        /*
152
         * Missing data?
153
         */
154 6 View Code Duplication
        if (! $content instanceof stdClass || ! isset($content->info)) {
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...
155 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
156
        }
157
158 5
        return $content;
159
    }
160
161
    /**
162
     *
163
     * @param  stdClass $resultRaw
164
     * @return boolean
165
     */
166 5 View Code Duplication
    private function isBot(stdClass $resultRaw)
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...
167
    {
168 5
        if (isset($resultRaw->type) && $resultRaw->type === 'Robot') {
169 1
            return true;
170
        }
171
172 4
        return false;
173
    }
174
175
    /**
176
     *
177
     * @param Model\Bot $bot
178
     * @param stdClass  $resultRaw
179
     */
180 1 View Code Duplication
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
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...
181
    {
182 1
        $bot->setIsBot(true);
183
184 1
        if (isset($resultRaw->ua_family) && $this->isRealResult($resultRaw->ua_family) === true) {
185 1
            $bot->setName($resultRaw->ua_family);
186 1
        }
187 1
    }
188
189
    /**
190
     *
191
     * @param Model\Browser $browser
192
     * @param stdClass      $resultRaw
193
     */
194 4
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
195
    {
196 4
        if (isset($resultRaw->ua_family) && $this->isRealResult($resultRaw->ua_family) === true) {
197 1
            $browser->setName($resultRaw->ua_family);
198 1
        }
199
200 4
        if (isset($resultRaw->ua_ver) && $this->isRealResult($resultRaw->ua_ver) === true) {
201 1
            $browser->getVersion()->setComplete($resultRaw->ua_ver);
202 1
        }
203 4
    }
204
205
    /**
206
     *
207
     * @param Model\RenderingEngine $engine
208
     * @param stdClass              $resultRaw
209
     */
210 4
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
211
    {
212 4
        if (isset($resultRaw->ua_engine) && $this->isRealResult($resultRaw->ua_engine) === true) {
213 1
            $engine->setName($resultRaw->ua_engine);
214 1
        }
215 4
    }
216
217
    /**
218
     *
219
     * @param Model\OperatingSystem $os
220
     * @param stdClass              $resultRaw
221
     */
222 4
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
223
    {
224 4
        if (isset($resultRaw->os_family) && $this->isRealResult($resultRaw->os_family) === true) {
225 1
            $os->setName($resultRaw->os_family);
226 1
        }
227 4
    }
228
229
    /**
230
     *
231
     * @param Model\UserAgent $device
232
     * @param stdClass        $resultRaw
233
     */
234 4
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
235
    {
236 4
        if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name) === true) {
237 1
            $device->setType($resultRaw->device_name);
238 1
        }
239 4
    }
240
241 12
    public function parse($userAgent, array $headers = [])
242
    {
243 12
        $resultRaw = $this->getResult($userAgent, $headers);
244
245
        /*
246
         * Hydrate the model
247
         */
248 5
        $result = new Model\UserAgent();
249 5
        $result->setProviderResultRaw($resultRaw);
250
251
        /*
252
         * Bot detection
253
         */
254 5
        if ($this->isBot($resultRaw->info) === true) {
255 1
            $this->hydrateBot($result->getBot(), $resultRaw->info);
256
257 1
            return $result;
258
        }
259
260
        /*
261
         * hydrate the result
262
         */
263 4
        $this->hydrateBrowser($result->getBrowser(), $resultRaw->info);
264 4
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw->info);
265 4
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->info);
266 4
        $this->hydrateDevice($result->getDevice(), $resultRaw->info);
267
268 4
        return $result;
269
    }
270
}
271