DiskCollector::collect()   A
last analyzed

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\BasicCollector;
6
use Startwind\Inventorio\Exec\System;
7
8
/**
9
 * This collector returns details about the operating system.
10
 *
11
 * - family: the OS family (MacOs, Linux, Windows). Please use the provided constants.
12
 * - version: the OS version
13
 *
14
 */
15
class DiskCollector extends BasicCollector
16
{
17
    protected string $identifier = 'SystemHardDisk';
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function collect(): array
23
    {
24
        return [
25
            '/' => $this->getDiskFreeInfo('/')
26
        ];
27
    }
28
29
    private function getDiskFreeInfo(string $path = '/'): array
30
    {
31
        $bytesFree = System::getInstance()->getDiskFreeSpace($path);
32
        $bytesTotal = System::getInstance()->getDiskTotalSpace($path);
33
34
        if ($bytesFree === false || $bytesTotal === false || $bytesTotal == 0) {
0 ignored issues
show
introduced by
The condition $bytesTotal === false is always false.
Loading history...
35
            return [];
36
        }
37
38
        $gbFree = round($bytesFree / 1024 / 1024 / 1024, 2);
39
        $percentFree = ($bytesFree / $bytesTotal) * 100;
40
        $percentFreeRounded = floor($percentFree); // immer abrunden
41
42
        if ($percentFreeRounded < 20) {
43
            $score = 100;
44
        } elseif ($percentFreeRounded < 40) {
45
            $score = 75;
46
        } elseif ($percentFreeRounded < 60) {
47
            $score = 50;
48
        } elseif ($percentFreeRounded < 80) {
49
            $score = 25;
50
        } else {
51
            $score = 0;
52
        }
53
54
        return [
55
            'gb_free' => $gbFree,
56
            'percent_free' => $percentFreeRounded,
57
            'score' => $score
58
        ];
59
    }
60
}
61