Completed
Push — master ( 033231...4a0dad )
by Kevin
12s queued 10s
created

ArrayReporter::onFinish()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Liip\MonitorBundle\Helper;
4
5
use ZendDiagnostics\Check\CheckInterface;
6
use ZendDiagnostics\Result\Collection as ResultsCollection;
7
use ZendDiagnostics\Result\ResultInterface;
8
use ZendDiagnostics\Result\SkipInterface;
9
use ZendDiagnostics\Result\SuccessInterface;
10
use ZendDiagnostics\Result\WarningInterface;
11
use ZendDiagnostics\Runner\Reporter\ReporterInterface;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
class ArrayReporter implements ReporterInterface
17
{
18
    const STATUS_OK = 'OK';
19
    const STATUS_KO = 'KO';
20
21
    private $globalStatus = self::STATUS_OK;
22
    private $results = [];
23
24
    /**
25
     * @return array
26
     */
27
    public function getResults()
28
    {
29
        return $this->results;
30
    }
31
32
    /**
33
     * @return string
34
     */
35
    public function getGlobalStatus()
36
    {
37
        return $this->globalStatus;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
44
    {
45
        $this->results[] = $this->prepareResult($check, $result, $checkAlias);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function onStart(\ArrayObject $checks, $runnerConfig)
52
    {
53
        return;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function onBeforeRun(CheckInterface $check, $checkAlias = null)
60
    {
61
        return;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function onStop(ResultsCollection $results)
68
    {
69
        return;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function onFinish(ResultsCollection $results)
76
    {
77
        return;
78
    }
79
80
    /**
81
     * @param $checkAlias
82
     */
83
    public function prepareResult(CheckInterface $check, ResultInterface $result, $checkAlias): array
84
    {
85
        switch (true) {
86
            case $result instanceof SuccessInterface:
87
                $status = 0;
88
                $statusName = 'check_result_ok';
89
                break;
90
91
            case $result instanceof WarningInterface:
92
                $status = 1;
93
                $statusName = 'check_result_warning';
94
                $this->globalStatus = self::STATUS_KO;
95
                break;
96
97
            case $result instanceof SkipInterface:
98
                $status = 2;
99
                $statusName = 'check_result_skip';
100
                break;
101
102
            default:
103
                $status = 3;
104
                $statusName = 'check_result_critical';
105
                $this->globalStatus = self::STATUS_KO;
106
        }
107
108
        return [
109
            'checkName' => $check->getLabel(),
110
            'message' => $result->getMessage(),
111
            'status' => $status,
112
            'status_name' => $statusName,
113
            'service_id' => $checkAlias,
114
        ];
115
    }
116
}
117