OperatingSystemDetector::getLinuxDistro()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 15
nc 6
nop 0
1
<?php
2
3
namespace Dock\System;
4
5
class OperatingSystemDetector
6
{
7
    const MAC = 1;
8
    const LINUX = 2;
9
    const WINDOWS = 3;
10
11
    private $os;
12
    private $distro;
13
14
    public function __construct()
15
    {
16
        $this->os = strtolower(php_uname('s'));
17
18
        if ($this->isLinux()) {
19
            $this->distro = $this->getLinuxDistro();
20
        }
21
    }
22
23
    public function getOperatingSystem()
24
    {
25
        return $this->os;
26
    }
27
28
    public function getLinuxDistribution()
29
    {
30
        return $this->distro;
31
    }
32
33
    public function isMac()
34
    {
35
        return $this->os === 'darwin';
36
    }
37
38
    public function isLinux()
39
    {
40
        return $this->os === 'linux';
41
    }
42
43
    public function isDebian()
44
    {
45
        return $this->isLinux() && in_array($this->distro, [
46
            'debian',
47
            'ubuntu',
48
            'linuxmint',
49
            'elementary os',
50
            'kali',
51
        ]);
52
    }
53
54
    public function isRedHat()
55
    {
56
        return $this->isLinux() && in_array($this->distro, [
57
            'redhat',
58
            'amzn',
59
            'fedora',
60
            'centos',
61
        ]);
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    private function getLinuxDistro()
68
    {
69
        exec('command -v lsb_release > /dev/null 2>&1', $out, $return);
70
        if ($return === 0 && false) {
71
            $distro = shell_exec('lsb_release -si');
72
        } elseif (file_exists('/etc/lsb-release')) {
73
            $distro = shell_exec('. /etc/lsb-release && echo "$DISTRIB_ID"');
74
        } elseif (file_exists('/etc/debian_version')) {
75
            $distro = 'debian';
76
        } elseif (file_exists('/etc/fedora-release')) {
77
            $distro = 'fedora';
78
        } elseif (file_exists('/etc/os-release')) {
79
            $distro = shell_exec('. /etc/os-release && echo "$ID"');
80
        } else {
81
            throw new \Exception('Unknown distribution');
82
        }
83
84
        return strtolower(trim($distro));
85
    }
86
}
87