Completed
Push — master ( aca6e3...e7aa24 )
by Vladimir
05:21
created

ReporterAbstract::onBeforeRun()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 2
c 0
b 0
f 0
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Reporter;
13
14
use ZendDiagnostics\Result\FailureInterface;
15
use ZendDiagnostics\Result\ResultInterface;
16
use ZendDiagnostics\Result\SkipInterface;
17
use ZendDiagnostics\Result\SuccessInterface;
18
use ZendDiagnostics\Result\WarningInterface;
19
use ZendDiagnostics\Runner\Reporter\ReporterInterface;
20
use ZendDiagnostics\Check\CheckInterface;
21
use ZendDiagnostics\Result\Collection as ResultsCollection;
22
23
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
24
 * @author Vladimir Turnaev <[email protected]>
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
abstract class ReporterAbstract implements ReporterInterface
27
{
28
    public const STATUS_CODE_SUCCESS = 0;
29
    public const STATUS_CODE_WARNING = 100;
30
    public const STATUS_CODE_SKIP = 200;
31
    public const STATUS_CODE_UNKNOWN = 300;
32
    public const STATUS_CODE_FAILURE = 1000;
33
34
    public const STATUS_NAME_SUCCESS = 'SUCCESS';
35
    public const STATUS_NAME_WARNING = 'WARNING';
36
    public const STATUS_NAME_SKIP = 'SKIP';
37
    public const STATUS_NAME_UNKNOWN = 'UNKNOWN';
38
    public const STATUS_NAME_FAILURE = 'FAILURE';
39
40
    public static $STATUS_MAP = [
41
        self::STATUS_CODE_SUCCESS => self::STATUS_NAME_SUCCESS,
42
        self::STATUS_CODE_WARNING => self::STATUS_NAME_WARNING,
43
        self::STATUS_CODE_SKIP => self::STATUS_NAME_SKIP,
44
        self::STATUS_CODE_UNKNOWN => self::STATUS_NAME_UNKNOWN,
45
        self::STATUS_CODE_FAILURE => self::STATUS_NAME_FAILURE,
46
    ];
47
48
    /**
49
     * Total number of Checks.
50
     *
51
     * @var int
52
     */
53
    protected $totalCount = 0;
54
55
    /**
56
     * Has the Runner operation been aborted (stopped) ?
57
     *
58
     * @var bool
59
     */
60
    protected $stopped = false;
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @var ResultsCollection
64
     */
65
    protected $results;
66
67
    public static function getStatusNameByCode(int $statusCode): string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getStatusNameByCode()
Loading history...
68
    {
69
        return isset(self::$STATUS_MAP[$statusCode]) ? self::$STATUS_MAP[$statusCode] : self::STATUS_NAME_UNKNOWN;
70
    }
71
72
    /**
73
     * This method is called right after Reporter starts running, via Runner::run().
74
     *
75
     * @param ArrayObject $checks       A collection of Checks that will be performed
0 ignored issues
show
Bug introduced by
The type Tvi\MonitorBundle\Reporter\ArrayObject was not found. Did you mean ArrayObject? If so, make sure to prefix the type with \.
Loading history...
76
     * @param array       $runnerConfig Complete Runner configuration, obtained via Runner::getConfig()
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function onStart(\ArrayObject $checks, $runnerConfig)
79
    {
80
        $this->totalCount = \count($checks);
81
    }
82
83
    /**
84
     * This method is called before each individual Check is performed. If this
85
     * method returns false, the Check will not be performed (will be skipped).
86
     *
87
     * @param CheckInterface $check      check instance that is about to be performed
88
     * @param string|null    $checkAlias The alias for the check that is about to be performed
89
     *
90
     * @return bool|void Return false to prevent check from happening
91
     */
92
    public function onBeforeRun(CheckInterface $check, $checkAlias = null)
93
    {
94
    }
95
96
    /**
97
     * This method is called every time a Check has been performed. If this method
98
     * returns false, the Runner will not perform any additional checks and stop
99
     * its run.
100
     *
101
     * @param CheckInterface  $check      A Check instance that has just finished running
102
     * @param ResultInterface $result     Result for that particular check instance
103
     * @param string|null     $checkAlias The alias for the check that has just finished
104
     *
105
     * @return bool|void Return false to prevent from running additional Checks
106
     */
107
    public function onAfterRun(CheckInterface $check, ResultInterface $result, $checkAlias = null)
108
    {
109
    }
110
111
    /**
112
     * This method is called when Runner has been aborted and could not finish the
113
     * whole run().
114
     *
115
     * @param ResultsCollection $results collection of Results for performed Checks
116
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
117
    public function onStop(ResultsCollection $results)
118
    {
119
        $this->stopped = true;
120
    }
121
122
    /**
123
     * This method is called when Runner has finished its run.
124
     *
125
     * @param ResultsCollection $results collection of Results for performed Checks
126
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
127 7
    public function onFinish(ResultsCollection $results)
128
    {
129 7
        $this->results = $results;
130 7
    }
131
132
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
133
     * @return int
134
     */
135
    public function getSuccessCount(): ?int
136
    {
137
        return $this->results ? $this->results->getSuccessCount() : null;
138
    }
139
140
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
141
     * @return int
142
     */
143
    public function getWarningCount(): ?int
144
    {
145
        return $this->results ? $this->results->getWarningCount() : null;
146
    }
147
148
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
149
     * @return int
150
     */
151
    public function getFailureCount(): ?int
152
    {
153
        return $this->results ? $this->results->getFailureCount() : null;
154
    }
155
156
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
157
     * @return int
158
     */
159
    public function getSkipCount(): ?int
160
    {
161
        return $this->results ? $this->results->getSkipCount() : null;
162
    }
163
164
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
165
     * @return int
166
     */
167
    public function getUnknownCount(): ?int
168
    {
169
        return $this->results ? $this->results->getUnknownCount() : null;
170
    }
171
172
    public function getTotalCount(): int
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getTotalCount()
Loading history...
173
    {
174
        return $this->totalCount;
175
    }
176
177
    public function isStopped(): bool
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isStopped()
Loading history...
178
    {
179
        return $this->stopped;
180
    }
181
182
    public function getResults(): ResultsCollection
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getResults()
Loading history...
183
    {
184
        return $this->results;
185
    }
186
187
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $result should have a doc-comment as per coding-style.
Loading history...
188
     * @return array [name, code]
189
     */
190
    protected function getStatusByResul(ResultInterface $result)
191
    {
192
        switch (true) {
193
            case $result instanceof SuccessInterface:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
194
                $name = self::STATUS_NAME_SUCCESS;
195
                $code = self::STATUS_CODE_SUCCESS;
196
                break;
197
198
            case $result instanceof WarningInterface:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
199
                $name = self::STATUS_NAME_WARNING;
200
                $code = self::STATUS_CODE_WARNING;
201
                break;
202
203
            case $result instanceof SkipInterface:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
204
                $name = self::STATUS_NAME_SKIP;
205
                $code = self::STATUS_CODE_SKIP;
206
                break;
207
208
            case $result instanceof FailureInterface:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
209
                $name = self::STATUS_NAME_FAILURE;
210
                $code = self::STATUS_CODE_FAILURE;
211
                break;
212
213
            default:
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
214
                $name = self::STATUS_NAME_UNKNOWN;
215
                $code = self::STATUS_CODE_UNKNOWN;
216
                break;
217
        }
218
219
        return [$name, $code];
220
    }
221
}
222