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
|
|
|
|