Completed
Pull Request — master (#1)
by
unknown
12:13
created

CompositeHealthIndicator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 62
ccs 0
cts 18
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A addHealthIndicator() 0 4 1
A health() 0 9 2
A jsonSerialize() 0 4 1
1
<?php
2
3
namespace Actuator\Health\Indicator;
4
5
use Actuator\Health\Health;
6
use Actuator\Health\HealthAggregatorInterface;
7
use JsonSerializable;
8
9
/**
10
 * HealthIndicator that returns health indications from all registered delegates.
11
 *
12
 * @package Actuator\Health\Indicator
13
 */
14
class CompositeHealthIndicator implements HealthIndicatorInterface, JsonSerializable
15
{
16
    /**
17
     * @var HealthIndicatorInterface[]
18
     */
19
    private $indicators;
20
21
    /**
22
     * @var HealthAggregatorInterface
23
     */
24
    private $healthAggregator;
25
26
    /**
27
     * Create a new CompositeHealthIndicator from the specified indicators.
28
     *
29
     * @param HealthAggregatorInterface $healthAggregator
30
     * @param HealthIndicatorInterface[] $indicators
31
     */
32
    public function __construct(HealthAggregatorInterface $healthAggregator, array $indicators = array())
33
    {
34
        assert(!is_null($healthAggregator), 'HealthAggregator must not be null');
35
36
        $this->indicators = $indicators;
37
        $this->healthAggregator = $healthAggregator;
38
    }
39
40
    /**
41
     * @param string $name
42
     * @param HealthIndicatorInterface $indicator
43
     */
44
    public function addHealthIndicator($name, HealthIndicatorInterface $indicator)
45
    {
46
        $this->indicators[$name] = $indicator;
47
    }
48
49
    /**
50
     * Return an indication of health.
51
     *
52
     * @return Health
53
     */
54
    public function health()
55
    {
56
        $healths = array();
57
        foreach ($this->indicators as $key => $indicator) {
58
            $healths[$key] = $indicator->health();
59
        }
60
61
        return $this->healthAggregator->aggregate($healths);
62
    }
63
64
    /**
65
     * Specify data which should be serialized to JSON
66
     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
67
     * @return mixed data which can be serialized by <b>json_encode</b>,
68
     * which is a value of any type other than a resource.
69
     * @since 5.4.0
70
     */
71
    public function jsonSerialize()
72
    {
73
        return $this->health();
74
    }
75
}
76