Completed
Branch master (f2efac)
by John
02:31
created

DiskSpaceHealthIndicator::doHealthCheck()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 10
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Actuator\Health\Indicator;
4
5
use Actuator\Health\HealthBuilder;
6
7
/**
8
 * A HealthIndicator that checks available disk space and reports a status of
9
 * Status::DOWN when it drops below a configurable threshold.
10
 *
11
 * @package Actuator\Health\Indicator
12
 */
13
class DiskSpaceHealthIndicator extends AbstractHealthIndicator
14
{
15
    /**
16
     * @var DiskSpaceHealthIndicatorProperties
17
     */
18
    private $properties;
19
20
    /**
21
     * Create a new DiskSpaceHealthIndicator.
22
     *
23
     * @param DiskSpaceHealthIndicatorProperties $properties
24
     */
25 9
    public function __construct(DiskSpaceHealthIndicatorProperties $properties = null)
26
    {
27 9
        if (is_null($properties)) {
28 3
            $properties = new DiskSpaceHealthIndicatorProperties();
29 3
        }
30
31 9
        $this->properties = $properties;
32 9
    }
33
34
    /**
35
     * Actual health check logic.
36
     *
37
     * @param HealthBuilder $builder
38
     * @throws \Exception any Exception that should create a Status::DOWN
39
     * system status.
40
     */
41 9
    protected function doHealthCheck(HealthBuilder $builder)
42
    {
43 9
        $directoryPath = $this->properties->getDirectory();
44 9
        $diskFreeInBytes = disk_free_space($directoryPath);
45 9
        if ($diskFreeInBytes >= $this->properties->getThreshold()) {
46 3
            $builder->up();
47 3
        } else {
48 6
            $builder->down();
49
        }
50 9
        $builder->withDetail('total', disk_total_space($directoryPath));
51 9
        $builder->withDetail('free', $diskFreeInBytes);
52 9
        $builder->withDetail('threshold', $this->properties->getThreshold());
53 9
    }
54
}
55