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 ( 9a6b33...16ad3e )
by François
03:05
created

GuzzleHttpClient::handleError()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
namespace SURFnet\VPN\Common\HttpClient;
19
20
use GuzzleHttp\Client;
21
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException;
22
use GuzzleHttp\Exception\BadResponseException;
23
24
class GuzzleHttpClient implements HttpClientInterface
25
{
26
    /** @var \GuzzleHttp\Client */
27
    private $httpClient;
28
29
    public function __construct(array $guzzleOptions)
30
    {
31
        // http://docs.guzzlephp.org/en/5.3/clients.html#request-options
32
        $defaultOptions = [
33
            'allow_redirects' => false,
34
            'timeout' => 5,
35
            'headers' => [
36
                'Accept' => 'application/json',
37
            ],
38
        ];
39
40
        $this->httpClient = new Client(
41
            array_merge_recursive($defaultOptions, $guzzleOptions)
42
        );
43
    }
44
45
    public function get($requestUri, array $requestOptions = [])
46
    {
47
        try {
48
            return $this->httpClient->get($requestUri, $requestOptions)->json();
49
        } catch (BadResponseException $e) {
50
            $this->handleError($e);
51
        }
52
    }
53
54
    public function post($requestUri, array $postData, array $requestOptions = [])
55
    {
56
        try {
57
            return $this->httpClient->post(
58
                $requestUri,
59
                array_merge_recursive(
60
                    $requestOptions,
61
                    [
62
                        'body' => [
63
                            $postData,
64
                        ],
65
                    ]
66
                )
67
            )->json();
68
        } catch (BadResponseException $e) {
69
            $this->handleError($e);
70
        }
71
    }
72
73
    public function handleError(BadResponseException $e)
0 ignored issues
show
Unused Code introduced by
The parameter $e 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...
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
74
    {
75
        try {
76
            $responseData = $e->getResponse()->json();
77
        } catch (InvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class SURFnet\VPN\Common\HttpC...nvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
78
            // unable to decode JSON
79
            throw new RuntimeException('expected JSON from HTTP endpoint');
80
        }
81
82
        if (!is_array($responseData) && !array_key_exists($responseData, 'error')) {
83
            throw new RuntimeException();
84
        }
85
86
        throw new HttpClientException($responseData['error']);
87
    }
88
}
89