HealthBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

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

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A down() 0 10 2
A unknown() 0 4 1
A up() 0 4 1
A outOfService() 0 4 1
A status() 0 10 2
A withException() 0 6 1
A withDetail() 0 8 1
A build() 0 4 1
1
<?php
2
3
namespace Actuator\Health;
4
5
/**
6
 * Builder for creating imutable Health instances.
7
 */
8
class HealthBuilder
9
{
10
    /**
11
     * @var Status
12
     */
13
    public $status;
14
15
    /**
16
     * @var array
17
     */
18
    public $details;
19
20
    /**
21
     * Create new Builder instance.
22
     *
23
     * @param Status $status
24
     * @param array  $details
25
     */
26
    public function __construct(Status $status = null, $details = [])
27
    {
28 108
        if (is_null($status)) {
29
            $status = new Status(Status::UNKNOWN);
30 108
        }
31 93
32 93
        $this->status = $status;
33
        $this->details = $details;
34 108
    }
35 108
36 108
    /**
37
     * Set status to Status::DOWN and add details for given Exception if available.
38
     *
39
     * @param \Exception $exception The exception
40
     *
41
     * @return HealthBuilder
42
     */
43
    public function down(\Exception $exception = null)
44 30
    {
45
        $builder = $this->status(new Status(Status::DOWN));
46 30
47
        if (!is_null($exception)) {
48 30
            $builder->withException($exception);
49 15
        }
50 15
51
        return $builder;
52 30
    }
53
54
    /**
55
     * Create a new Builder instance with a Status::UNKNOWN status.
56
     *
57
     * @return HealthBuilder
58
     */
59
    public function unknown()
60 21
    {
61
        return $this->status(new Status(Status::UNKNOWN));
62 21
    }
63
64
    /**
65
     * Create a new Builder instance with a Status::UP status.
66
     *
67
     * @return HealthBuilder
68
     */
69
    public function up()
70 18
    {
71
        return $this->status(new Status(Status::UP));
72 18
    }
73
74
    /**
75
     * Create a new Builder instance with a Status::OUT_OF_SERVICE status.
76
     *
77
     * @return HealthBuilder
78
     */
79
    public function outOfService()
80 3
    {
81
        return $this->status(Status::OUT_OF_SERVICE);
82 3
    }
83
84
    /**
85
     * Set status to given Status instance or status code.
86
     *
87
     * @param Status|string $status
88
     *
89
     * @return $this
90
     */
91 90
    public function status($status)
92
    {
93 90
        if (!($status instanceof Status)) {
94 6
            $status = new Status($status);
95 6
        }
96
97 90
        $this->status = $status;
98 90
99
        return $this;
100
    }
101
102
    /**
103
     * Record detail for given {@link Exception}.
104
     *
105
     * @param \Exception $exception
106
     *
107 18
     * @return $this
108
     */
109 18
    public function withException(\Exception $exception)
110 18
    {
111
        assert(!is_null($exception), 'Exception must not be null');
112
113
        return $this->withDetail('error', $exception->getMessage());
114
    }
115
116
    /**
117
     * Record detail using key and message.
118
     *
119
     * @param string $key     the detail key
120 57
     * @param mixed  $message the detail message
121
     *
122 57
     * @return $this
123 57
     */
124 57
    public function withDetail($key, $message)
125 57
    {
126
        assert(!is_null($key), 'Key must not be null');
127
        assert(!is_null($message), 'Message must not be null');
128
        $this->details[strval($key)] = $message;
129
130
        return $this;
131 105
    }
132
133 105
    /**
134
     * @return Health
135
     */
136
    public function build()
137
    {
138
        return new Health($this);
139
    }
140
}
141