Response::getReasonPhrase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Zapheus\Bridge\Psr;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\StreamInterface;
7
8
/**
9
 * Response
10
 *
11
 * @package Zapheus
12
 * @author  Rougin Gutib <[email protected]>
13
 */
14
class Response extends Message implements ResponseInterface
15
{
16
    /**
17
     * @var integer
18
     */
19
    protected $code = 200;
20
21
    /**
22
     * @var array
23
     */
24
    protected $codes = array(
25
        100 => 'Continue',
26
        101 => 'Switching Protocols',
27
        102 => 'Processing',
28
        200 => 'OK',
29
        201 => 'Created',
30
        202 => 'Accepted',
31
        203 => 'Non Authoritative Information',
32
        204 => 'No Content',
33
        205 => 'Reset Content',
34
        206 => 'Partial Content',
35
        207 => 'Multi Status',
36
        208 => 'Already Reported',
37
        226 => 'Im Used',
38
        300 => 'Multiple Choices',
39
        301 => 'Moved Permanently',
40
        302 => 'Found',
41
        303 => 'See Other',
42
        304 => 'Not Modified',
43
        305 => 'Use Proxy',
44
        306 => 'Reserved',
45
        307 => 'Temporary Redirect',
46
        308 => 'Permanent Redirect',
47
        400 => 'Bad Request',
48
        401 => 'Unauthorized',
49
        402 => 'Payment Required',
50
        403 => 'Forbidden',
51
        404 => 'Not Found',
52
        405 => 'Method Not Allowed',
53
        406 => 'Not Acceptable',
54
        407 => 'Proxy Authentication Required',
55
        408 => 'Request Timeout',
56
        409 => 'Conflict',
57
        410 => 'Gone',
58
        411 => 'Length Required',
59
        412 => 'Precondition Failed',
60
        413 => 'Payload Too Large',
61
        414 => 'Uri Too Long',
62
        415 => 'Unsupported Media Type',
63
        416 => 'Range Not Satisfiable',
64
        417 => 'Expectation Failed',
65
        418 => 'Im A Teapot',
66
        421 => 'Misdirected Request',
67
        422 => 'Unprocessable Entity',
68
        423 => 'Locked',
69
        424 => 'Failed Dependency',
70
        426 => 'Upgrade Required',
71
        428 => 'Precondition Required',
72
        429 => 'Too Many Requests',
73
        431 => 'Request Header Fields Too Large',
74
        451 => 'Unavailable For Legal Reasons',
75
        500 => 'Internal Server Error',
76
        501 => 'Not Implemented',
77
        502 => 'Bad Gateway',
78
        503 => 'Service Unavailable',
79
        504 => 'Gateway Timeout',
80
        505 => 'Version Not Supported',
81
        506 => 'Variant Also Negotiates',
82
        507 => 'Insufficient Storage',
83
        508 => 'Loop Detected',
84
        510 => 'Not Extended',
85
        511 => 'Network Authentication Required',
86
    );
87
88
    /**
89
     * @var string
90
     */
91
    protected $reason = 'OK';
92
93
    /**
94
     * Initializes the response instance.
95
     *
96
     * @param integer                                $code
97
     * @param \Psr\Http\Message\StreamInterface|null $body
98
     * @param string                                 $version
99
     * @param array                                  $headers
100
     */
101 12
    public function __construct($code = 200, StreamInterface $body = null, array $headers = array(), $version = '1.1')
102
    {
103 12
        parent::__construct($body, $headers, $version);
104
105 12
        $this->code = $code;
106
107 12
        $this->reason = $this->codes[$code];
108 12
    }
109
110
    /**
111
     * Returns the response reason phrase associated with the status code.
112
     *
113
     * @return string
114
     */
115 3
    public function getReasonPhrase()
116
    {
117 3
        return $this->reason;
118
    }
119
120
    /**
121
     * Returns the response status code.
122
     *
123
     * @return integer
124
     */
125 9
    public function getStatusCode()
126
    {
127 9
        return $this->code;
128
    }
129
130
    /**
131
     * Returns an instance with the specified status code and, optionally, reason phrase.
132
     *
133
     * @param  integer $code
134
     * @param  string  $reason
135
     * @return static
136
     *
137
     * @throws \InvalidArgumentException
138
     */
139 6
    public function withStatus($code, $reason = '')
140
    {
141
        // TODO: Add \InvalidArgumentException
142
143 6
        $static = clone $this;
144
145 6
        $static->code = $code;
146
147 6
        $static->reason = $reason ?: $static->codes[$code];
148
149 6
        return $static;
150
    }
151
}
152