Passed
Push — master ( 7901b7...23a0ea )
by Nils
01:55
created

UptimeCheck::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Device;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
class UptimeCheck implements Check
9
{
10
    const IDENTIFIER = 'base:device:spaceUsed';
11
12
    /**
13
     * @var \DateInterval
14
     */
15
    private $dateInterval;
16
17
    public function init($maxUptime)
18
    {
19
        $this->dateInterval = \DateInterval::createFromDateString($maxUptime);
20
    }
21
22
    /**
23
     * Checks if the server uptime is ok
24
     *
25
     * @return Result
26
     */
27
    public function run()
28
    {
29
        $uptime = $this->getUptime();
30
31
        if ($this->dateIntervalToSeconds($uptime) > $this->dateIntervalToSeconds($this->dateInterval)) {
0 ignored issues
show
Bug introduced by
It seems like $uptime can also be of type false; however, parameter $dateInterval of Leankoala\HealthFoundati...dateIntervalToSeconds() does only seem to accept DateInterval, maybe add an additional type check? ( Ignorable by Annotation )

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

31
        if ($this->dateIntervalToSeconds(/** @scrutinizer ignore-type */ $uptime) > $this->dateIntervalToSeconds($this->dateInterval)) {
Loading history...
32
            return new Result(Result::STATUS_FAIL, 'Servers uptime is too high (' . $this->dateIntervalToString($uptime) . ')');
0 ignored issues
show
Bug introduced by
It seems like $uptime can also be of type false; however, parameter $dateInterval of Leankoala\HealthFoundati...:dateIntervalToString() does only seem to accept DateInterval, maybe add an additional type check? ( Ignorable by Annotation )

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

32
            return new Result(Result::STATUS_FAIL, 'Servers uptime is too high (' . $this->dateIntervalToString(/** @scrutinizer ignore-type */ $uptime) . ')');
Loading history...
33
        } else {
34
            return new Result(Result::STATUS_PASS, 'Servers uptime is ok (' . $this->dateIntervalToString($uptime) . ')');
35
        }
36
    }
37
38
    /**
39
     * @return bool|\DateInterval
40
     * @throws \Exception
41
     */
42
    private function getUptime()
43
    {
44
        $systemStartDate = new \DateTime(date('Y-m-d H:i:s', \uptime()));
0 ignored issues
show
Bug introduced by
uptime() of type double is incompatible with the type integer expected by parameter $timestamp of date(). ( Ignorable by Annotation )

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

44
        $systemStartDate = new \DateTime(date('Y-m-d H:i:s', /** @scrutinizer ignore-type */ \uptime()));
Loading history...
45
        $now = new \DateTime();
46
47
        $uptime = $now->diff($systemStartDate);
48
49
        return $uptime;
50
    }
51
52
    private function dateIntervalToSeconds(\DateInterval $dateInterval)
53
    {
54
        $reference = new \DateTimeImmutable;
55
        $endTime = $reference->add($dateInterval);
56
57
        return $reference->getTimestamp() - $endTime->getTimestamp();
58
    }
59
60
    private function dateIntervalToString(\DateInterval $dateInterval)
61
    {
62
        $string = '';
63
64
        if ($dateInterval->y > 0) {
65
            $string .= $dateInterval->y . ' years ';
66
        }
67
68
        if ($dateInterval->d > 0) {
69
            $string .= $dateInterval->d . ' days ';
70
        }
71
72
        if ($dateInterval->h > 0) {
73
            $string .= $dateInterval->h . ' hours ';
74
        }
75
76
        if ($dateInterval->i > 0) {
77
            $string .= $dateInterval->i . ' minutes ';
78
        }
79
80
        return trim($string);
81
    }
82
83
    public function getIdentifier()
84
    {
85
        return self::IDENTIFIER;
86
    }
87
}
88