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

StatusLine::code()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Interface 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
 * Interface 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 integer
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 code()
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 5 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
71
    {
72
        return $this->code;
73
    }
74
75
    /**
76
     * Return the reason phrase
77
     *
78
     * @return string
79
     */
80
    final public function reason()
81
    {
82
        return (string) $this->reason;
83
    }
84
85
    /**
86
     * Add the status code and reason phrase to a Response.
87
     *
88
     * @param Psr\Http\Message\ResponseInterface $response The response
0 ignored issues
show
Documentation introduced by
Should the type for parameter $response not be ResponseInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
89
     * @return Psr\Http\Message\ResponseInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be ResponseInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
90
     */
91
    public function response(ResponseInterface $response)
92
    {
93
        return $response->withStatus($this->code(), $this->reason());
94
    }
95
96
    /**
97
     * Return the status class of the response code.
98
     *
99
     * @return int
100
     */
101
    final public function responseClass()
102
    {
103
        return (int) floor($this->code / 100);
104
    }
105
106
    /**
107
     * Helper to establish if the class of the status code
108
     * is informational (1xx).
109
     *
110
     * @return bool
111
     */
112
    final public function isInformational()
113
    {
114
        return $this->is(StatusCode::INFORMATIONAL);
115
    }
116
117
    /**
118
     * Helper to establish if the class of the status code
119
     * is successful (2xx).
120
     *
121
     * @return bool
122
     */
123
    final public function isSuccessful()
124
    {
125
        return $this->is(StatusCode::SUCCESSFUL);
126
    }
127
128
    /**
129
     * Helper to establish if the class of the status code
130
     * is redirection (3xx).
131
     *
132
     * @return bool
133
     */
134
    final public function isRedirection()
135
    {
136
        return $this->is(StatusCode::REDIRECTION);
137
    }
138
139
    /**
140
     * Helper to establish if the class of the status code
141
     * is client error (4xx).
142
     *
143
     * @return bool
144
     */
145
    final public function isClientError()
146
    {
147
        return $this->is(StatusCode::CLIENT_ERROR);
148
    }
149
150
    /**
151
     * Helper to establish if the class of the status code
152
     * is server error (5xx).
153
     *
154
     * @return bool
155
     */
156
    final public function isServerError()
157
    {
158
        return $this->is(StatusCode::SERVER_ERROR);
159
    }
160
161
    /**
162
     * Set the code. Used in constructor to ensure the code meets the
163
     * requirements for a status code.
164
     *
165
     */
166
    private function setCode($code)
167
    {
168
        if (!is_numeric($code)) {
169
            throw new InvalidStatusCodeException(
170
                "Status code must be numeric, but received $code"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $code instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
171
            );
172
        }
173
        $code = (int) $code;
174
175
        if ($code < 100) {
176
            throw new InvalidStatusCodeException(
177
                "Status code must be 100 or greater but code was $code"
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $code instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
178
            );
179
        }
180
        $this->code = $code;
181
    }
182
183
    /**
184
     * Test whether the response class matches the class passed to it.
185
     *
186
     * @return bool
187
     */
188
    private function is($class)
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 5 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
189
    {
190
        return ($this->responseClass() === $class);
191
    }
192
}
193