SpaceUsedCheck::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 19
rs 9.8666
1
<?php
2
3
namespace Leankoala\HealthFoundation\Check\Device;
4
5
use Leankoala\HealthFoundation\Check\Check;
6
use Leankoala\HealthFoundation\Check\MetricAwareResult;
7
use Leankoala\HealthFoundation\Check\Result;
8
9
class SpaceUsedCheck implements Check
10
{
11
    const IDENTIFIER = 'base:device:spaceUsed';
12
13
    private $maxUsageInPercent = 95;
14
15
    private $directory = '/';
16
17
    public function init($maxUsageInPercent, $directory = '/')
18
    {
19
        $this->maxUsageInPercent = $maxUsageInPercent;
20
        $this->directory = $directory;
21
    }
22
23
    /**
24
     * Checks if the space left on device is sufficient
25
     *
26
     * @return Result
27
     */
28
    public function run()
29
    {
30
        $free = disk_free_space($this->directory);
31
        $total = disk_total_space($this->directory);
32
33
        $usage = 100 - round(($free / $total) * 100);
34
35
        if ($usage > $this->maxUsageInPercent) {
36
            $result = new MetricAwareResult(Result::STATUS_FAIL, 'No space left on device. ' . $usage . '% used (' . $this->directory . ').');
37
        } else {
38
            $result = new MetricAwareResult(Result::STATUS_PASS, 'Enough space left on device. ' . $usage . '% used (' . $this->directory . ').');
39
        }
40
41
        $result->setMetric($usage / 100, 'percent', MetricAwareResult::METRIC_TYPE_PERCENT);
42
        $result->setLimit($this->maxUsageInPercent / 100);
43
        $result->setLimitType(MetricAwareResult::LIMIT_TYPE_MAX);
44
        $result->setObservedValuePrecision(2);
45
46
        return $result;
47
    }
48
49
    public function getIdentifier()
50
    {
51
        return self::IDENTIFIER;
52
    }
53
}
54