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.

ApiRequester::response()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Vindi;
4
5
use Psr\Http\Message\ResponseInterface;
6
use GuzzleHttp\Exception\ClientException;
7
use Vindi\Exceptions\ValidationException;
8
use Vindi\Http\Client;
9
use Vindi\Exceptions\RequestException;
10
use Vindi\Exceptions\RateLimitException;
11
12
/**
13
 * Class ApiRequester
14
 *
15
 * @package Vindi
16
 */
17
class ApiRequester
18
{
19
    /**
20
     * @var \Vindi\Http\Client
21
     */
22
    public $client;
23
24
    /**
25
     * @var ResponseInterface
26
     */
27
    public $lastResponse;
28
29
    /**
30
     * @var array
31
     */
32
    public $lastOptions;
33
34
    /**
35
     * ApiRequester constructor.
36
     */
37 458
    public function __construct()
38
    {
39 458
        $this->client = new Client;
40 458
    }
41
42
    /**
43
     * @param string $method   HTTP Method.
44
     * @param string $endpoint Relative to API base path.
45
     * @param array  $options  Options for the request.
46
     *
47
     * @return mixed
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     * @throws \Vindi\Exceptions\RateLimitException
50
     * @throws \Vindi\Exceptions\RequestException
51
     */
52 10
    public function request($method, $endpoint, array $options = [])
53
    {
54 10
        $this->lastOptions = $options;
55
        try {
56 10
            $response = $this->client->request($method, $endpoint, $options);
57 10
        } catch (ClientException $e) {
58 2
            $response = $e->getResponse();
59
        }
60
61 10
        return $this->response($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $e->getResponse() on line 58 can be null; however, Vindi\ApiRequester::response() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
62
    }
63
64
    /**
65
     * @param ResponseInterface $response
66
     *
67
     * @return object
68
     * @throws RateLimitException
69
     * @throws \Vindi\Exceptions\RequestException
70
     */
71 10
    public function response(ResponseInterface $response)
72
    {
73 10
        $this->lastResponse = $response;
74
75 10
        $content = $response->getBody()->getContents();
76
77 10
        $decoded = json_decode($content); // parse as object
78 10
        $data = $decoded;
79 10
80
        if (!empty($decoded)) {
81 10
            reset($decoded);
82 8
            $data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors.
83
        }
84 2
85
        $this->checkRateLimit($response)
86
            ->checkForErrors($response, $data);
87
88
        return $data;
89
    }
90
91
    /**
92
     * @param ResponseInterface $response
93 10
     *
94
     * @return $this
95 10
     * @throws RateLimitException
96 2
     */
97
    private function checkRateLimit(ResponseInterface $response)
98
    {
99 8
        if (429 === $response->getStatusCode()) {
100
            throw new RateLimitException($response);
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * @param ResponseInterface $response
108
     * @param mixed             $data
109 8
     *
110
     * @return $this
111 8
     * @throws \Vindi\Exceptions\RequestException
112
     */
113 8
    private function checkForErrors(ResponseInterface $response, $data)
114
    {
115 8
        $status = $response->getStatusCode();
116
117 8
        $data = (array)$data;
118
119 6
        $statusClass = (int)($status / 100);
120 2
121 4
        if (($statusClass === 4) || ($statusClass === 5)) {
122 4
            switch ($status) {
123 4
                case 422:
124
                    throw new ValidationException($status, $data, $this->lastOptions);
125
                default:
126 2
                    throw new RequestException($status, $data, $this->lastOptions);
127
            }
128
        }
129
130
        return $this;
131
    }
132
}
133