1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the `tvi/monitor-bundle` project. |
5
|
|
|
* |
6
|
|
|
* (c) https://github.com/turnaev/monitor-bundle/graphs/contributors |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Tvi\MonitorBundle\Reporter; |
13
|
|
|
|
14
|
|
|
use ZendDiagnostics\Result\FailureInterface; |
15
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
16
|
|
|
use ZendDiagnostics\Result\SkipInterface; |
17
|
|
|
use ZendDiagnostics\Result\SuccessInterface; |
18
|
|
|
use ZendDiagnostics\Result\WarningInterface; |
19
|
|
|
use ZendDiagnostics\Runner\Reporter\ReporterInterface; |
20
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
21
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
22
|
|
|
|
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @author Vladimir Turnaev <[email protected]> |
25
|
|
|
*/ |
|
|
|
|
26
|
|
|
abstract class ReporterAbstract implements ReporterInterface |
27
|
|
|
{ |
28
|
|
|
const STATUS_CODE_SUCCESS = 0; |
29
|
|
|
const STATUS_CODE_WARNING = 100; |
30
|
|
|
const STATUS_CODE_SKIP = 200; |
31
|
|
|
const STATUS_CODE_UNKNOWN = 300; |
32
|
|
|
const STATUS_CODE_FAILURE = 1000; |
33
|
|
|
|
34
|
|
|
const STATUS_NAME_SUCCESS = 'SUCCESS'; |
35
|
|
|
const STATUS_NAME_WARNING = 'WARNING'; |
36
|
|
|
const STATUS_NAME_SKIP = 'SKIP'; |
37
|
|
|
const STATUS_NAME_UNKNOWN = 'UNKNOWN'; |
38
|
|
|
const STATUS_NAME_FAILURE = 'FAILURE'; |
39
|
|
|
|
40
|
|
|
public static $STATUS_MAP = [ |
41
|
|
|
self::STATUS_CODE_SUCCESS => self::STATUS_NAME_SUCCESS, |
42
|
|
|
self::STATUS_CODE_WARNING => self::STATUS_NAME_WARNING, |
43
|
|
|
self::STATUS_CODE_SKIP => self::STATUS_NAME_SKIP, |
44
|
|
|
self::STATUS_CODE_UNKNOWN => self::STATUS_NAME_UNKNOWN, |
45
|
|
|
self::STATUS_CODE_FAILURE => self::STATUS_NAME_FAILURE, |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Total number of Checks. |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
protected $totalCount = 0; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Has the Runner operation been aborted (stopped) ? |
57
|
|
|
* |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $stopped = false; |
61
|
|
|
|
62
|
|
|
/** |
|
|
|
|
63
|
|
|
* @var ResultsCollection |
64
|
|
|
*/ |
65
|
|
|
protected $results; |
66
|
|
|
|
67
|
56 |
|
public static function getStatusNameByCode(int $statusCode): string |
|
|
|
|
68
|
|
|
{ |
69
|
56 |
|
return static::$STATUS_MAP[$statusCode] ?? static::STATUS_NAME_UNKNOWN; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This method is called right after Reporter starts running, via Runner::run(). |
74
|
|
|
* |
75
|
|
|
* @param ArrayObject $checks A collection of Checks that will be performed |
|
|
|
|
76
|
|
|
* @param array $runnerConfig Complete Runner configuration, obtained via Runner::getConfig() |
77
|
|
|
*/ |
|
|
|
|
78
|
57 |
|
public function onStart(\ArrayObject $checks, $runnerConfig) |
79
|
|
|
{ |
80
|
57 |
|
$this->stopped = false; |
81
|
|
|
|
82
|
57 |
|
$this->totalCount = \count($checks); |
83
|
57 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* This method is called before each individual Check is performed. If this |
87
|
|
|
* method returns false, the Check will not be performed (will be skipped). |
88
|
|
|
* |
89
|
|
|
* @param CheckInterface $check check instance that is about to be performed |
90
|
|
|
* @param string|null $checkAlias The alias for the check that is about to be performed |
91
|
|
|
* |
92
|
|
|
* @return bool|void Return false to prevent check from happening |
93
|
|
|
*/ |
94
|
47 |
|
public function onBeforeRun(CheckInterface $check, $checkAlias = null) |
95
|
|
|
{ |
96
|
47 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* This method is called every time a Check has been performed. If this method |
100
|
|
|
* returns false, the Runner will not perform any additional checks and stop |
101
|
|
|
* its run. |
102
|
|
|
* |
103
|
|
|
* @param CheckInterface $check A Check instance that has just finished running |
104
|
|
|
* @param ResultInterface $result Result for that particular check instance |
105
|
|
|
* @param string|null $checkAlias The alias for the check that has just finished |
106
|
|
|
* |
107
|
|
|
* @return bool|void Return false to prevent from running additional Checks |
108
|
|
|
*/ |
109
|
|
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
110
|
|
|
{ |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* This method is called when Runner has been aborted and could not finish the |
115
|
|
|
* whole run(). |
116
|
|
|
* |
117
|
|
|
* @param ResultsCollection $results collection of Results for performed Checks |
118
|
|
|
*/ |
|
|
|
|
119
|
2 |
|
public function onStop(ResultsCollection $results) |
120
|
|
|
{ |
121
|
2 |
|
$this->stopped = true; |
122
|
2 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* This method is called when Runner has finished its run. |
126
|
|
|
* |
127
|
|
|
* @param ResultsCollection $results collection of Results for performed Checks |
128
|
|
|
*/ |
|
|
|
|
129
|
64 |
|
public function onFinish(ResultsCollection $results) |
130
|
|
|
{ |
131
|
64 |
|
$this->results = $results; |
132
|
|
|
|
133
|
|
|
// Display information that the test has been aborted. |
134
|
64 |
|
if ($this->stopped) { |
135
|
2 |
|
$this->onStopped(); |
136
|
|
|
} |
137
|
64 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
|
|
|
|
140
|
|
|
* @return ?int |
141
|
|
|
*/ |
142
|
14 |
|
public function getSuccessCount() |
143
|
|
|
{ |
144
|
14 |
|
return $this->results ? $this->results->getSuccessCount() : null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
|
|
|
|
148
|
|
|
* @return ?int |
149
|
|
|
*/ |
150
|
14 |
|
public function getWarningCount() |
151
|
|
|
{ |
152
|
14 |
|
return $this->results ? $this->results->getWarningCount() : null; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
|
|
|
|
156
|
|
|
* @return ?int |
157
|
|
|
*/ |
158
|
14 |
|
public function getFailureCount() |
159
|
|
|
{ |
160
|
14 |
|
return $this->results ? $this->results->getFailureCount() : null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
|
|
|
|
164
|
|
|
* @return ?int |
165
|
|
|
*/ |
166
|
|
|
public function getSkipCount() |
167
|
|
|
{ |
168
|
|
|
return $this->results ? $this->results->getSkipCount() : null; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
|
|
|
|
172
|
|
|
* @return ?int |
173
|
|
|
*/ |
174
|
14 |
|
public function getUnknownCount() |
175
|
|
|
{ |
176
|
14 |
|
return $this->results ? $this->results->getUnknownCount() : null; |
177
|
|
|
} |
178
|
|
|
|
179
|
32 |
|
public function getTotalCount(): int |
|
|
|
|
180
|
|
|
{ |
181
|
32 |
|
return $this->totalCount; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
public function isStopped(): bool |
|
|
|
|
185
|
|
|
{ |
186
|
|
|
return $this->stopped; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function getResults(): ResultsCollection |
|
|
|
|
190
|
|
|
{ |
191
|
|
|
return $this->results; |
192
|
|
|
} |
193
|
|
|
|
194
|
2 |
|
protected function onStopped() |
|
|
|
|
195
|
|
|
{ |
196
|
2 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
|
|
|
|
199
|
|
|
* @return array [name, code] |
200
|
|
|
*/ |
201
|
47 |
|
protected function getStatusByResul(ResultInterface $result) |
202
|
|
|
{ |
203
|
|
|
switch (true) { |
204
|
47 |
|
case $result instanceof SuccessInterface: |
|
|
|
|
205
|
27 |
|
$name = static::STATUS_NAME_SUCCESS; |
206
|
27 |
|
$code = static::STATUS_CODE_SUCCESS; |
207
|
27 |
|
break; |
208
|
|
|
|
209
|
33 |
|
case $result instanceof WarningInterface: |
|
|
|
|
210
|
11 |
|
$name = static::STATUS_NAME_WARNING; |
211
|
11 |
|
$code = static::STATUS_CODE_WARNING; |
212
|
11 |
|
break; |
213
|
|
|
|
214
|
27 |
|
case $result instanceof SkipInterface: |
|
|
|
|
215
|
11 |
|
$name = static::STATUS_NAME_SKIP; |
216
|
11 |
|
$code = static::STATUS_CODE_SKIP; |
217
|
11 |
|
break; |
218
|
|
|
|
219
|
21 |
|
case $result instanceof FailureInterface: |
|
|
|
|
220
|
21 |
|
$name = static::STATUS_NAME_FAILURE; |
221
|
21 |
|
$code = static::STATUS_CODE_FAILURE; |
222
|
21 |
|
break; |
223
|
|
|
|
224
|
|
|
default: |
|
|
|
|
225
|
|
|
$name = static::STATUS_NAME_UNKNOWN; |
226
|
|
|
$code = static::STATUS_CODE_UNKNOWN; |
227
|
|
|
break; |
228
|
|
|
} |
229
|
|
|
|
230
|
47 |
|
return [$name, $code]; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|