Passed
Push — master ( fa6d7a...69655a )
by Nils
04:07
created

DailyFilter::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Leankoala\HealthFoundation\Filter\Time;
4
5
use Leankoala\HealthFoundation\Check\MetricAwareResult;
6
use Leankoala\HealthFoundation\Check\Result;
7
use Leankoala\HealthFoundation\Filter\BasicFilter;
8
9
class DailyFilter extends BasicFilter
10
{
11
    private $hour;
12
    private $reason = false;
13
14
    public function init($hour, $reason = false)
15
    {
16
        $this->hour = $hour;
17
        $this->reason = $reason;
18
    }
19
20
    public function run()
21
    {
22
        $result = $this->getCheck()->run();
23
24
        $currentHour = (int)date('H');
25
26
        if ($result->getStatus() == Result::STATUS_FAIL) {
27
            if ($currentHour == $this->hour) {
28
                if ($this->reason) {
29
                    $message = $this->reason . ' (fail reason was: ' . $result->getMessage() . ')';
0 ignored issues
show
Bug introduced by
Are you sure $this->reason of type true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
                    $message = /** @scrutinizer ignore-type */ $this->reason . ' (fail reason was: ' . $result->getMessage() . ')';
Loading history...
30
                } else {
31
                    $message = 'Passed due daily filter (fail reason was: ' . $result->getMessage() . ')';
32
                }
33
34
                if ($result instanceof MetricAwareResult) {
35
                    $newResult = new MetricAwareResult(Result::STATUS_PASS, $message);
36
                    $newResult->setMetric($result->getMetricValue(), $result->getMetricUnit());
37
                } else {
38
                    $newResult = new Result(Result::STATUS_PASS, $message);
39
                }
40
41
                return $newResult;
42
            } else {
43
                return $result;
44
            }
45
        } else {
46
            return $result;
47
        }
48
    }
49
}
50