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

UdgerCom::getResult()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 63
Code Lines 26

Duplication

Lines 12
Ratio 19.05 %

Code Coverage

Tests 26
CRAP Score 14

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 63
ccs 26
cts 26
cp 1
rs 6.0952
cc 14
eloc 26
nc 8
nop 2
crap 14

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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