Passed
Push — monitor-3.0.x ( f85cdc...51d4ea )
by Tim
03:29
created

FreeSpace::invokeTest()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 26
rs 9.2888
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
use Webmozart\Assert\Assert;
9
10
final class FreeSpace extends \SimpleSAML\Module\monitor\TestCaseFactory
11
{
12
    /** @var string */
13
    private $path = '';
14
15
16
    /**
17
     * @var \SimpleSAML\Module\monitor\TestData $testData
18
     *
19
     * @return void
20
     */
21
    protected function initialize(TestData $testData): void
22
    {
23
        $path = $testData->getInputItem('path');
24
        $category = $testData->getInputItem('category');
25
26
        Assert::string($path);
27
        Assert::string($category);
28
29
        $this->setPath($path);
0 ignored issues
show
Bug introduced by
It seems like $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

29
        $this->setPath(/** @scrutinizer ignore-type */ $path);
Loading history...
30
        $this->setCategory($category);
0 ignored issues
show
Bug introduced by
It seems like $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

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