1 | <?php |
||||
2 | |||||
3 | namespace ProtoneMedia\ApiHealth\Checkers; |
||||
4 | |||||
5 | use ProtoneMedia\ApiHealth\Checkers\Checker; |
||||
6 | use ProtoneMedia\ApiHealth\Checkers\CheckerHasFailed; |
||||
7 | use ProtoneMedia\ApiHealth\Storage\CheckerState; |
||||
8 | |||||
9 | class Executor |
||||
10 | { |
||||
11 | /** |
||||
12 | * The checker. |
||||
13 | * |
||||
14 | * @var \ProtoneMedia\ApiHealth\Checkers\Checker |
||||
15 | */ |
||||
16 | private $checker; |
||||
17 | |||||
18 | /** |
||||
19 | * The state of the checker. |
||||
20 | * |
||||
21 | * @var \ProtoneMedia\ApiHealth\Storage\CheckerState |
||||
22 | */ |
||||
23 | private $state; |
||||
24 | |||||
25 | /** |
||||
26 | * The caught exception if the checker fails. |
||||
27 | * |
||||
28 | * @var \ProtoneMedia\ApiHealth\Checkers\CheckerHasFailed |
||||
29 | */ |
||||
30 | private $exception; |
||||
31 | |||||
32 | /** |
||||
33 | * Boolean wether the checker has failed of not. |
||||
34 | * |
||||
35 | * @var bool |
||||
36 | */ |
||||
37 | private $failed; |
||||
38 | |||||
39 | /** |
||||
40 | * Creates an instance with the given checker |
||||
41 | * |
||||
42 | * @param \ProtoneMedia\ApiHealth\Checkers\Checker $checker |
||||
43 | */ |
||||
44 | public function __construct(Checker $checker) |
||||
45 | { |
||||
46 | $this->checker = $checker; |
||||
47 | $this->state = new CheckerState($checker); |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * Shortcut for creating an instance for a checker class. |
||||
52 | * |
||||
53 | * @param string $checkerClass |
||||
54 | * @return \ProtoneMedia\ApiHealth\Checkers\Executor |
||||
55 | */ |
||||
56 | public static function make(string $checkerClass) |
||||
57 | { |
||||
58 | return new static($checkerClass::create()); |
||||
59 | } |
||||
60 | |||||
61 | /** |
||||
62 | * Returns a boolean wether the checker passes. |
||||
63 | * |
||||
64 | * @return bool |
||||
65 | */ |
||||
66 | public function passes(): bool |
||||
67 | { |
||||
68 | $this->handle(); |
||||
69 | |||||
70 | return !$this->failed; |
||||
71 | } |
||||
72 | |||||
73 | /** |
||||
74 | * Returns a boolean wether the checker fails. |
||||
75 | * |
||||
76 | * @return bool |
||||
77 | */ |
||||
78 | public function fails(): bool |
||||
79 | { |
||||
80 | return !$this->passes(); |
||||
81 | } |
||||
82 | |||||
83 | /** |
||||
84 | * Returns the checker. |
||||
85 | * |
||||
86 | * @return \ProtoneMedia\ApiHealth\Checkers\Checker |
||||
87 | */ |
||||
88 | public function getChecker(): Checker |
||||
89 | { |
||||
90 | return $this->checker; |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * Returns the caught exception. |
||||
95 | * |
||||
96 | * @return \ProtoneMedia\ApiHealth\Checkers\CheckerHasFailed |
||||
97 | */ |
||||
98 | public function getException(): CheckerHasFailed |
||||
99 | { |
||||
100 | return $this->exception; |
||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Runs the checker, stores the state and lets events take |
||||
105 | * care of sending the notifications. |
||||
106 | * |
||||
107 | * @return $this |
||||
108 | */ |
||||
109 | public function handle() |
||||
110 | { |
||||
111 | $this->failed = false; |
||||
112 | |||||
113 | try { |
||||
114 | $this->checker->run(); |
||||
115 | $this->state->setToPassing(); |
||||
116 | } catch (CheckerHasFailed $exception) { |
||||
117 | if ($this->state->retryIsAllowed()) { |
||||
118 | $this->handleAllowedRetry(); |
||||
119 | } else { |
||||
120 | $this->exception = $exception; |
||||
121 | $this->failed = true; |
||||
122 | $this->handleFailedChecker(); |
||||
123 | } |
||||
124 | } |
||||
125 | |||||
126 | return $this; |
||||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * Handler for whenever the checker fails. Stores the state or adds a timestamp |
||||
131 | * to the state if the checker previously failed. |
||||
132 | * |
||||
133 | * @return null |
||||
134 | */ |
||||
135 | private function handleFailedChecker() |
||||
136 | { |
||||
137 | if ($this->state->exists() && $this->state->isFailing()) { |
||||
138 | return $this->state->addFailedTimestamp($this->exception); |
||||
0 ignored issues
–
show
|
|||||
139 | } |
||||
140 | |||||
141 | $this->state->setToFailed($this->exception); |
||||
142 | } |
||||
143 | |||||
144 | /** |
||||
145 | * Adds a retry timestamp to the state of checker or dispaches |
||||
146 | * the retry job. |
||||
147 | * |
||||
148 | * @return null |
||||
149 | */ |
||||
150 | private function handleAllowedRetry() |
||||
151 | { |
||||
152 | if (!$this->state->exists()) { |
||||
153 | $this->state->setToPassing(); |
||||
154 | } |
||||
155 | |||||
156 | if (!$jobClass = $this->checker->retryJob()) { |
||||
0 ignored issues
–
show
The method
retryJob() does not exist on ProtoneMedia\ApiHealth\Checkers\Checker . It seems like you code against a sub-type of ProtoneMedia\ApiHealth\Checkers\Checker such as ProtoneMedia\ApiHealth\Checkers\AbstractChecker .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
157 | return $this->state->addRetryTimestamp(); |
||||
0 ignored issues
–
show
Are you sure the usage of
$this->state->addRetryTimestamp() targeting ProtoneMedia\ApiHealth\S...te::addRetryTimestamp() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. ![]() |
|||||
158 | } |
||||
159 | |||||
160 | $job = new $jobClass($this->checker); |
||||
161 | |||||
162 | $this->configureRetryJobDefaults($job, config('api-health.retries.job')); |
||||
163 | |||||
164 | if (method_exists($this->checker, 'withRetryJob')) { |
||||
165 | $this->checker->withRetryJob($job); |
||||
166 | } |
||||
167 | |||||
168 | dispatch($job); |
||||
169 | } |
||||
170 | |||||
171 | /** |
||||
172 | * Sets the default connection, delay and queue |
||||
173 | * on the retry job. |
||||
174 | * |
||||
175 | * @param mixed $job |
||||
176 | * @param array $config |
||||
177 | */ |
||||
178 | private function configureRetryJobDefaults($job, array $config) |
||||
179 | { |
||||
180 | if ($connection = $config['connection'] ?? null) { |
||||
181 | $job->onConnection($connection); |
||||
182 | } |
||||
183 | |||||
184 | if ($delay = $config['delay'] ?? null) { |
||||
185 | $job->delay($delay); |
||||
186 | } |
||||
187 | |||||
188 | if ($queue = $config['queue'] ?? null) { |
||||
189 | $job->onQueue($queue); |
||||
190 | } |
||||
191 | } |
||||
192 | } |
||||
193 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.