FreeSpace   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 26
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A invokeTest() 0 26 5
A initialize() 0 5 1
A setPath() 0 3 1
A getPath() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\monitor\TestCase\FileSystem;
6
7
use SimpleSAML\Module\monitor\State;
8
use SimpleSAML\Module\monitor\TestData;
9
use SimpleSAML\Module\monitor\TestResult;
10
11
use function disk_free_space;
12
use function disk_total_space;
13
use function round;
14
15
final class FreeSpace extends \SimpleSAML\Module\monitor\TestCaseFactory
16
{
17
    /** @var string */
18
    private string $path = '';
19
20
21
    /**
22
     * @var \SimpleSAML\Module\monitor\TestData $testData
23
     *
24
     * @return void
25
     */
26
    protected function initialize(TestData $testData): void
27
    {
28
        $this->setPath($testData->getInputItem('path'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('path') can also be of type null; however, parameter $path of SimpleSAML\Module\monito...em\FreeSpace::setPath() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
        $this->setPath(/** @scrutinizer ignore-type */ $testData->getInputItem('path'));
Loading history...
29
        $this->setCategory($testData->getInputItem('category'));
0 ignored issues
show
Bug introduced by
It seems like $testData->getInputItem('category') can also be of type null; however, parameter $category of SimpleSAML\Module\monito...eFactory::setCategory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        $this->setCategory(/** @scrutinizer ignore-type */ $testData->getInputItem('category'));
Loading history...
30
        parent::initialize($testData);
31
    }
32
33
34
    /**
35
     * @param string $path
36
     * @return void
37
     */
38
    private function setPath(string $path): void
39
    {
40
        $this->path = $path;
41
    }
42
43
44
    /**
45
     * @return string
46
     */
47
    private function getPath(): string
48
    {
49
        return $this->path;
50
    }
51
52
53
    /**
54
     * @return void
55
     */
56
    public function invokeTest(): void
57
    {
58
        $path = $this->getPath();
59
        $testResult = new TestResult($this->getCategory(), $path);
60
61
        $size = disk_total_space($path);
62
        $free = disk_free_space($path);
63
        if ($size !== false && $free !== false) {
64
            $free = round(100 - ((($size - $free) / $size) * 100));
65
66
            if ($free >= 15) {
67
                $testResult->setMessage($free . '% free space');
68
                $testResult->setState(State::OK);
69
            } elseif ($free < 5) {
70
                $testResult->setMessage('Critical: ' . $free . '% free space');
71
                $testResult->setState(State::ERROR);
72
            } else {
73
                $testResult->setMessage($free . '% free space');
74
                $testResult->setState(State::WARNING);
75
            }
76
            $testResult->addOutput($free, 'free_percentage');
77
        } else {
78
            $testResult->setMessage('Error collecting disk usage');
79
            $testResult->setState(State::FATAL);
80
        }
81
        $this->setTestResult($testResult);
82
    }
83
}
84