Passed
Push — master ( bff9c8...c1feec )
by Nils
02:07
created

IetfFormat::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 4
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Result\Format\Ietf;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
use Leankoala\HealthFoundation\Result\Format\Format;
8
use Leankoala\HealthFoundation\RunResult;
9
10
class IetfFormat implements Format
11
{
12
    const DEFAULT_OUTPUT_PASS = 'The health check was passed.';
13
    const DEFAULT_OUTPUT_WARN = 'Warning.';
14
    const DEFAULT_OUTPUT_FAIL = 'The health check failed.';
15
16
    private $passMessage = self::DEFAULT_OUTPUT_PASS;
17
    private $failMessage = self::DEFAULT_OUTPUT_FAIL;
18
19
    public function __construct($passMessage = null, $failMessage = null)
20
    {
21
        if ($passMessage) {
22
            $this->passMessage = $passMessage;
23
        }
24
25
        if ($failMessage) {
26
            $this->failMessage = $failMessage;
27
        }
28
    }
29
30
    public function handle(RunResult $runResult)
31
    {
32
        header('Content-Type: application/json');
33
34
        $output = $this->getOutput($runResult, $this->passMessage, $this->failMessage);
35
36
        $details = [];
37
38
        foreach ($runResult->getResults() as $resultArray) {
39
            /** @var Result $result */
40
            $result = $resultArray['result'];
41
42
            /** @var Check $check */
43
            $check = $resultArray['check'];
44
45
46
            if (is_string($resultArray['identifier'])) {
47
                $identifier = $resultArray['identifier'];
48
            } else {
49
                $identifier = $check->getIdentifier();
50
            }
51
52
            $details[$identifier] = [
53
                'status' => $result->getStatus(),
54
                'output' => $result->getMessage()
55
            ];
56
57
            $description = $resultArray['description'];
58
            if ($description) {
59
                $details[$identifier]['description'] = $description;
60
            }
61
        }
62
63
        $resultArray = [
64
            'status' => $runResult->getStatus(),
65
            'output' => $output,
66
            'details' => $details
67
        ];
68
69
        echo json_encode($resultArray, JSON_PRETTY_PRINT);
70
    }
71
72
    private function getOutput(RunResult $runResult, $passMessage = null, $failMessage = null)
73
    {
74
        if ($runResult->getStatus() == Result::STATUS_PASS) {
75
            if (is_null($passMessage)) {
76
                $output = self::DEFAULT_OUTPUT_PASS;
77
            } else {
78
                $output = $passMessage;
79
            }
80
        } else {
81
            if (is_null($failMessage)) {
82
                $output = self::DEFAULT_OUTPUT_FAIL;
83
            } else {
84
                $output = $failMessage;
85
            }
86
        }
87
88
        return $output;
89
    }
90
}
91