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 (#43)
by Martin
06:34
created

NeutrinoApiCom::getVersion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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://www.neutrinoapi.com/api/user-agent-info/
13
 */
14
class NeutrinoApiCom extends AbstractHttpProvider
15
{
16
    protected $detectionCapabilities = [
17
18
        'browser' => [
19
            'name'    => true,
20
            'version' => true,
21
        ],
22
23
        'renderingEngine' => [
24
            'name'    => false,
25
            'version' => false,
26
        ],
27
28
        'operatingSystem' => [
29
            'name'    => true,
30
            'version' => false,
31
        ],
32
33
        'device' => [
34
            'model'    => true,
35
            'brand'    => true,
36
            'type'     => true,
37
            'isMobile' => true,
38
            'isTouch'  => false,
39
        ],
40
41
        'bot' => [
42
            'isBot' => true,
43
            'name'  => true,
44
            'type'  => false,
45
        ],
46
    ];
47
48
    protected $defaultValues = [
49
        'unknown',
50
    ];
51
52
    private static $uri = 'https://neutrinoapi.com/user-agent-info';
53
54
    private $apiUserId;
55
56
    private $apiKey;
57
58 17
    public function __construct(Client $client, $apiUserId, $apiKey)
59
    {
60 17
        parent::__construct($client);
61
62 17
        $this->apiUserId = $apiUserId;
63 17
        $this->apiKey    = $apiKey;
64 17
    }
65
66 3
    public function getName()
67
    {
68 3
        return 'NeutrinoApiCom';
69
    }
70
71 1
    public function getComposerPackageName()
72
    {
73 1
        return;
74
    }
75
76 1
    public function getVersion()
77
    {
78 1
        return;
79
    }
80
81
    /**
82
     *
83
     * @param  string                     $userAgent
84
     * @param  array                      $headers
85
     * @return stdClass
86
     * @throws Exception\RequestException
87
     */
88 13
    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...
89
    {
90
        /*
91
         * an empty UserAgent makes no sense
92
         */
93 13
        if ($userAgent == '') {
94 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
95
        }
96
97
        $params = [
98 12
            'user-id'       => $this->apiUserId,
99 12
            'api-key'       => $this->apiKey,
100 12
            'output-format' => 'json',
101 12
            'output-case'   => 'snake',
102
103 12
            'user-agent' => $userAgent,
104 12
        ];
105
106 12
        $body = http_build_query($params, null, '&');
107
108 12
        $request = new Request('POST', self::$uri, [
109 12
            'Content-Type' => 'application/x-www-form-urlencoded',
110 12
        ], $body);
111
112
        try {
113 12
            $response = $this->getResponse($request);
114 12
        } catch (Exception\RequestException $ex) {
115
            /* @var $prevEx \GuzzleHttp\Exception\ClientException */
116 2
            $prevEx = $ex->getPrevious();
117
118 2
            if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 403) {
119 1
                throw new Exception\InvalidCredentialsException('Your API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex);
120
            }
121
122 1
            throw $ex;
123
        }
124
125
        /*
126
         * no json returned?
127
         */
128 10
        $contentType = $response->getHeader('Content-Type');
129 10 View Code Duplication
        if (! isset($contentType[0]) || $contentType[0] != 'application/json;charset=UTF-8') {
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...
130 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
131
        }
132
133 9
        $content = json_decode($response->getBody()->getContents());
134
135
        /*
136
         * errors
137
         */
138 9
        if (isset($content->api_error)) {
139 3
            switch ($content->api_error) {
140
141 3 View Code Duplication
                case 1:
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...
142 1
                    throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
143
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
144
145 2
                case 2:
146 1
                    throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API userId "' . $this->apiUserId . '" and key "' . $this->apiKey . '" for ' . $this->getName());
147
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
148
149 1 View Code Duplication
                default:
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...
150 1
                    throw new Exception\RequestException('"' . $content->api_error_msg . '" response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
151
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
152
            }
153
        }
154
155
        /*
156
         * Missing data?
157
         */
158 6
        if (! $content instanceof stdClass) {
159 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
160
        }
161
162 5
        return $content;
163
    }
164
165
    /**
166
     *
167
     * @param stdClass $resultRaw
168
     *
169
     * @return bool
170
     */
171 5 View Code Duplication
    private function hasResult(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...
172
    {
173 5
        if (isset($resultRaw->type) && $this->isRealResult($resultRaw->type)) {
174 4
            return true;
175
        }
176
177 1
        return false;
178
    }
179
180
    /**
181
     *
182
     * @param  stdClass $resultRaw
183
     * @return boolean
184
     */
185 4 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...
186
    {
187 4
        if (isset($resultRaw->type) && $resultRaw->type === 'robot') {
188 1
            return true;
189
        }
190
191 3
        return false;
192
    }
193
194
    /**
195
     *
196
     * @param Model\Bot $bot
197
     * @param stdClass  $resultRaw
198
     */
199 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...
200
    {
201 1
        $bot->setIsBot(true);
202
203 1
        if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
204 1
            $bot->setName($resultRaw->browser_name);
205
        }
206 1
    }
207
208
    /**
209
     *
210
     * @param Model\Browser $browser
211
     * @param stdClass      $resultRaw
212
     */
213 3
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
214
    {
215 3
        if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
216 1
            $browser->setName($resultRaw->browser_name);
217
        }
218
219 3
        if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) {
220 1
            $browser->getVersion()->setComplete($resultRaw->version);
221
        }
222 3
    }
223
224
    /**
225
     *
226
     * @param Model\OperatingSystem $os
227
     * @param stdClass              $resultRaw
228
     */
229 3
    private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
230
    {
231 3
        if (isset($resultRaw->operating_system) && $this->isRealResult($resultRaw->operating_system) === true) {
232 1
            $os->setName($resultRaw->operating_system);
233
        }
234 3
    }
235
236
    /**
237
     *
238
     * @param Model\Device $device
239
     * @param stdClass     $resultRaw
240
     */
241 3
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
242
    {
243 3
        if (isset($resultRaw->mobile_model) && $this->isRealResult($resultRaw->mobile_model) === true) {
244 1
            $device->setModel($resultRaw->mobile_model);
245
        }
246
247 3 View Code Duplication
        if (isset($resultRaw->mobile_brand) && $this->isRealResult($resultRaw->mobile_brand) === true) {
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...
248 1
            $device->setBrand($resultRaw->mobile_brand);
249
        }
250
251 3
        if (isset($resultRaw->type) && $this->isRealResult($resultRaw->type) === true) {
252 3
            $device->setType($resultRaw->type);
253
        }
254
255 3 View Code Duplication
        if (isset($resultRaw->is_mobile) && $this->isRealResult($resultRaw->is_mobile) === true) {
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...
256 1
            $device->setIsMobile(true);
257
        }
258 3
    }
259
260 13 View Code Duplication
    public function parse($userAgent, array $headers = [])
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...
261
    {
262 13
        $resultRaw = $this->getResult($userAgent, $headers);
263
264
        /*
265
         * No result found?
266
         */
267 5
        if ($this->hasResult($resultRaw) !== true) {
268 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
269
        }
270
271
        /*
272
         * Hydrate the model
273
         */
274 4
        $result = new Model\UserAgent();
275 4
        $result->setProviderResultRaw($resultRaw);
276
277
        /*
278
         * Bot detection
279
         */
280 4
        if ($this->isBot($resultRaw) === true) {
281 1
            $this->hydrateBot($result->getBot(), $resultRaw);
282
283 1
            return $result;
284
        }
285
286
        /*
287
         * hydrate the result
288
         */
289 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
290 3
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
291 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
292
293 3
        return $result;
294
    }
295
}
296