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 (#26)
by
unknown
05:57
created

ApiRequester::__construct()   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.037

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1.037
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
class ApiRequester
13
{
14
    /**
15
     * @var \Vindi\Http\Client
16
     */
17
    public $client;
18
19
    /**
20
     * @var \Psr\Http\Message\ResponseInterface
21
     */
22
    public $lastResponse;
23
24
    /**
25
     * @var array
26
     */
27
    public $lastOptions;
28
29
    /**
30
     * ApiRequester constructor.
31
     */
32 424
    public function __construct()
33
    {
34 424
        $this->client = new Client;
35
    }
36
37
    /**
38
     * @param string $method   HTTP Method.
39
     * @param string $endpoint Relative to API base path.
40
     * @param array  $options  Options for the request.
41
     *
42
     * @return mixed
43
     */
44
    public function request($method, $endpoint, array $options = [])
45
    {
46
        $this->lastOptions = $options;
47
        try {
48
            $response = $this->client->request($method, $endpoint, $options);
49
        } catch (ClientException $e) {
50
            $response = $e->getResponse();
51
        }
52
53
        return $this->response($response);
0 ignored issues
show
Bug introduced by
It seems like $response defined by $e->getResponse() on line 50 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...
54
    }
55
56
    /**
57
     * @param \Psr\Http\Message\ResponseInterface $response
58
     *
59
     * @return object
60
     */
61
    public function response(ResponseInterface $response)
62
    {
63
        $this->lastResponse = $response;
64
65
        $content = $response->getBody()->getContents();
66
67
        $decoded = json_decode($content); // parse as object
68
        reset($decoded);
69
        $data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors.
70
71
        $this->checkRateLimit($response)
72
            ->checkForErrors($response, $data);
73
74
        return $data;
75
    }
76
77
    /**
78
     * @param \Psr\Http\Message\ResponseInterface $response
79
     *
80
     * @return $this
81
     * @throws \Vindi\Exceptions\RateLimitException
82
     */
83
    private function checkRateLimit(ResponseInterface $response)
84
    {
85
        if (429 === $response->getStatusCode()) {
86
            throw new RateLimitException($response);
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param \Psr\Http\Message\ResponseInterface $response
94
     * @param mixed                     $data
95
     *
96
     * @return $this
97
     * @throws \Vindi\Exceptions\RequestException
98
     */
99
    private function checkForErrors(ResponseInterface $response, $data)
100
    {
101
        $status = $response->getStatusCode();
102
103
        $data = (array) $data;
104
105
        $statusClass = (int) ($status / 100);
106
107
        if (($statusClass === 4) || ($statusClass === 5)) {
108
            switch ($status) {
109
                case 422:
110
                    throw new ValidationException($status, $data, $this->lastOptions);
111
                default:
112
                    throw new RequestException($status, $data, $this->lastOptions);
113
            }
114
        }
115
116
        return $this;
117
    }
118
}
119