1 | <?php |
||
2 | |||
3 | namespace SilverStripe\EnvironmentCheck; |
||
4 | |||
5 | use InvalidArgumentException; |
||
6 | use SilverStripe\ORM\ArrayList; |
||
7 | use SilverStripe\View\ArrayData; |
||
8 | use SilverStripe\View\ViewableData; |
||
9 | |||
10 | /** |
||
11 | * A single set of results from running an EnvironmentCheckSuite |
||
12 | * |
||
13 | * @package environmentcheck |
||
14 | */ |
||
15 | class EnvironmentCheckSuiteResult extends ViewableData |
||
16 | { |
||
17 | /** |
||
18 | * @var ArrayList |
||
19 | */ |
||
20 | protected $details; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $worst = 0; |
||
26 | |||
27 | public function __construct() |
||
28 | { |
||
29 | parent::__construct(); |
||
30 | $this->details = new ArrayList(); |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param int $status |
||
35 | * @param string $message |
||
36 | * @param string $checkIdentifier |
||
37 | */ |
||
38 | public function addResult($status, $message, $checkIdentifier) |
||
39 | { |
||
40 | $this->details->push(new ArrayData([ |
||
41 | 'Check' => $checkIdentifier, |
||
42 | 'Status' => $this->statusText($status), |
||
43 | 'StatusCode' => $status, |
||
44 | 'Message' => $message, |
||
45 | ])); |
||
46 | |||
47 | $this->worst = max($this->worst, $status); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * Returns true if there are no errors. |
||
52 | * |
||
53 | * @return bool |
||
54 | */ |
||
55 | public function ShouldPass() |
||
56 | { |
||
57 | return $this->worst <= EnvironmentCheck::WARNING; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns overall (i.e. worst) status as a string. |
||
62 | * |
||
63 | * @return string |
||
64 | */ |
||
65 | public function Status() |
||
66 | { |
||
67 | return $this->statusText($this->worst); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Returns detailed status information about each check. |
||
72 | * |
||
73 | * @return ArrayList |
||
74 | */ |
||
75 | public function Details() |
||
76 | { |
||
77 | return $this->details; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Convert the final result status and details to JSON. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function toJSON() |
||
86 | { |
||
87 | $result = [ |
||
88 | 'Status' => $this->Status(), |
||
89 | 'ShouldPass' => $this->ShouldPass(), |
||
90 | 'Checks' => [] |
||
91 | ]; |
||
92 | foreach ($this->details as $detail) { |
||
93 | $result['Checks'][] = $detail->toMap(); |
||
94 | } |
||
95 | return json_encode($result); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Return a text version of a status code. |
||
100 | * |
||
101 | * @param int $status |
||
102 | * @return string |
||
103 | * @throws InvalidArgumentException |
||
104 | */ |
||
105 | protected function statusText($status) |
||
106 | { |
||
107 | switch ($status) { |
||
108 | case EnvironmentCheck::ERROR: |
||
109 | return 'ERROR'; |
||
110 | break; |
||
0 ignored issues
–
show
|
|||
111 | case EnvironmentCheck::WARNING: |
||
112 | return 'WARNING'; |
||
113 | break; |
||
114 | case EnvironmentCheck::OK: |
||
115 | return 'OK'; |
||
116 | break; |
||
117 | case 0: |
||
118 | return 'NO CHECKS'; |
||
119 | break; |
||
120 | default: |
||
121 | throw new InvalidArgumentException("Bad environment check status '$status'"); |
||
122 | break; |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 |
The
break
statement is not necessary if it is preceded for example by areturn
statement:If you would like to keep this construct to be consistent with other
case
statements, you can safely mark this issue as a false-positive.