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

CompositeHealthIndicator::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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