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 ArrayObject; |
21
|
|
|
use ZendDiagnostics\Check\CheckInterface; |
22
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
23
|
|
|
|
24
|
|
|
/** |
|
|
|
|
25
|
|
|
* @author Vladimir Turnaev <[email protected]> |
26
|
|
|
*/ |
|
|
|
|
27
|
|
|
abstract class AbstractReporter implements ReporterInterface |
28
|
|
|
{ |
29
|
|
|
public const STATUS_CODE_SUCCESS = 0; |
30
|
|
|
public const STATUS_CODE_WARNING = 100; |
31
|
|
|
public const STATUS_CODE_SKIP = 200; |
32
|
|
|
public const STATUS_CODE_UNKNOWN = 300; |
33
|
|
|
public const STATUS_CODE_FAILURE = 1000; |
34
|
|
|
|
35
|
|
|
public const STATUS_NAME_SUCCESS = 'SUCCESS'; |
36
|
|
|
public const STATUS_NAME_WARNING = 'WARNING'; |
37
|
|
|
public const STATUS_NAME_SKIP = 'SKIP'; |
38
|
|
|
public const STATUS_NAME_UNKNOWN = 'UNKNOWN'; |
39
|
|
|
public const STATUS_NAME_FAILURE = 'FAILURE'; |
40
|
|
|
|
41
|
|
|
public static $STATUS_MAP = [ |
42
|
|
|
self::STATUS_CODE_SUCCESS => self::STATUS_NAME_SUCCESS, |
43
|
|
|
self::STATUS_CODE_WARNING => self::STATUS_NAME_WARNING, |
44
|
|
|
self::STATUS_CODE_SKIP => self::STATUS_NAME_SKIP, |
45
|
|
|
self::STATUS_CODE_UNKNOWN => self::STATUS_NAME_UNKNOWN, |
46
|
|
|
self::STATUS_CODE_FAILURE => self::STATUS_NAME_FAILURE, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
public static function getStatusNameByCode(int $statusCode): string |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
return isset(self::$STATUS_MAP[$statusCode]) ? self::$STATUS_MAP[$statusCode] : self::STATUS_NAME_UNKNOWN; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* This method is called right after Reporter starts running, via Runner::run(). |
56
|
|
|
* |
57
|
|
|
* @param ArrayObject $checks A collection of Checks that will be performed |
58
|
|
|
* @param array $runnerConfig Complete Runner configuration, obtained via Runner::getConfig() |
59
|
|
|
*/ |
|
|
|
|
60
|
|
|
public function onStart(ArrayObject $checks, $runnerConfig) |
61
|
|
|
{ |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* This method is called before each individual Check is performed. If this |
66
|
|
|
* method returns false, the Check will not be performed (will be skipped). |
67
|
|
|
* |
68
|
|
|
* @param CheckInterface $check check instance that is about to be performed |
69
|
|
|
* @param string|null $checkAlias The alias for the check that is about to be performed |
70
|
|
|
* |
71
|
|
|
* @return bool|void Return false to prevent check from happening |
72
|
|
|
*/ |
73
|
|
|
public function onBeforeRun(CheckInterface $check, $checkAlias = null) |
74
|
|
|
{ |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* This method is called every time a Check has been performed. If this method |
79
|
|
|
* returns false, the Runner will not perform any additional checks and stop |
80
|
|
|
* its run. |
81
|
|
|
* |
82
|
|
|
* @param CheckInterface $check A Check instance that has just finished running |
83
|
|
|
* @param ResultInterface $result Result for that particular check instance |
84
|
|
|
* @param string|null $checkAlias The alias for the check that has just finished |
85
|
|
|
* |
86
|
|
|
* @return bool|void Return false to prevent from running additional Checks |
87
|
|
|
*/ |
88
|
|
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
89
|
|
|
{ |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* This method is called when Runner has been aborted and could not finish the |
94
|
|
|
* whole run(). |
95
|
|
|
* |
96
|
|
|
* @param ResultsCollection $results collection of Results for performed Checks |
97
|
|
|
*/ |
|
|
|
|
98
|
|
|
public function onStop(ResultsCollection $results) |
99
|
|
|
{ |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This method is called when Runner has finished its run. |
104
|
|
|
* |
105
|
|
|
* @param ResultsCollection $results collection of Results for performed Checks |
106
|
|
|
*/ |
|
|
|
|
107
|
|
|
public function onFinish(ResultsCollection $results) |
108
|
|
|
{ |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
|
|
|
|
112
|
|
|
* @return array [name, code] |
113
|
|
|
*/ |
114
|
|
|
protected function getStatusByResul(ResultInterface $result) |
115
|
|
|
{ |
116
|
|
|
switch (true) { |
117
|
|
|
case $result instanceof SuccessInterface: |
|
|
|
|
118
|
|
|
$name = self::STATUS_NAME_SUCCESS; |
119
|
|
|
$code = self::STATUS_CODE_SUCCESS; |
120
|
|
|
break; |
121
|
|
|
|
122
|
|
|
case $result instanceof WarningInterface: |
|
|
|
|
123
|
|
|
$name = self::STATUS_NAME_WARNING; |
124
|
|
|
$code = self::STATUS_CODE_WARNING; |
125
|
|
|
break; |
126
|
|
|
|
127
|
|
|
case $result instanceof SkipInterface: |
|
|
|
|
128
|
|
|
$name = self::STATUS_NAME_SKIP; |
129
|
|
|
$code = self::STATUS_CODE_SKIP; |
130
|
|
|
break; |
131
|
|
|
|
132
|
|
|
case $result instanceof FailureInterface: |
|
|
|
|
133
|
|
|
$name = self::STATUS_NAME_FAILURE; |
134
|
|
|
$code = self::STATUS_CODE_FAILURE; |
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
default: |
|
|
|
|
138
|
|
|
$name = self::STATUS_NAME_UNKNOWN; |
139
|
|
|
$code = self::STATUS_CODE_UNKNOWN; |
140
|
|
|
break; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return [$name, $code]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|