KoalityFormat   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 79
c 3
b 0
f 0
dl 0
loc 146
rs 10
wmc 23

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A comvertToDataProtectedResult() 0 10 2
F handle() 0 91 14
A getOutput() 0 17 4
1
<?php
2
3
namespace Leankoala\HealthFoundation\Result\Format\Koality;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\MetricAwareResult;
7
use Leankoala\HealthFoundation\Check\Result;
8
use Leankoala\HealthFoundation\Result\Format\Format;
9
use Leankoala\HealthFoundation\RunResult;
10
11
class KoalityFormat implements Format
12
{
13
    const DEFAULT_OUTPUT_PASS = 'The health check was passed.';
14
    const DEFAULT_OUTPUT_WARN = 'Warning.';
15
    const DEFAULT_OUTPUT_FAIL = 'The health check failed.';
16
17
    private $passMessage = self::DEFAULT_OUTPUT_PASS;
18
    private $failMessage = self::DEFAULT_OUTPUT_FAIL;
19
20
    private $dataProctection = false;
21
22
    public function __construct($passMessage = null, $failMessage = null, $dataProctection = false)
23
    {
24
        if ($passMessage) {
25
            $this->passMessage = $passMessage;
26
        }
27
28
        if ($failMessage) {
29
            $this->failMessage = $failMessage;
30
        }
31
32
        $this->dataProctection = $dataProctection;
33
    }
34
35
    private function comvertToDataProtectedResult(Result $result)
36
    {
37
        if ($result instanceof MetricAwareResult) {
38
            $result->setLimitType(Result::LIMIT_TYPE_MIN);
39
            $result->setLimit(1);
40
            $result->setObservedValuePrecision(2);
41
            $result->setMetric(1, 'percent', MetricAwareResult::METRIC_TYPE_PERCENT);
42
        }
43
44
        return $result;
45
    }
46
47
    public function handle(RunResult $runResult, $echoValue = true)
48
    {
49
        header('Content-Type: application/json');
50
51
        $output = $this->getOutput($runResult, $this->passMessage, $this->failMessage);
52
53
        $details = [];
54
55
        foreach ($runResult->getResults() as $resultArray) {
56
57
            /** @var Result $result */
58
            $result = $resultArray['result'];
59
60
            if ($this->dataProctection) {
61
                $result = $this->comvertToDataProtectedResult($result);
62
            }
63
64
            /** @var Check $check */
65
            $check = $resultArray['check'];
66
67
            if (is_string($resultArray['identifier'])) {
68
                $identifier = $resultArray['identifier'];
69
            } else {
70
                $identifier = $check->getIdentifier();
71
            }
72
73
            $details[$identifier] = [
74
                'status' => $result->getStatus(),
75
                'output' => $result->getMessage()
76
            ];
77
78
            /** @var string $group */
79
            $group = $resultArray['group'];
80
81
            if($group) {
82
                $details[$identifier]['group'] = $group;
83
            }
84
85
            $description = $resultArray['description'];
86
            if ($description) {
87
                $details[$identifier]['description'] = $description;
88
            }
89
90
            if ($result instanceof MetricAwareResult) {
91
                $details[$identifier]["observedValue"] = $result->getMetricValue();
92
                $details[$identifier]["observedUnit"] = $result->getMetricUnit();
93
94
                if ($result->getMetricType()) {
95
                    $details[$identifier]['metricType'] = $result->getMetricType();
96
                }
97
98
                if (!is_null($result->getObservedValuePrecision())) {
99
                    $details[$identifier]['observedValuePrecision'] = $result->getObservedValuePrecision();
100
                }
101
102
                if (is_numeric($result->getLimit())) {
103
                    $details[$identifier]['limit'] = $result->getLimit();
104
                }
105
106
                if (!is_null($result->getLimitType())) {
107
                    $details[$identifier]['limitType'] = $result->getLimitType();
108
                }
109
            } else {
110
                if ($result->getStatus() == Result::STATUS_PASS) {
111
                    $details[$identifier]["observedValue"] = 1;
112
                } else {
113
                    $details[$identifier]["observedValue"] = 0;
114
                }
115
                $details[$identifier]["metricType"] = MetricAwareResult::METRIC_TYPE_PERCENT;
116
                $details[$identifier]["observedUnit"] = 'percent';
117
            }
118
119
            $attributes = $result->getAttributes();
120
            if (count($attributes) > 0) {
121
                $details[$identifier]['attributes'] = $attributes;
122
            }
123
        }
124
125
        $resultArray = [
126
            'status' => $runResult->getStatus(),
127
            'output' => $output,
128
            'checks' => $details
129
        ];
130
131
        $resultJson = json_encode($resultArray, JSON_PRETTY_PRINT);
132
133
        if ($echoValue) {
134
            echo $resultJson;
135
        }
136
137
        return $resultArray;
138
    }
139
140
    private function getOutput(RunResult $runResult, $passMessage = null, $failMessage = null)
141
    {
142
        if ($runResult->getStatus() == Result::STATUS_PASS) {
143
            if (is_null($passMessage)) {
144
                $output = self::DEFAULT_OUTPUT_PASS;
145
            } else {
146
                $output = $passMessage;
147
            }
148
        } else {
149
            if (is_null($failMessage)) {
150
                $output = self::DEFAULT_OUTPUT_FAIL;
151
            } else {
152
                $output = $failMessage;
153
            }
154
        }
155
156
        return $output;
157
    }
158
}
159