Completed
Push — master ( ee6112...f2efac )
by John
15:02
created

HealthBuilder::outOfService()   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 1
Metric Value
c 1
b 0
f 1
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;
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 18
    public function __construct(Status $status = null, $details = array())
29
    {
30 18
        if (is_null($status)) {
31 18
            $status = new Status(Status::UNKNOWN);
32 18
        }
33
34 18
        $this->status = $status;
35 18
        $this->details = $details;
36 18
    }
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 3
    public function down(\Exception $exception = null)
45
    {
46 3
        $builder = $this->status(new Status(Status::DOWN));
47
48 3
        if (!is_null($exception)) {
49
            $builder->withException($exception);
50
        }
51
52 3
        return $builder;
53
    }
54
55
    /**
56
     * Create a new Builder instance with a Status::UNKNOWN status.
57
     *
58
     * @return HealthBuilder
59
     */
60 3
    public function unknown()
61
    {
62 3
        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
    public function up()
71 18
    {
72
        return $this->status(new Status(Status::UP));
73 18
    }
74
75
    /**
76
     * Create a new Builder instance with a Status::OUT_OF_SERVICE status.
77 18
     *
78 18
     * @return HealthBuilder
79
     */
80
    public function outOfService()
81
    {
82
        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
    public function status($status)
92
    {
93
        if (!($status instanceof Status)) {
94
            $status = new Status($status);
95
        }
96
97
        $this->status = $status;
98
        return $this;
99
    }
100 6
101
    /**
102 6
     * Record detail for given {@link Exception}.
103 6
     *
104 6
     * @param \Exception $exception
105 6
     * @return $this
106
     */
107
    public function withException(\Exception $exception)
108
    {
109
        assert(!is_null($exception), 'Exception must not be null');
110
        return $this->withDetail('error', $exception->getMessage());
111 18
    }
112
113 18
    /**
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
    public function withDetail($key, $message)
121
    {
122
        assert(!is_null($key), 'Key must not be null');
123
        assert(!is_null($message), 'Message must not be null');
124
        $this->details[strval($key)] = $message;
125
        return $this;
126
    }
127
128
    /**
129
     * @return Health
130
     */
131
    public function build()
132
    {
133
        return new Health($this);
134
    }
135
}
136