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

CompositeHealthIndicator::addHealthIndicator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Actuator\Health\Indicator;
4
5
use Actuator\Health\Health;
6
use Actuator\Health\HealthAggregatorInterface;
7
8
/**
9
 * HealthIndicator that returns health indications from all registered delegates.
10
 *
11
 * @package Actuator\Health\Indicator
12
 */
13
class CompositeHealthIndicator implements HealthIndicatorInterface
14
{
15
    /**
16
     * @var HealthIndicatorInterface[]
17
     */
18
    private $indicators;
19
20
    /**
21
     * @var HealthAggregatorInterface
22
     */
23
    private $healthAggregator;
24
25
    /**
26
     * Create a new CompositeHealthIndicator from the specified indicators.
27
     *
28
     * @param HealthAggregatorInterface $healthAggregator
29
     * @param HealthIndicatorInterface[] $indicators
30
     */
31 15
    public function __construct(HealthAggregatorInterface $healthAggregator, array $indicators = array())
32
    {
33 15
        assert(!is_null($healthAggregator), 'HealthAggregator must not be null');
34
35 15
        $this->indicators = $indicators;
36 15
        $this->healthAggregator = $healthAggregator;
37 15
    }
38
39
    /**
40
     * @param string $name
41
     * @param HealthIndicatorInterface $indicator
42
     */
43 12
    public function addHealthIndicator($name, HealthIndicatorInterface $indicator)
44
    {
45 12
        $this->indicators[$name] = $indicator;
46 12
    }
47
48
    /**
49
     * Return an indication of health.
50
     *
51
     * @return Health
52
     */
53 15
    public function health()
54
    {
55 15
        $healths = array();
56 15
        foreach ($this->indicators as $key => $indicator) {
57 15
            $healths[$key] = $indicator->health();
58 15
        }
59
60 15
        return $this->healthAggregator->aggregate($healths);
61
    }
62
}
63