1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Leankoala\HealthFoundation; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
use Leankoala\HealthFoundation\Check\Check; |
7
|
|
|
use Leankoala\HealthFoundation\Check\HttpClientAwareCheck; |
8
|
|
|
use Leankoala\HealthFoundation\Check\CacheAwareCheck; |
9
|
|
|
use Leankoala\HealthFoundation\Extenstion\Cache\Cache; |
10
|
|
|
|
11
|
|
|
class HealthFoundation |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Check[] |
15
|
|
|
*/ |
16
|
|
|
private $registeredChecks = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
private $httpClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Client $httpClient |
25
|
|
|
*/ |
26
|
|
|
public function setHttpClient(Client $httpClient) |
27
|
|
|
{ |
28
|
|
|
$this->httpClient = $httpClient; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return the current httpClient if set otherwise create one. |
33
|
|
|
* |
34
|
|
|
* @return Client |
35
|
|
|
*/ |
36
|
|
|
private function getHttpClient() |
37
|
|
|
{ |
38
|
|
|
if (!$this->httpClient) { |
39
|
|
|
$this->httpClient = new Client(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->httpClient; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Return the current cache abstraction |
47
|
|
|
* |
48
|
|
|
* @return Cache |
49
|
|
|
*/ |
50
|
|
|
private function getCache(CacheAwareCheck $check) |
51
|
|
|
{ |
52
|
|
|
return new Cache($check->getIdentifier()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function registerCheck(Check $check, $identifier = false, $description = "", $group = "") |
56
|
|
|
{ |
57
|
|
|
if ($check instanceof HttpClientAwareCheck) { |
58
|
|
|
$check->setHttpClient($this->getHttpClient()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($check instanceof CacheAwareCheck) { |
62
|
|
|
$check->setCache($this->getCache($check)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($identifier) { |
66
|
|
|
$this->registeredChecks[$identifier] = ['check' => $check, 'description' => $description, 'group' => $group]; |
67
|
|
|
} else { |
68
|
|
|
$this->registeredChecks[] = ['check' => $check, 'description' => $description, 'group' => $group]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function runHealthCheck() |
73
|
|
|
{ |
74
|
|
|
$runResult = new RunResult(); |
75
|
|
|
|
76
|
|
|
foreach ($this->registeredChecks as $identifier => $checkInfos) { |
77
|
|
|
/** @var Check $check */ |
78
|
|
|
$check = $checkInfos['check']; |
79
|
|
|
$group = $checkInfos['group']; |
80
|
|
|
|
81
|
|
|
$checkResult = $check->run(); |
82
|
|
|
$runResult->addResult($check, $checkResult, $identifier, $checkInfos['description'], $group); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $runResult; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|