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 (#28)
by
unknown
01:39
created

ApiRequester   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 5.88%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 109
ccs 2
cts 34
cp 0.0588
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A request() 0 13 2
A response() 0 15 1
A checkRateLimit() 0 8 2
A checkForErrors() 0 19 4
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
        \Yii::$app->logHandler->run(print_r(json_decode($response->getBody()->getContents(),true),true), 'APIRequest');
54
55
        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...
56
    }
57
58
    /**
59
     * @param \Psr\Http\Message\ResponseInterface $response
60
     *
61
     * @return object
62
     */
63
    public function response(ResponseInterface $response)
64
    {
65
        $this->lastResponse = $response;
66
67
        $content = $response->getBody()->getContents();
68
69
        $decoded = json_decode($content, true); // parse as object
70
        reset($decoded);
71
        $data = current($decoded); // get first attribute from array, e.g.: subscription, subscriptions, errors.
72
73
        $this->checkRateLimit($response)
74
            ->checkForErrors($response, $data);
75
76
        return $data;
77
    }
78
79
    /**
80
     * @param \Psr\Http\Message\ResponseInterface $response
81
     *
82
     * @return $this
83
     * @throws \Vindi\Exceptions\RateLimitException
84
     */
85
    private function checkRateLimit(ResponseInterface $response)
86
    {
87
        if (429 === $response->getStatusCode()) {
88
            throw new RateLimitException($response);
89
        }
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param \Psr\Http\Message\ResponseInterface $response
96
     * @param mixed                     $data
97
     *
98
     * @return $this
99
     * @throws \Vindi\Exceptions\RequestException
100
     */
101
    private function checkForErrors(ResponseInterface $response, $data)
102
    {
103
        $status = $response->getStatusCode();
104
105
        $data = (array) $data;
106
107
        $statusClass = (int) ($status / 100);
108
109
        if (($statusClass === 4) || ($statusClass === 5)) {
110
            switch ($status) {
111
                case 422:
112
                    throw new ValidationException($status, $data, $this->lastOptions);
113
                default:
114
                    throw new RequestException($status, $data, $this->lastOptions);
115
            }
116
        }
117
118
        return $this;
119
    }
120
}
121