Passed
Push — master ( 9792fc...04e5e0 )
by Tim
06:59
created

FreeSpace   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

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

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
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
        $size = disk_total_space($path);
56
        $free = disk_free_space($path);
57
        if ($size !== false && $free !== false) {
58
            $free = round(100 - ((($size - $free) / $size) * 100));
59
60
            if ($free >= 15) {
61
                $testResult->setMessage($free . '% free space');
62
                $testResult->setState(State::OK);
63
            } elseif ($free < 5) {
64
                $testResult->setMessage('Critical: ' . $free . '% free space');
65
                $testResult->setState(State::ERROR);
66
            } else {
67
                $testResult->setMessage($free . '% free space');
68
                $testResult->setState(State::WARNING);
69
            }
70
            $testResult->addOutput($free, 'free_percentage');
71
        } else {
72
            $testResult->setMessage('Error collecting disk usage');
73
            $testResult->setState(State::FATAL);
74
        }
75
        $this->setTestResult($testResult);
76
    }
77
}
78