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

DiskSpaceHealthIndicator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 42
ccs 17
cts 17
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A doHealthCheck() 0 13 2
A __construct() 0 8 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