Passed
Push — master ( 6fc0aa...bcd893 )
by Nils
02:28
created

ConfigurationCollector::getDiskSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Collector\System\General;
4
5
use Startwind\Inventorio\Collector\Collector;
6
7
class ConfigurationCollector implements Collector
8
{
9
    protected const COLLECTION_IDENTIFIER = 'SystemConfigurationCollector';
10
11
    /**
12
     * @inheritDoc
13
     */
14
    public function getIdentifier(): string
15
    {
16
        return self::COLLECTION_IDENTIFIER;
17
    }
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function collect(): array
23
    {
24
        return [
25
            'cpu' => $this->getCpuCount(),
26
            'memory' => $this->getMemorySize(),
27
            'disk' => $this->getDiskSize()
28
        ];
29
    }
30
31
    private function getCpuCount(): int
32
    {
33
        $os = PHP_OS_FAMILY;
34
        if ($os === 'Windows') {
0 ignored issues
show
introduced by
The condition $os === 'Windows' is always false.
Loading history...
35
            return (int)getenv("NUMBER_OF_PROCESSORS");
36
        } elseif ($os === 'Darwin' || $os === 'Linux') {
0 ignored issues
show
introduced by
The condition $os === 'Linux' is always true.
Loading history...
37
            return (int)shell_exec("getconf _NPROCESSORS_ONLN");
38
        }
39
        return 0;
40
    }
41
42
    private function getMemorySize(): string
43
    {
44
        $os = PHP_OS_FAMILY;
45
        if ($os === 'Windows') {
0 ignored issues
show
introduced by
The condition $os === 'Windows' is always false.
Loading history...
46
            $output = shell_exec("wmic computersystem get TotalPhysicalMemory");
47
            preg_match("/\d+/", $output, $matches);
48
            return isset($matches[0]) ? round($matches[0] / 1024 / 1024, 2) . " MB" : "Nicht verfügbar";
49
        } elseif ($os === 'Darwin') {
0 ignored issues
show
introduced by
The condition $os === 'Darwin' is always false.
Loading history...
50
            $memBytes = trim(shell_exec("sysctl -n hw.memsize"));
51
            return round($memBytes / 1024 / 1024, 2) . " MB";
52
        } elseif ($os === 'Linux') {
0 ignored issues
show
introduced by
The condition $os === 'Linux' is always true.
Loading history...
53
            $meminfo = file_get_contents("/proc/meminfo");
54
            preg_match("/MemTotal:\s+(\d+)\skB/", $meminfo, $matches);
55
            return isset($matches[1]) ? round($matches[1] / 1024, 2) . " MB" : "Nicht verfügbar";
56
        }
57
        return 0;
58
    }
59
60
    function getDiskSize(): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
    {
62
        $bytes = disk_total_space("/");
63
        return round($bytes / (1024 ** 3), 2) . " GB";
64
    }
65
66
}
67