Boottime   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 4
Bugs 2 Features 1
Metric Value
c 4
b 2
f 1
dl 0
loc 33
rs 10
wmc 2
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 10 1
A convertWmicOffset() 0 7 1
1
<?php
2
3
namespace Uptime\Runtime\Windows;
4
5
use Uptime\Runtime\RuntimeInterface;
6
7
class Boottime implements RuntimeInterface
8
{
9
    /**
10
     * Reads the wmic command which returns the last bootup time and converts it to a unix timestamp
11
     *
12
     * @param string $command wmic command
13
     * @return int
14
     */
15
    public function read($command = 'wmic os get lastbootuptime')
16
    {
17
        $wmicString = trim(explode("\n", shell_exec($command))[1]);
18
19
        $dateTime = \DateTime::createFromFormat(
20
            'YmdHis.uO',
21
            $this->convertWmicOffset($wmicString)
22
        );
23
        return $dateTime->getTimestamp();
24
    }
25
26
    /**
27
     * Takes the output of wmic os get lastbootuptime and converts the offset given in minutes to an HHMM offset acceptable by PHP createFromFormat 'O'
28
     *
29
     * @param string $wmicString string output given by wmic command
30
     * @return string
31
     */
32
    private function convertWmicOffset($wmicString)
33
    {
34
        $offset = substr($wmicString, -3);
35
        $hours = floor($offset / 60);
36
        $minutes = ($offset % 60);
37
        return substr($wmicString, 0, -3) . sprintf('%02d%02d', $hours, $minutes);
38
    }
39
}
40