Completed
Pull Request — master (#223)
by
unknown
15:19
created

ArrayReporter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 105
ccs 0
cts 53
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 4 1
A getGlobalStatus() 0 4 1
A onAfterRun() 0 4 1
A onStart() 0 4 1
A onBeforeRun() 0 4 1
A onStop() 0 4 1
A onFinish() 0 4 1
A prepareResult() 0 33 4
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 CheckInterface $check
82
     * @param ResultInterface $result
83
     * @param $checkAlias
84
     *
85
     * @return array
86
     */
87
    public function prepareResult(CheckInterface $check, ResultInterface $result, $checkAlias): array
88
    {
89
        switch (true) {
90
            case $result instanceof SuccessInterface:
91
                $status = 0;
92
                $statusName = 'check_result_ok';
93
                break;
94
95
            case $result instanceof WarningInterface:
96
                $status = 1;
97
                $statusName = 'check_result_warning';
98
                $this->globalStatus = self::STATUS_KO;
99
                break;
100
101
            case $result instanceof SkipInterface:
102
                $status = 2;
103
                $statusName = 'check_result_skip';
104
                break;
105
106
            default:
107
                $status = 3;
108
                $statusName = 'check_result_critical';
109
                $this->globalStatus = self::STATUS_KO;
110
        }
111
112
        return [
113
            'checkName' => $check->getLabel(),
114
            'message' => $result->getMessage(),
115
            'status' => $status,
116
            'status_name' => $statusName,
117
            'service_id' => $checkAlias,
118
        ];
119
    }
120
}
121