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.

AbstractHttpChecker   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 19 5
A throwExceptionByResponse() 0 3 1
A throwConnectException() 0 3 1
A __construct() 0 4 1
1
<?php
2
3
namespace ProtoneMedia\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());
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 \ProtoneMedia\ApiHealth\Checkers\CheckerHasFailed
84
     * @return null
85
     */
86
    private function throwConnectException(ConnectException $exception)
87
    {
88
        throw new CheckerHasFailed("{$this->method} 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 \ProtoneMedia\ApiHealth\Checkers\CheckerHasFailed
97
     * @return null
98
     */
99
    private function throwExceptionByResponse(ResponseInterface $response)
100
    {
101
        throw new CheckerHasFailed("{$this->method} request to \"{$this->url}\" failed, returned status code {$response->getStatusCode()} and reason phrase: \"{$response->getReasonPhrase()}\"");
102
    }
103
}
104