1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pbmedia\ApiHealth\Checkers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Notifications\Notification; |
6
|
|
|
use Pbmedia\ApiHealth\Checkers\Checker; |
7
|
|
|
use Pbmedia\ApiHealth\Checkers\CheckerHasFailed; |
8
|
|
|
use Pbmedia\ApiHealth\Storage\CheckerState; |
9
|
|
|
|
10
|
|
|
class Executor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The checker. |
14
|
|
|
* |
15
|
|
|
* @var \Pbmedia\ApiHealth\Checkers\Checker |
16
|
|
|
*/ |
17
|
|
|
private $checker; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The state of the checker. |
21
|
|
|
* |
22
|
|
|
* @var \Pbmedia\ApiHealth\Storage\CheckerState |
23
|
|
|
*/ |
24
|
|
|
private $state; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The caught exception if the checker fails. |
28
|
|
|
* |
29
|
|
|
* @var \Pbmedia\ApiHealth\Checkers\CheckerHasFailed |
30
|
|
|
*/ |
31
|
|
|
private $exception; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Boolean wether the checker has failed of not. |
35
|
|
|
* |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
private $failed; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates an instance with the given checker |
42
|
|
|
* |
43
|
|
|
* @param \Pbmedia\ApiHealth\Checkers\Checker $checker |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Checker $checker) |
46
|
|
|
{ |
47
|
|
|
$this->checker = $checker; |
48
|
|
|
$this->state = new CheckerState($checker); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Shortcut for creating an instance for a checker class. |
53
|
|
|
* |
54
|
|
|
* @param string $checkerClass |
55
|
|
|
* @return \Pbmedia\ApiHealth\Checkers\Executor |
56
|
|
|
*/ |
57
|
|
|
public static function make(string $checkerClass) |
58
|
|
|
{ |
59
|
|
|
return new static($checkerClass::create()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a boolean wether the checker passes. |
64
|
|
|
* |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function passes(): bool |
68
|
|
|
{ |
69
|
|
|
if (is_null($this->failed)) { |
70
|
|
|
$this->handle(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return !$this->failed; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns a boolean wether the checker fails. |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function fails(): bool |
82
|
|
|
{ |
83
|
|
|
return !$this->passes(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the checker. |
88
|
|
|
* |
89
|
|
|
* @return \Pbmedia\ApiHealth\Checkers\Checker |
90
|
|
|
*/ |
91
|
|
|
public function getChecker(): Checker |
92
|
|
|
{ |
93
|
|
|
return $this->checker; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the caught exception. |
98
|
|
|
* |
99
|
|
|
* @return \Pbmedia\ApiHealth\Checkers\CheckerHasFailed |
100
|
|
|
*/ |
101
|
|
|
public function getException(): CheckerHasFailed |
102
|
|
|
{ |
103
|
|
|
return $this->exception; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Runs the checker, stores the state and lets events take |
108
|
|
|
* care of sending the notifications. |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function handle() |
113
|
|
|
{ |
114
|
|
|
$this->failed = false; |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
$this->checker->run(); |
118
|
|
|
$this->state->setToPassing(); |
119
|
|
|
} catch (CheckerHasFailed $exception) { |
120
|
|
|
$this->exception = $exception; |
121
|
|
|
$this->failed = true; |
122
|
|
|
$this->handleFailedChecker(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Handler for whenever the checker fails. Stores the state or adds a timestamp |
130
|
|
|
* to the state if the checker previously failed. |
131
|
|
|
* |
132
|
|
|
* @return null |
133
|
|
|
*/ |
134
|
|
|
private function handleFailedChecker() |
135
|
|
|
{ |
136
|
|
|
if ($this->state->exists() && $this->state->isFailing()) { |
137
|
|
|
return $this->state->addFailedTimestamp($this->exception); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->state->setToFailed($this->exception); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|