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.

UserAgentApiCom::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
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
 * Abstraction of useragentapi.com
12
 *
13
 * @author Martin Keckeis <[email protected]>
14
 * @license MIT
15
 * @see https://useragentapi.com/docs
16
 */
17
class UserAgentApiCom extends AbstractHttpProvider
18
{
19
    /**
20
     * Name of the provider
21
     *
22
     * @var string
23
     */
24
    protected $name = 'UserAgentApiCom';
25
26
    /**
27
     * Homepage of the provider
28
     *
29
     * @var string
30
     */
31
    protected $homepage = 'http://useragentapi.com/';
32
33
    protected $detectionCapabilities = [
34
35
        'browser' => [
36
            'name'    => true,
37
            'version' => true,
38
        ],
39
40
        'renderingEngine' => [
41
            'name'    => true,
42
            'version' => true,
43
        ],
44
45
        'operatingSystem' => [
46
            'name'    => false,
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'  => true,
61
            'type'  => false,
62
        ],
63
    ];
64
65
    private static $uri = 'https://useragentapi.com/api/v3/json';
66
67
    private $apiKey;
68
69 19
    public function __construct(Client $client, $apiKey)
70
    {
71 19
        parent::__construct($client);
72
73 19
        $this->apiKey = $apiKey;
74 19
    }
75
76 6
    public function getVersion()
77
    {
78 6
        return;
79
    }
80
81
    /**
82
     *
83
     * @param  string                     $userAgent
84
     * @param  array                      $headers
85
     * @return stdClass
86
     * @throws Exception\RequestException
87
     */
88 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...
89
    {
90
        /*
91
         * an empty UserAgent makes no sense
92
         */
93 12
        if ($userAgent == '') {
94 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
95
        }
96
97 11
        $parameters = '/' . $this->apiKey;
98 11
        $parameters .= '/' . rawurlencode($userAgent);
99
100 11
        $uri = self::$uri . $parameters;
101
102 11
        $request = new Request('GET', $uri);
103
104
        try {
105 11
            $response = $this->getResponse($request);
106 3
        } catch (Exception\RequestException $ex) {
107
            /* @var $prevEx \GuzzleHttp\Exception\ClientException */
108 3
            $prevEx = $ex->getPrevious();
109
110 3
            if ($prevEx->hasResponse() === true && $prevEx->getResponse()->getStatusCode() === 400) {
111 2
                $content = $prevEx->getResponse()
112 2
                    ->getBody()
113 2
                    ->getContents();
114 2
                $content = json_decode($content);
115
116
                /*
117
                 * Error
118
                 */
119 2
                if (isset($content->error->code) && $content->error->code == 'key_invalid') {
120 1
                    throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName(), null, $ex);
121
                }
122
123 1
                if (isset($content->error->code) && $content->error->code == 'useragent_invalid') {
124 1
                    throw new Exception\RequestException('User agent is invalid "' . $userAgent . '"');
125
                }
126
            }
127
128 1
            throw $ex;
129
        }
130
131
        /*
132
         * no json returned?
133
         */
134 8
        $contentType = $response->getHeader('Content-Type');
135 8
        if (! isset($contentType[0]) || $contentType[0] != 'application/json') {
136 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
137
        }
138
139 7
        $content = json_decode($response->getBody()->getContents());
140
141
        /*
142
         * No result
143
         */
144 7
        if (isset($content->error->code) && $content->error->code == 'useragent_not_found') {
145 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
146
        }
147
148
        /*
149
         * Missing data?
150
         */
151 6
        if (! $content instanceof stdClass || ! isset($content->data)) {
152 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Data is missing "' . $response->getBody()->getContents() . '"');
153
        }
154
155 5
        return $content->data;
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->platform_type) && $resultRaw->platform_type === 'Bot') {
166 2
            return true;
167
        }
168
169 3
        return false;
170
    }
171
172
    /**
173
     *
174
     * @param Model\Bot $bot
175
     * @param stdClass  $resultRaw
176
     */
177 2
    private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
178
    {
179 2
        $bot->setIsBot(true);
180
181 2
        if (isset($resultRaw->platform_name)) {
182 2
            $bot->setName($this->getRealResult($resultRaw->platform_name));
183
        }
184 2
    }
185
186
    /**
187
     *
188
     * @param Model\Browser $browser
189
     * @param stdClass      $resultRaw
190
     */
191 3
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
192
    {
193 3
        if (isset($resultRaw->browser_name)) {
194 1
            $browser->setName($this->getRealResult($resultRaw->browser_name));
195
        }
196
197 3
        if (isset($resultRaw->browser_version)) {
198 1
            $browser->getVersion()->setComplete($this->getRealResult($resultRaw->browser_version));
199
        }
200 3
    }
201
202
    /**
203
     *
204
     * @param Model\RenderingEngine $engine
205
     * @param stdClass              $resultRaw
206
     */
207 3
    private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw)
208
    {
209 3
        if (isset($resultRaw->engine_name)) {
210 1
            $engine->setName($this->getRealResult($resultRaw->engine_name));
211
        }
212
213 3
        if (isset($resultRaw->engine_version)) {
214 1
            $engine->getVersion()->setComplete($this->getRealResult($resultRaw->engine_version));
215
        }
216 3
    }
217
218
    /**
219
     *
220
     * @param Model\Device $device
221
     * @param stdClass     $resultRaw
222
     */
223 3
    private function hydrateDevice(Model\Device $device, stdClass $resultRaw)
224
    {
225 3
        if (isset($resultRaw->platform_type)) {
226 1
            $device->setType($this->getRealResult($resultRaw->platform_type));
227
        }
228 3
    }
229
230 12
    public function parse($userAgent, array $headers = [])
231
    {
232 12
        $resultRaw = $this->getResult($userAgent, $headers);
233
234
        /*
235
         * Hydrate the model
236
         */
237 5
        $result = new Model\UserAgent($this->getName(), $this->getVersion());
238 5
        $result->setProviderResultRaw($resultRaw);
239
240
        /*
241
         * Bot detection
242
         */
243 5
        if ($this->isBot($resultRaw) === true) {
244 2
            $this->hydrateBot($result->getBot(), $resultRaw);
245
246 2
            return $result;
247
        }
248
249
        /*
250
         * hydrate the result
251
         */
252 3
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
253 3
        $this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw);
254 3
        $this->hydrateDevice($result->getDevice(), $resultRaw);
255
256 3
        return $result;
257
    }
258
}
259