1
|
|
|
<?php |
2
|
|
|
namespace Health\Checks\Network; |
3
|
|
|
|
4
|
|
|
use Health\Checks\BaseCheck; |
5
|
|
|
use Health\Checks\HealthCheckInterface; |
6
|
|
|
use Health\Checks\Traits\HttpClientTrait; |
7
|
|
|
|
8
|
|
|
class Http extends BaseCheck implements HealthCheckInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
use HttpClientTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
* @see \Health\Checks\HealthCheckInterface::call() |
17
|
|
|
*/ |
18
|
|
|
public function call() |
19
|
|
|
{ |
20
|
|
|
$builder = $this->getBuilder(); |
21
|
|
|
|
22
|
|
|
try { |
23
|
|
|
$uri = $this->getParam('uri'); |
24
|
|
|
$options = $this->getParam('options', []); |
25
|
|
|
$method = strtoupper($this->getParam('method', 'GET')); |
26
|
|
|
$status = $this->getParam('status', null); |
27
|
|
|
$result = $this->getParam('result', null); |
28
|
|
|
|
29
|
|
|
/** @var \Psr\Http\Message\ResponseInterface $response */ |
30
|
|
|
$response = $this->getHttpClient()->request($method, $uri, $options); |
31
|
|
|
|
32
|
|
|
$statusCode = $response->getStatusCode(); |
33
|
|
|
|
34
|
|
|
$up = ($statusCode >= 200 && $statusCode < 300) ? true : false; |
35
|
|
|
|
36
|
|
|
if ($status !== null) { |
37
|
|
|
$up = ($status == $statusCode); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($result !== null) { |
41
|
|
|
if (preg_match("|{$result}|", $response->getBody()->getContents())) { |
42
|
|
|
$up = false; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$builder->state($up) |
47
|
|
|
->withData('uri', $uri) |
48
|
|
|
->withData('status_code', $statusCode) |
49
|
|
|
->withData('options', $options); |
50
|
|
|
} catch (\Exception $exception) { |
51
|
|
|
$builder->down()->withData('error', $exception->getMessage()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $builder->build(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|