System   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 28
c 3
b 0
f 0
dl 0
loc 61
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getInstance() 0 7 2
A getLoadAverage() 0 13 2
A getDiskTotalSpace() 0 7 2
A getDiskFreeSpace() 0 6 2
A getPlatform() 0 9 2
A getEnvironmentVariable() 0 5 1
1
<?php
2
3
namespace Startwind\Inventorio\Exec;
4
5
class System
6
{
7
    private static ?System $instance = null;
8
9
    public static function getInstance(): System
10
    {
11
        if (self::$instance === null) {
12
            self::$instance = new self();
13
        }
14
15
        return self::$instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::instance could return the type null which is incompatible with the type-hinted return Startwind\Inventorio\Exec\System. Consider adding an additional type-check to rule them out.
Loading history...
16
    }
17
18
    public function getDiskTotalSpace($directory): float
19
    {
20
        $escaped = escapeshellarg($directory);
21
        $cmd = "df -k $escaped | awk 'NR==2 {print \$2 * 1024}'";
0 ignored issues
show
Unused Code introduced by
The assignment to $cmd is dead and can be removed.
Loading history...
22
        $cmd = 'df -k ' . $escaped . '| awk "NR==2 {print \$2 * 1024}"';
23
        $output = trim(Runner::getInstance()->run($cmd)->getOutput());
24
        return is_numeric($output) ? (float)$output : 0.0;
25
    }
26
27
    public function getDiskFreeSpace($directory): float
28
    {
29
        $escaped = escapeshellarg($directory);
30
        $cmd = 'df -k ' . $escaped . '| awk "NR==2 {print \$4 * 1024}"';
31
        $output = trim(Runner::getInstance()->run($cmd)->getOutput());
32
        return is_numeric($output) ? (float)$output : 0.0;
33
    }
34
35
    public function getLoadAverage(): array
36
    {
37
        $output = trim(Runner::getInstance()->run('uptime')->getOutput());
38
39
        if (preg_match('/load averages?: ([\d\.,]+)[, ]+([\d\.,]+)[, ]+([\d\.,]+)/', $output, $matches)) {
40
            return [
41
                (float)str_replace(',', '.', $matches[1]),
42
                (float)str_replace(',', '.', $matches[2]),
43
                (float)str_replace(',', '.', $matches[3]),
44
            ];
45
        }
46
47
        return [];
48
    }
49
50
    public function getEnvironmentVariable($name): string
51
    {
52
        $escaped = escapeshellarg($name);
53
        $cmd = "printenv $escaped";
54
        return trim(Runner::getInstance()->run($cmd)->getOutput());
55
    }
56
57
    public function getPlatform(): string
58
    {
59
        static $platform;
60
61
        if (!$platform) {
62
            $platform = trim(Runner::getInstance()->run('uname -s')->getOutput());
63
        }
64
65
        return $platform;
66
    }
67
}