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 ( 96f191...a0fbd6 )
by Martin
06:51
created

UdgerCom::getResult()   C

Complexity

Conditions 14
Paths 8

Size

Total Lines 63
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 14

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 63
ccs 24
cts 24
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
 * 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 20
    public function __construct(Client $client, $apiKey)
76
    {
77 20
        parent::__construct($client);
78
79 20
        $this->apiKey = $apiKey;
80 20
    }
81
82 1
    public function getVersion()
83
    {
84 1
        return;
85
    }
86
87
    /**
88
     *
89
     * @param  string                     $userAgent
90
     * @param  array                      $headers
91
     * @return stdClass
92
     * @throws Exception\RequestException
93
     */
94 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...
95
    {
96
        /*
97
         * an empty UserAgent makes no sense
98
         */
99 13
        if ($userAgent == '') {
100 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
101
        }
102
103
        $params = [
104 12
            'accesskey' => $this->apiKey,
105 12
            'uastrig'   => $userAgent,
106
        ];
107
108 12
        $body = http_build_query($params, null, '&');
109
110 12
        $request = new Request('POST', self::$uri, [
111 12
            'Content-Type' => 'application/x-www-form-urlencoded',
112
        ], $body);
113
114 12
        $response = $this->getResponse($request);
115
116
        /*
117
         * no json returned?
118
         */
119 11
        $contentType = $response->getHeader('Content-Type');
120 11
        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 10
        $content = json_decode($response->getBody()->getContents());
125
126
        /*
127
         * No result found?
128
         */
129 10
        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 9
        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 8
        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 7
        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 6
        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 5
        return $content;
156
    }
157
158
    /**
159
     *
160
     * @param  stdClass $resultRaw
161
     * @return boolean
162
     */
163 5
    private function isBot(stdClass $resultRaw)
164
    {
165 5
        if (isset($resultRaw->type) && $resultRaw->type === 'Robot') {
166 1
            return true;
167
        }
168
169 4
        return false;
170
    }
171
172
    /**
173
     *
174
     * @param Model\Bot $bot
175
     * @param stdClass  $resultRaw
176
     */
177 1
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
178
    {
179 1
        $bot->setIsBot(true);
180
181 1
        if (isset($resultRaw->ua_family)) {
182 1
            $bot->setName($this->getRealResult($resultRaw->ua_family));
183
        }
184 1
    }
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 13
    public function parse($userAgent, array $headers = [])
239
    {
240 13
        $resultRaw = $this->getResult($userAgent, $headers);
241
242
        /*
243
         * Hydrate the model
244
         */
245 5
        $result = new Model\UserAgent();
246 5
        $result->setProviderResultRaw($resultRaw);
247
248
        /*
249
         * Bot detection
250
         */
251 5
        if ($this->isBot($resultRaw->info) === true) {
252 1
            $this->hydrateBot($result->getBot(), $resultRaw->info);
253
254 1
            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