Completed
Push — master ( bc72c5...1b234b )
by richard
02:39
created

Response   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 252
c 0
b 0
f 0
wmc 8
lcom 2
cbo 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 7 1
A getStatusCode() 0 4 1
A withStatus() 0 8 1
A setReasonPhrase() 0 5 1
A getReasonPhrase() 0 4 1
A getMessages() 0 4 1
A getMessage() 0 4 1
1
<?php
2
3
namespace Almendra\Http\Psr\Messages;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * Represents a response
10
 *
11
 * @package Almendra\Http
12
 */
13
class Response extends Message implements ResponseInterface
14
{
15
    /**
16
    * HTTP Response codes.
17
    *
18
    */
19
    const HTTP_CONTINUE = 100;
20
    const HTTP_SWITCHING_PROTOCOLS = 101;
21
    const HTTP_PROCESSING = 102;            // RFC2518
22
    const HTTP_OK = 200;
23
    const HTTP_CREATED = 201;
24
    const HTTP_ACCEPTED = 202;
25
    const HTTP_NON_AUTHORITATIVE_INFORMATION = 203;
26
    const HTTP_NO_CONTENT = 204;
27
    const HTTP_RESET_CONTENT = 205;
28
    const HTTP_PARTIAL_CONTENT = 206;
29
    const HTTP_MULTI_STATUS = 207;          // RFC4918
30
    const HTTP_ALREADY_REPORTED = 208;      // RFC5842
31
    const HTTP_IM_USED = 226;               // RFC3229
32
    const HTTP_MULTIPLE_CHOICES = 300;
33
    const HTTP_MOVED_PERMANENTLY = 301;
34
    const HTTP_FOUND = 302;
35
    const HTTP_SEE_OTHER = 303;
36
    const HTTP_NOT_MODIFIED = 304;
37
    const HTTP_USE_PROXY = 305;
38
    const HTTP_RESERVED = 306;
39
    const HTTP_TEMPORARY_REDIRECT = 307;
40
    const HTTP_PERMANENTLY_REDIRECT = 308;  // RFC7238
41
    const HTTP_BAD_REQUEST = 400;
42
    const HTTP_UNAUTHORIZED = 401;
43
    const HTTP_PAYMENT_REQUIRED = 402;
44
    const HTTP_FORBIDDEN = 403;
45
    const HTTP_NOT_FOUND = 404;
46
    const HTTP_METHOD_NOT_ALLOWED = 405;
47
    const HTTP_NOT_ACCEPTABLE = 406;
48
    const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
49
    const HTTP_REQUEST_TIMEOUT = 408;
50
    const HTTP_CONFLICT = 409;
51
    const HTTP_GONE = 410;
52
    const HTTP_LENGTH_REQUIRED = 411;
53
    const HTTP_PRECONDITION_FAILED = 412;
54
    const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
55
    const HTTP_REQUEST_URI_TOO_LONG = 414;
56
    const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
57
    const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
58
    const HTTP_EXPECTATION_FAILED = 417;
59
    const HTTP_I_AM_A_TEAPOT = 418;                                               // RFC2324
60
    const HTTP_MISDIRECTED_REQUEST = 421;                                         // RFC7540
61
    const HTTP_UNPROCESSABLE_ENTITY = 422;                                        // RFC4918
62
    const HTTP_LOCKED = 423;                                                      // RFC4918
63
    const HTTP_FAILED_DEPENDENCY = 424;                                           // RFC4918
64
    const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425;   // RFC2817
65
    const HTTP_UPGRADE_REQUIRED = 426;                                            // RFC2817
66
    const HTTP_PRECONDITION_REQUIRED = 428;                                       // RFC6585
67
    const HTTP_TOO_MANY_REQUESTS = 429;                                           // RFC6585
68
    const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;                             // RFC6585
69
    const HTTP_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
70
    const HTTP_INTERNAL_SERVER_ERROR = 500;
71
    const HTTP_NOT_IMPLEMENTED = 501;
72
    const HTTP_BAD_GATEWAY = 502;
73
    const HTTP_SERVICE_UNAVAILABLE = 503;
74
    const HTTP_GATEWAY_TIMEOUT = 504;
75
    const HTTP_VERSION_NOT_SUPPORTED = 505;
76
    const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506;                        // RFC2295
77
    const HTTP_INSUFFICIENT_STORAGE = 507;                                        // RFC4918
78
    const HTTP_LOOP_DETECTED = 508;                                               // RFC5842
79
    const HTTP_NOT_EXTENDED = 510;                                                // RFC2774
80
    const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511;                             // RFC6585
81
82
83
    /**
84
     * Status codes and reason phrases
85
     *
86
     * @var array
87
     */
88
    protected static $_messages = [
89
90
        /* Information */
91
        100 => 'Continue',
92
        101 => 'Switching Protocols',
93
        102 => 'Processing',
94
95
96
        /* Success */
97
        200 => 'OK',
98
        201 => 'Created',
99
        202 => 'Accepted',
100
        203 => 'Non-Authoritative Information',
101
        204 => 'No Content',
102
        205 => 'Reset Content',
103
        206 => 'Partial Content',
104
        207 => 'Multi-Status',
105
        208 => 'Already Reported',
106
        226 => 'IM Used',
107
108
109
        /* Redirection */
110
        300 => 'Multiple Choices',
111
        301 => 'Moved Permanently',
112
        302 => 'Found',
113
        303 => 'See Other',
114
        304 => 'Not Modified',
115
        305 => 'Use Proxy',
116
        306 => '(Unused)',
117
        307 => 'Temporary Redirect',
118
        308 => 'Permanent Redirect',
119
120
121
        /* Client Error */
122
        400 => 'Bad Request',
123
        401 => 'Unauthorized',
124
        402 => 'Payment Required',
125
        403 => 'Forbidden',
126
        404 => 'Not Found',
127
        405 => 'Method Not Allowed',
128
        406 => 'Not Acceptable',
129
        407 => 'Proxy Authentication Required',
130
        408 => 'Request Timeout',
131
        409 => 'Conflict',
132
        410 => 'Gone',
133
        411 => 'Length Required',
134
        412 => 'Precondition Failed',
135
        413 => 'Request Entity Too Large',
136
        414 => 'Request-URI Too Long',
137
        415 => 'Unsupported Media Type',
138
        416 => 'Requested Range Not Satisfiable',
139
        417 => 'Expectation Failed',
140
        418 => 'I\'m a teapot',
141
        422 => 'Unprocessable Entity',
142
        423 => 'Locked',
143
        424 => 'Failed Dependency',
144
        426 => 'Upgrade Required',
145
        428 => 'Precondition Required',
146
        429 => 'Too Many Requests',
147
        431 => 'Request Header Fields Too Large',
148
        451 => 'Unavailable For Legal Reasons',
149
150
151
        /* Server Error */
152
        500 => 'Internal Server Error',
153
        501 => 'Not Implemented',
154
        502 => 'Bad Gateway',
155
        503 => 'Service Unavailable',
156
        504 => 'Gateway Timeout',
157
        505 => 'HTTP Version Not Supported',
158
        506 => 'Variant Also Negotiates',
159
        507 => 'Insufficient Storage',
160
        508 => 'Loop Detected',
161
        510 => 'Not Extended',
162
        511 => 'Network Authentication Required',
163
    ];
164
165
    /**
166
     * @var int Status code
167
     */
168
    protected $_code = self::HTTP_OK;
169
170
    /**
171
     * @var string Reason phrase
172
     */
173
    protected $_reasonPhrase;
174
175
176
    public function __construct(StreamInterface $body = null)
177
    {
178
        $this -> _body = $body;
179
    }
180
181
    public function __toString()
182
    {
183
        // transform the stream to a string
184
        $body = $this -> getBody() -> __toString();
185
186
        return $body;
187
    }
188
189
    /**
190
     * Gets the response status code.
191
     *
192
     * The status code is a 3-digit integer result code of the server's attempt
193
     * to understand and satisfy the request.
194
     *
195
     * @return int Status code.
196
     */
197
    public function getStatusCode()
198
    {
199
        return $this -> _code;
200
    }
201
202
    /**
203
     * Return an instance with the specified status code and, optionally, reason phrase.
204
     *
205
     * If no reason phrase is specified, implementations MAY choose to default
206
     * to the RFC 7231 or IANA recommended reason phrase for the response's
207
     * status code.
208
     *
209
     * This method MUST be implemented in such a way as to retain the
210
     * immutability of the message, and MUST return an instance that has the
211
     * updated status and reason phrase.
212
     *
213
     * @link http://tools.ietf.org/html/rfc7231#section-6
214
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
215
     * @param int $code The 3-digit integer result code to set.
216
     * @param string $reasonPhrase The reason phrase to use with the
217
     *     provided status code; if none is provided, implementations MAY
218
     *     use the defaults as suggested in the HTTP specification.
219
     * @return static
220
     * @throws \InvalidArgumentException For invalid status code arguments.
221
     */
222
    public function withStatus($code, $reasonPhrase = '')
223
    {
224
        $clone = clone $this;
225
        $clone -> _code = $code;
226
        $clone -> setReasonPhrase($reasonPhrase);
227
228
        return $clone;
229
    }
230
231
    protected function setReasonPhrase($reasonPhrase) {
232
        $this -> _reasonPhrase = $reasonPhrase;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Gets the response reason phrase associated with the status code.
239
     *
240
     * Because a reason phrase is not a required element in a response
241
     * status line, the reason phrase value MAY be null. Implementations MAY
242
     * choose to return the default RFC 7231 recommended reason phrase (or those
243
     * listed in the IANA HTTP Status Code Registry) for the response's
244
     * status code.
245
     *
246
     * @link http://tools.ietf.org/html/rfc7231#section-6
247
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
248
     * @return string Reason phrase; must return an empty string if none present.
249
     */
250
    public function getReasonPhrase()
251
    {
252
        return $this -> _reasonPhrase;
253
    }
254
255
    public static function getMessages()
256
    {
257
        return self::$_messages;
258
    }
259
260
    public static function getMessage($code)
261
    {
262
        return self::$_messages[$code];
263
    }
264
}
265