1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Koality\ShopwarePlugin\Formatter; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class KoalityFormatter |
7
|
|
|
* |
8
|
|
|
* This class is used to create an IETF conform health JSON that can be |
9
|
|
|
* read by koality.io. |
10
|
|
|
* |
11
|
|
|
* @see https://tools.ietf.org/html/draft-inadarei-api-health-check-05 |
12
|
|
|
* |
13
|
|
|
* @package Koality\ShopwarePlugin\Formatter |
14
|
|
|
* |
15
|
|
|
* @author Nils Langner <[email protected]> |
16
|
|
|
* created 2020-12-28 |
17
|
|
|
*/ |
18
|
|
|
class KoalityFormatter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Result[] |
22
|
|
|
*/ |
23
|
|
|
private $results = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add a new result. |
27
|
|
|
* |
28
|
|
|
* If the status of the result is "fail" the whole check will be marked as failed. |
29
|
|
|
* |
30
|
|
|
* @param Result $result |
31
|
|
|
*/ |
32
|
|
|
public function addResult(Result $result) |
33
|
|
|
{ |
34
|
|
|
$this->results[] = $result; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return an IETF conform result array with all sub results. |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function getFormattedResults() |
43
|
|
|
{ |
44
|
|
|
$formattedResult = []; |
45
|
|
|
$checks = []; |
46
|
|
|
$status = Result::STATUS_PASS; |
47
|
|
|
|
48
|
|
|
foreach ($this->results as $result) { |
49
|
|
|
$check = [ |
50
|
|
|
'status' => $result->getStatus(), |
51
|
|
|
'output' => $result->getMessage() |
52
|
|
|
]; |
53
|
|
|
|
54
|
|
|
if (is_numeric($result->getLimit())) { |
55
|
|
|
$check['limit'] = $result->getLimit(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!is_null($result->getLimitType())) { |
59
|
|
|
$check['limitType'] = $result->getLimitType(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!is_null($result->getObservedValue())) { |
63
|
|
|
$check['observedValue'] = $result->getObservedValue(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!is_null($result->getObservedValueUnit())) { |
67
|
|
|
$check['observedUnit'] = $result->getObservedValueUnit(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!is_null($result->getObservedValuePrecision())) { |
71
|
|
|
$check['observedValuePrecision'] = $result->getObservedValuePrecision(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!is_null($result->getType())) { |
75
|
|
|
$check['metricType'] = $result->getType(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$attributes = $result->getAttributes(); |
79
|
|
|
if (count($attributes) > 0) { |
80
|
|
|
$check['attributes'] = $attributes; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$checks[$result->getKey()] = $check; |
84
|
|
|
|
85
|
|
|
if ($result->getStatus() == Result::STATUS_FAIL) { |
86
|
|
|
$status = Result::STATUS_FAIL; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$formattedResult['status'] = $status; |
91
|
|
|
$formattedResult['output'] = $this->getOutput($status); |
92
|
|
|
|
93
|
|
|
$formattedResult['checks'] = $checks; |
94
|
|
|
|
95
|
|
|
$formattedResult['info'] = $this->getInfoBlock(); |
96
|
|
|
|
97
|
|
|
return $formattedResult; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the output string depending on the given status. |
102
|
|
|
* |
103
|
|
|
* @param string $status |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
private function getOutput($status) |
108
|
|
|
{ |
109
|
|
|
if ($status === Result::STATUS_PASS) { |
110
|
|
|
return 'All Shopware6 health metrics passed.'; |
111
|
|
|
} else { |
112
|
|
|
return 'Some Shopware6 health metrics failed: '; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return the info block for the JSON output |
118
|
|
|
* |
119
|
|
|
* @return string[] |
120
|
|
|
*/ |
121
|
|
|
private function getInfoBlock() |
122
|
|
|
{ |
123
|
|
|
return [ |
124
|
|
|
'creator' => 'koality.io Shopware Plugin', |
125
|
|
|
'version' => '1.0.0', |
126
|
|
|
'plugin_url' => 'https://www.koality.io/plugins/shopware' |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|