Completed
Pull Request — master (#45)
by Barney
02:20
created

StatusLine::isInformational()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Class representing a Value Object of the HTTP Status-Line, as
4
 * specified in RFC 2616 and RFC 7231.
5
 *
6
 * PHP version 5.3
7
 *
8
 * @category StatusLine
9
 *
10
 * @package Teapot
11
 *
12
 * @author    Barney Hanlon <[email protected]>
13
 * @copyright 2013-2016 B Hanlon. All rights reserved.
14
 * @license   MIT http://opensource.org/licenses/MIT
15
 * @link      https://shrikeh.github.com/teapot
16
 */
17
namespace Teapot;
18
19
use Teapot\StatusCodeException\InvalidStatusCodeException;
20
use Psr\Http\Message\ResponseInterface;
21
22
/**
23
 * Class representing a Value Object of the HTTP Status-Line, as
24
 * specified in RFC 2616 and RFC 7231.
25
 *
26
 * PHP version 5.3
27
 *
28
 * @category StatusLine
29
 *
30
 * @package Teapot
31
 *
32
 * @author    Barney Hanlon <[email protected]>
33
 * @copyright 2013-2016 B Hanlon. All rights reserved.
34
 * @license   MIT http://opensource.org/licenses/MIT
35
 * @link      https://shrikeh.github.com/teapot
36
 */
37
class StatusLine
38
{
39
    /**
40
     * The actual response code.
41
     *
42
     * @var int
43
     */
44
    private $code;
45
46
    /**
47
     * The reason phrase.
48
     *
49
     * @var string
50
     */
51
    private $reason;
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param int    $code   The response code
57
     * @param string $reason The reason phrase
58
     */
59
    final public function __construct($code, $reason)
60
    {
61
        $this->setCode($code);
62
        $this->reason = $reason;
63
    }
64
65
    /**
66
     * Return the response code.
67
     *
68
     * @return int
69
     */
70
    final public function statusCode()
71
    {
72
        return $this->code;
73
    }
74
75
    /**
76
     * Return the reason phrase
77
     *
78
     * @return string
79
     */
80
    final public function reasonPhrase()
81
    {
82
        return (string) $this->reason;
83
    }
84
85
    /**
86
     * Add the status code and reason phrase to a Response.
87
     *
88
     * @param ResponseInterface $response The response
89
     * @return ResponseInterface
90
     */
91
    public function response(ResponseInterface $response)
92
    {
93
        return $response->withStatus(
94
            $this->statusCode(),
95
            $this->reasonPhrase()
96
        );
97
    }
98
99
    /**
100
     * Return the status class of the response code.
101
     *
102
     * @return int
103
     */
104
    final public function responseClass()
105
    {
106
        return (int) floor($this->code / 100);
107
    }
108
109
    /**
110
     * Helper to establish if the class of the status code
111
     * is informational (1xx).
112
     *
113
     * @return bool
114
     */
115
    final public function isInformational()
116
    {
117
        return $this->isStatusClass(StatusCode::INFORMATIONAL);
118
    }
119
120
    /**
121
     * Helper to establish if the class of the status code
122
     * is successful (2xx).
123
     *
124
     * @return bool
125
     */
126
    final public function isSuccessful()
127
    {
128
        return $this->isStatusClass(StatusCode::SUCCESSFUL);
129
    }
130
131
    /**
132
     * Helper to establish if the class of the status code
133
     * is redirection (3xx).
134
     *
135
     * @return bool
136
     */
137
    final public function isRedirection()
138
    {
139
        return $this->isStatusClass(StatusCode::REDIRECTION);
140
    }
141
142
    /**
143
     * Helper to establish if the class of the status code
144
     * is client error (4xx).
145
     *
146
     * @return bool
147
     */
148
    final public function isClientError()
149
    {
150
        return $this->isStatusClass(StatusCode::CLIENT_ERROR);
151
    }
152
153
    /**
154
     * Helper to establish if the class of the status code
155
     * is server error (5xx).
156
     *
157
     * @return bool
158
     */
159
    final public function isServerError()
160
    {
161
        return $this->isStatusClass(StatusCode::SERVER_ERROR);
162
    }
163
164
    /**
165
     * Set the code. Used in constructor to ensure the code meets the
166
     * requirements for a status code.
167
     *
168
     * @param int $code The status code
169
     * @throws InvalidStatusCodeException If the status code is invalid
170
     */
171
    private function setCode($code)
172
    {
173
        if (!is_numeric($code)) {
174
            throw new InvalidStatusCodeException(sprintf(
175
                'Status code must be numeric, but received %d',
176
                $code
177
            ));
178
        }
179
        $code = (int) $code;
180
181
        if ($code < 100) {
182
            throw new InvalidStatusCodeException(sprintf(
183
                'Status code must be 100 or greater but code was %d',
184
                $code
185
            ));
186
        }
187
        $this->code = $code;
188
    }
189
190
    /**
191
     * Test whether the response class matches the class passed to it.
192
     *
193
     * @param int $class The class of the response code
194
     * @return bool
195
     */
196
    private function isStatusClass($class)
197
    {
198
        return ($this->responseClass() === $class);
199
    }
200
}
201