Passed
Push — master ( f2f3ef...f406b6 )
by Nils
01:48
created

UptimeCheck::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\System;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\Result;
7
8
class UptimeCheck implements Check
9
{
10
    const IDENTIFIER = 'base:system:uptime';
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)) {
32
            return new Result(Result::STATUS_FAIL, 'Servers uptime is too high (' . $this->dateIntervalToString($uptime) . ')');
33
        } else {
34
            return new Result(Result::STATUS_PASS, 'Servers uptime is ok (' . $this->dateIntervalToString($uptime) . ')');
35
        }
36
    }
37
38
    /**
39
     * @return \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 = $systemStartDate->diff($now);
48
49
        return $uptime;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $uptime could also return false which is incompatible with the documented return type DateInterval. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
50
    }
51
52
    private function dateIntervalToSeconds(\DateInterval $dateInterval)
53
    {
54
        $reference = new \DateTimeImmutable;
55
        $endTime = $reference->add($dateInterval);
56
57
        return $endTime->getTimestamp() - $reference->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