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

WhatIsMyBrowserCom::hydrateRenderingEngine()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 3
Ratio 30 %

Code Coverage

Tests 8
CRAP Score 5

Importance

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