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 ( 35bec3...9db1c3 )
by Pascal
01:48
created

AbstractHttpChecker::throwExceptionByResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Pbmedia\ApiHealth\Checkers;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\ClientException;
7
use GuzzleHttp\Exception\ConnectException;
8
use Psr\Http\Message\ResponseInterface;
9
10
abstract class AbstractHttpChecker extends AbstractChecker
11
{
12
    /**
13
     * The Guzzle HTTP client
14
     *
15
     * @var \GuzzleHttp\Client
16
     */
17
    private $httpClient;
18
19
    /**
20
     * Guzzle options
21
     *
22
     * @var array
23
     */
24
    private $guzzleOptions;
25
26
    /**
27
     * The URL that must be requested.
28
     *
29
     * @var string
30
     */
31
    protected $url;
32
33
    /**
34
     * The method of the request.
35
     *
36
     * @var string
37
     */
38
    protected $method = 'GET';
39
40
    /**
41
     * Creates a new instance of this checker with a Guzzle HTTP client and options.
42
     *
43
     * @param \GuzzleHttp\Client $httpClient
44
     * @param array  $guzzleOptions
45
     */
46
    public function __construct(Client $httpClient, array $guzzleOptions = [])
47
    {
48
        $this->httpClient    = $httpClient;
49
        $this->guzzleOptions = $guzzleOptions;
50
    }
51
52
    /**
53
     * Requests the URL and handles any thrown exceptions.
54
     *
55
     * @return null
56
     */
57
    public function run()
58
    {
59
        $method = strtolower($this->method);
60
61
        try {
62
            $response = $this->httpClient->{$method}($this->url, $this->guzzleOptions);
63
        } catch (ClientException $exception) {
64
            $this->throwExceptionByResponse($exception->getResponse());
0 ignored issues
show
Bug introduced by
It seems like $exception->getResponse() can be null; however, throwExceptionByResponse() 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...
65
        } catch (ConnectException $exception) {
66
            $this->throwConnectException($exception);
67
        }
68
69
        $statusCode = $response->getStatusCode();
70
71
        if ($statusCode >= 200 && $statusCode < 300) {
72
            return;
73
        }
74
75
        $this->throwExceptionByResponse($response);
76
    }
77
78
    /**
79
     * Maps a ConnectException into a CheckerHasFailed exception and throws it.
80
     *
81
     * @param  \GuzzleHttp\Exception\ConnectException $exception
82
     *
83
     * @throws \Pbmedia\ApiHealth\Checkers\CheckerHasFailed
84
     * @return null
85
     */
86
    private function throwConnectException(ConnectException $exception)
87
    {
88
        throw new CheckerHasFailed("GET request to \"{$this->url}\" failed, client message: {$exception->getMessage()}");
89
    }
90
91
    /**
92
     * Maps a Response into a CheckerHasFailed exception and throws it.
93
     *
94
     * @param  \Psr\Http\Message\ResponseInterface $response
95
     *
96
     * @throws \Pbmedia\ApiHealth\Checkers\CheckerHasFailed
97
     * @return null
98
     */
99
    private function throwExceptionByResponse(ResponseInterface $response)
100
    {
101
        throw new CheckerHasFailed("GET request to \"{$this->url}\" failed, returned status code {$response->getStatusCode()} and reason phrase: \"{$response->getReasonPhrase()}\"");
102
    }
103
}
104