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

WhatIsMyBrowserCom::getComposerPackageName()   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://developers.whatismybrowser.com/reference
13
 */
14
class WhatIsMyBrowserCom 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' => true,
31
        ],
32
33
        'device' => [
34
            'model' => false,
35
            'brand' => false,
36
37
            'type'     => false,
38
            'isMobile' => false,
39
            'isTouch'  => false,
40
        ],
41
42
        'bot' => [
43
            'isBot' => false,
44
            'name'  => false,
45
            'type'  => false,
46
        ],
47
    ];
48
49
    protected $defaultValues = [
50
        'Unknown Mobile Browser',
51
    ];
52
53
    private static $uri = 'http://api.whatismybrowser.com/api/v1/user_agent_parse';
54
55
    private $apiKey;
56
57 15
    public function __construct(Client $client, $apiKey)
58
    {
59 15
        parent::__construct($client);
60
61 15
        $this->apiKey = $apiKey;
62 15
    }
63
64 4
    public function getName()
65
    {
66 4
        return 'WhatIsMyBrowserCom';
67
    }
68
69 1
    public function getComposerPackageName()
70
    {
71 1
        return;
72
    }
73
74 1
    public function getVersion()
75
    {
76 1
        return;
77
    }
78
79
    /**
80
     *
81
     * @param  string                     $userAgent
82
     * @param  array                      $headers
83
     * @return stdClass
84
     * @throws Exception\RequestException
85
     */
86 11
    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...
87
    {
88
        /*
89
         * an empty UserAgent makes no sense
90
         */
91 11
        if ($userAgent == '') {
92 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
93
        }
94
95
        $params = [
96 10
            'user_key'   => $this->apiKey,
97 10
            'user_agent' => $userAgent,
98 10
        ];
99
100 10
        $body = http_build_query($params, null, '&');
101
102 10
        $request = new Request('POST', self::$uri, [], $body);
103
104 10
        $response = $this->getResponse($request);
105
106
        /*
107
         * no json returned?
108
         */
109 10
        $contentType = $response->getHeader('Content-Type');
110 10 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...
111 1
            throw new Exception\RequestException('Could not get valid "application/json" response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
112
        }
113
114 9
        $content = json_decode($response->getBody()->getContents());
115
116
        /*
117
         * No result
118
         */
119 9
        if (isset($content->message_code) && $content->message_code == 'no_user_agent') {
120 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
121
        }
122
123
        /*
124
         * Limit exceeded
125
         */
126 8 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...
127 1
            throw new Exception\LimitationExceededException('Exceeded the maximum number of request with API key "' . $this->apiKey . '" for ' . $this->getName());
128
        }
129
130
        /*
131
         * Error
132
         */
133 7
        if (isset($content->message_code) && $content->message_code == 'no_api_user_key') {
134 1
            throw new Exception\InvalidCredentialsException('Missing API key for ' . $this->getName());
135
        }
136
137 6 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...
138 1
            throw new Exception\InvalidCredentialsException('Your API key "' . $this->apiKey . '" is not valid for ' . $this->getName());
139
        }
140
141 5 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...
142 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . $response->getBody()->getContents() . '"');
143
        }
144
145
        /*
146
         * Missing data?
147
         */
148 4
        if (! $content instanceof stdClass || ! isset($content->parse) || ! $content->parse instanceof stdClass) {
149 1
            throw new Exception\RequestException('Could not get valid response from "' . $request->getUri() . '". Response is "' . print_r($content, true) . '"');
150
        }
151
152 3
        return $content->parse;
153
    }
154
155
    /**
156
     *
157
     * @param stdClass $resultRaw
158
     *
159
     * @return bool
160
     */
161 3
    private function hasResult(stdClass $resultRaw)
162
    {
163 3
        if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
164 1
            return true;
165
        }
166
167 2
        if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name) === true) {
168 1
            return true;
169
        }
170
171 1
        return false;
172
    }
173
174
    /**
175
     *
176
     * @param Model\Browser $browser
177
     * @param stdClass      $resultRaw
178
     */
179 2
    private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
180
    {
181 2
        if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
182 1
            $browser->setName($resultRaw->browser_name);
183
        }
184
185 2
        if (isset($resultRaw->browser_version_full) && $this->isRealResult($resultRaw->browser_version_full) === true) {
186 1
            $browser->getVersion()->setComplete($resultRaw->browser_version_full);
187
        }
188 2
    }
189
190
    /**
191
     *
192
     * @param Model\OperatingSystem $os
193
     * @param stdClass              $resultRaw
194
     */
195 2
    private function hydrateOperatingSystem(Model\OperatingSystem $os, $resultRaw)
196
    {
197 2
        if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name) === true) {
198 1
            $os->setName($resultRaw->operating_system_name);
199
        }
200
201 2
        if (isset($resultRaw->operating_system_version_full) && $this->isRealResult($resultRaw->operating_system_version_full) === true) {
202 1
            $os->getVersion()->setComplete($resultRaw->operating_system_version_full);
203
        }
204 2
    }
205
206 11
    public function parse($userAgent, array $headers = [])
207
    {
208 11
        $resultRaw = $this->getResult($userAgent, $headers);
209
210
        /*
211
         * No result found?
212
         */
213 3
        if ($this->hasResult($resultRaw) !== true) {
214 1
            throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent);
215
        }
216
217
        /*
218
         * Hydrate the model
219
         */
220 2
        $result = new Model\UserAgent();
221 2
        $result->setProviderResultRaw($resultRaw);
222
223
        /*
224
         * hydrate the result
225
         */
226 2
        $this->hydrateBrowser($result->getBrowser(), $resultRaw);
227 2
        $this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw);
228
229 2
        return $result;
230
    }
231
}
232