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

ConfigurationCollector   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDiskSize() 0 4 1
A getCpuCount() 0 9 4
A collect() 0 6 1
A getMemorySize() 0 16 6
A getIdentifier() 0 3 1
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