System::getBoottime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace Uptime;
4
5
use Uptime\System\SystemInterface;
6
use Uptime\System\SystemTable;
7
use Uptime\System\UnsupportedSystemException;
8
9
/**
10
 * System
11
 *
12
 * Provides a cross-platform way to figure out the system uptime and boottime
13
 * Should work on most common operating systems supported by PHP
14
 *
15
 */
16
class System implements SystemInterface
17
{
18
19
    const ERROR = 'System "%s" is not supported yet. Patches welcome :)';
20
21
    const RUNTIME_CLASS = '\Uptime\Runtime\\%s\\%s';
22
23
    protected $system;
24
25
    public function __construct($system = PHP_OS)
26
    {
27
        $this->system = SystemTable::getSystemIdentifier( $system );
28
        if (!$this->system) {
29
            throw new UnsupportedSystemException( sprintf( self::ERROR, $system ) );
30
        }
31
    }
32
33
    public function getSystem()
34
    {
35
        return $this->system;
36
    }
37
38
    public function getBoottime()
39
    {
40
        return $this->getTime('Boottime');
41
    }
42
43
    public function getUptime()
44
    {
45
        return $this->getTime('Uptime');
46
    }
47
48
    private function getTime($value_object)
49
    {
50
        $class = sprintf(self::RUNTIME_CLASS, $this->system,  $value_object);
51
        $runtime = new $class();
52
        $value_object = 'Uptime\\' . $value_object;
53
54
        return new $value_object( $runtime->read() );
55
    }
56
57
    public static function createFromContext()
58
    {
59
        return new self();
60
    }
61
}
62