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 Tvi\MonitorBundle\Check\CheckInterface; |
15
|
|
|
use ZendDiagnostics\Result\Collection as ResultsCollection; |
16
|
|
|
use ZendDiagnostics\Result\ResultInterface; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author Kevin Bond <[email protected]>, Vladimir Turnaev <[email protected]> |
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
class ArrayReporter extends AbstractReporter |
22
|
|
|
{ |
23
|
|
|
/** |
|
|
|
|
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $statusCode = self::STATUS_CODE_SUCCESS; |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $statusName = self::STATUS_NAME_SUCCESS; |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $checkResults = []; |
37
|
|
|
|
38
|
|
|
/** |
|
|
|
|
39
|
|
|
* @var ResultsCollection |
40
|
|
|
*/ |
41
|
|
|
protected $results; |
42
|
|
|
|
43
|
|
|
/** |
|
|
|
|
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
|
|
|
|
46
|
|
|
public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null) |
47
|
|
|
{ |
48
|
|
|
list($statusName, $statusCode) = $this->getStatusByResul($result); |
49
|
|
|
|
50
|
|
|
$this->statusCode = max($this->statusCode, $statusCode); |
51
|
|
|
|
52
|
|
|
$res = [ |
53
|
|
|
'statusCode' => $statusCode, |
54
|
|
|
'statusName' => $statusName, |
55
|
|
|
'label' => $check->getLabel(), |
56
|
|
|
'check' => $checkAlias, |
57
|
|
|
'message' => $result->getMessage(), |
58
|
|
|
'tags' => $check->getTags(), |
59
|
|
|
'group' => $check->getGroup(), |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
$data = $result->getData(); |
63
|
|
|
if (null !== $data) { |
64
|
|
|
if ($data instanceof \Exception) { |
65
|
|
|
$res['data'] = $data->getMessage(); |
66
|
|
|
} else { |
67
|
|
|
$res['data'] = $data; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$res = array_filter($res, static function ($v) { |
|
|
|
|
72
|
|
|
return \is_array($v) ? !empty($v) : (null !== $v); |
73
|
|
|
}); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->checkResults[] = $res; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
|
|
|
|
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
|
|
public function getStatusCode() |
82
|
|
|
{ |
83
|
|
|
return $this->statusCode; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
|
|
|
|
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getStatusName() |
90
|
|
|
{ |
91
|
|
|
return $this->statusName; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
|
|
|
|
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
|
|
public function getSuccessCount() |
98
|
|
|
{ |
99
|
|
|
return $this->results ? $this->results->getSuccessCount() : null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
|
|
|
|
103
|
|
|
* @return int |
104
|
|
|
*/ |
105
|
|
|
public function getWarningCount() |
106
|
|
|
{ |
107
|
|
|
return $this->results ? $this->results->getWarningCount() : null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* @return int |
112
|
|
|
*/ |
113
|
|
|
public function getFailureCount() |
114
|
|
|
{ |
115
|
|
|
return $this->results ? $this->results->getFailureCount() : null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
|
|
|
|
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
public function getSkipCount() |
122
|
|
|
{ |
123
|
|
|
return $this->results ? $this->results->getSkipCount() : null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
|
|
|
|
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
|
|
public function getUnknownCount() |
130
|
|
|
{ |
131
|
|
|
return $this->results ? $this->results->getUnknownCount() : null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getCheckResults(): array |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return $this->checkResults; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
|
|
|
|
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
|
|
|
|
142
|
|
|
public function onStart(\ArrayObject $checks, $runnerConfig) |
143
|
|
|
{ |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
|
|
|
|
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
|
|
|
|
149
|
|
|
public function onBeforeRun(CheckInterface $check, $checkAlias = null) |
150
|
|
|
{ |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
|
|
|
|
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
|
|
|
|
156
|
|
|
public function onStop(ResultsCollection $results) |
157
|
|
|
{ |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
|
|
|
|
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
|
|
|
|
163
|
|
|
public function onFinish(ResultsCollection $results) |
164
|
|
|
{ |
165
|
|
|
$this->results = $results; |
166
|
|
|
|
167
|
|
|
$this->statusName = self::getStatusNameByCode($this->statusCode); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|