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