System   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getSystem() 0 4 1
A getBoottime() 0 4 1
A getUptime() 0 4 1
A getTime() 0 8 1
A createFromContext() 0 4 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