System   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 55
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerOs() 0 14 4
A getOs() 0 3 1
A __construct() 0 8 1
1
<?php
2
/**
3
 * @package     Cronfig Sysinfo Library
4
 * @link        https://github.com/cronfig/sysinfo
5
 * @license     http://opensource.org/licenses/MIT
6
 */
7
8
namespace Cronfig\Sysinfo;
9
10
use Cronfig\Sysinfo\Linux;
11
use Cronfig\Sysinfo\Mac;
12
use Cronfig\Sysinfo\OsInterface;
13
14
/**
15
 * Class System
16
 */
17
class System
18
{
19
    /**
20
     * OS object representation matching current OS
21
     *
22
     * @var OsInterface
23
     */
24
    protected $os;
25
26
    /**
27
     * Constructor
28
     *
29
     * @param array $customs Array of custom OS class names can be added here
30
     */
31
    public function __construct(array $customs = [])
32
    {
33
        $defaults = [
34
            Linux::class,
35
            Mac::class,
36
        ];
37
38
        $this->registerOs(array_merge($customs, $defaults));
39
    }
40
41
    /**
42
     * Register array of OS class and pick the one in use
43
     *
44
     * @param  array  $classNames
45
     */
46
    public function registerOs(array $classNames)
47
    {
48
        foreach ($classNames as $className) {
49
            $os = new $className;
50
51
            if (!$os instanceof OsInterface) {
52
                throw new \UnexpectedValueException("Class {$className} must implement ".OsInterface::class);
53
            }
54
55
            if ($os->inUse()) {
56
                $this->os = $os;
57
58
                // The OS has been found, no need to iterate anymore
59
                break;
60
            }
61
        }
62
    }
63
64
    /**
65
     * Returns currently used OS object
66
     *
67
     * @return OsInterface
68
     */
69
    public function getOs()
70
    {
71
        return $this->os;
72
    }
73
}
74