Passed
Push — master ( c79f04...a6b429 )
by Nils
02:28
created

KoalityFormat::handle()   C

Complexity

Conditions 11
Paths 145

Size

Total Lines 74
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 42
c 1
b 0
f 0
nc 145
nop 1
dl 0
loc 74
rs 6.9416

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct($passMessage = null, $failMessage = null)
21
    {
22
        if ($passMessage) {
23
            $this->passMessage = $passMessage;
24
        }
25
26
        if ($failMessage) {
27
            $this->failMessage = $failMessage;
28
        }
29
    }
30
31
    public function handle(RunResult $runResult)
32
    {
33
        header('Content-Type: application/json');
34
35
        $output = $this->getOutput($runResult, $this->passMessage, $this->failMessage);
36
37
        $details = [];
38
39
        foreach ($runResult->getResults() as $resultArray) {
40
            /** @var Result $result */
41
            $result = $resultArray['result'];
42
43
            /** @var Check $check */
44
            $check = $resultArray['check'];
45
46
47
            if (is_string($resultArray['identifier'])) {
48
                $identifier = $resultArray['identifier'];
49
            } else {
50
                $identifier = $check->getIdentifier();
51
            }
52
53
            $details[$identifier] = [
54
                'status' => $result->getStatus(),
55
                'output' => $result->getMessage()
56
            ];
57
58
            $description = $resultArray['description'];
59
            if ($description) {
60
                $details[$identifier]['description'] = $description;
61
            }
62
63
            if ($result instanceof MetricAwareResult) {
64
                $details[$identifier]["observedValue"] = $result->getMetricValue();
65
                $details[$identifier]["observedUnit"] = $result->getMetricUnit();
66
67
                if ($result->getMetricType()) {
68
                    $details[$identifier]['metricType'] = $result->getMetricType();
69
                }
70
71
                if (!is_null($result->getObservedValuePrecision())) {
72
                    $details[$identifier]['observedValuePrecision'] = $result->getObservedValuePrecision();
73
                }
74
75
                if (is_numeric($result->getLimit())) {
76
                    $details[$identifier]['limit'] = $result->getLimit();
77
                }
78
79
                if (!is_null($result->getLimitType())) {
80
                    $details[$identifier]['limitType'] = $result->getLimitType();
81
                }
82
            } else {
83
                if ($result->getStatus() == Result::STATUS_PASS) {
84
                    $details[$identifier]["observedValue"] = 1;
85
                } else {
86
                    $details[$identifier]["observedValue"] = 0;
87
                }
88
                $details[$identifier]["metricType"] = MetricAwareResult::METRIC_TYPE_PERCENT;
89
                $details[$identifier]["observedUnit"] = 'percent';
90
            }
91
92
            $attributes = $result->getAttributes();
93
            if (count($attributes) > 0) {
94
                $details['attributes'] = $attributes;
95
            }
96
        }
97
98
        $resultArray = [
99
            'status' => $runResult->getStatus(),
100
            'output' => $output,
101
            'checks' => $details
102
        ];
103
104
        echo json_encode($resultArray, JSON_PRETTY_PRINT);
105
    }
106
107
    private function getOutput(RunResult $runResult, $passMessage = null, $failMessage = null)
108
    {
109
        if ($runResult->getStatus() == Result::STATUS_PASS) {
110
            if (is_null($passMessage)) {
111
                $output = self::DEFAULT_OUTPUT_PASS;
112
            } else {
113
                $output = $passMessage;
114
            }
115
        } else {
116
            if (is_null($failMessage)) {
117
                $output = self::DEFAULT_OUTPUT_FAIL;
118
            } else {
119
                $output = $failMessage;
120
            }
121
        }
122
123
        return $output;
124
    }
125
}
126