Passed
Branch master (4b23d6)
by Tim
04:40
created

FreeSpace::invokeTest()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
rs 9.2888
c 0
b 0
f 0
cc 5
nc 4
nop 0
1
<?php
2
3
namespace SimpleSAML\Module\Monitor\TestCase\FileSystem;
4
5
use SimpleSAML\Module\Monitor\State;
6
use SimpleSAML\Module\Monitor\TestData;
7
use SimpleSAML\Module\Monitor\TestResult;
8
9
final class FreeSpace extends \SimpleSAML\Module\Monitor\TestCaseFactory
10
{
11
    /** @var string */
12
    private $path = '';
13
14
15
    /**
16
     * @var \SimpleSAML\Module\Monitor\TestData $testData
17
     *
18
     * @return void
19
     */
20
    protected function initialize(TestData $testData): void
21
    {
22
        $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

22
        $this->setPath(/** @scrutinizer ignore-type */ $testData->getInputItem('path'));
Loading history...
23
        $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

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