DiskSpaceHealthIndicator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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