Completed
Push — master ( 588b5e...a331b6 )
by Matthew
04:08
created

Response::getStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Fyuze\Http\Message;
3
4
use Psr\Http\Message\ResponseInterface;
5
6
class Response extends Message implements ResponseInterface
7
{
8
    /**
9
     * @var integer
10
     */
11
    protected $statusCode;
12
13
    /**
14
     * @var string
15
     */
16
    protected $reasonPhrase = '';
17
18
    /**
19
     * @var array
20
     */
21
    protected $messages = [
22
        // Informational
23
        100 => 'Continue',
24
        101 => 'Switching Protocols',
25
        102 => 'Processing',
26
        // Successful
27
        200 => 'OK',
28
        201 => 'Created',
29
        202 => 'Accepted',
30
        203 => 'Non-Authoritative Information',
31
        204 => 'No Content',
32
        205 => 'Reset Content',
33
        206 => 'Partial Content',
34
        207 => 'Multi-Status',
35
        208 => 'Already Reported',
36
        226 => 'IM Used',
37
        // Redirection
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 => '(Unused)',
45
        307 => 'Temporary Redirect',
46
        308 => 'Permanent Redirect',
47
        // Client Error
48
        400 => 'Bad Request',
49
        401 => 'Unauthorized',
50
        402 => 'Payment Required',
51
        403 => 'Forbidden',
52
        404 => 'Not Found',
53
        405 => 'Method Not Allowed',
54
        406 => 'Not Acceptable',
55
        407 => 'Proxy Authentication Required',
56
        408 => 'Request Timeout',
57
        409 => 'Conflict',
58
        410 => 'Gone',
59
        411 => 'Length Required',
60
        412 => 'Precondition Failed',
61
        413 => 'Request Entity Too Large',
62
        414 => 'Request-URI Too Long',
63
        415 => 'Unsupported Media Type',
64
        416 => 'Requested Range Not Satisfiable',
65
        417 => 'Expectation Failed',
66
        418 => 'I\'m a teapot',
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
        // Server Error
75
        500 => 'Internal Server Error',
76
        501 => 'Not Implemented',
77
        502 => 'Bad Gateway',
78
        503 => 'Service Unavailable',
79
        504 => 'Gateway Timeout',
80
        505 => 'HTTP 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
     * {@inheritdoc}
90
     *
91
     * @return int Status code.
92
     */
93 6
    public function getStatusCode()
94
    {
95 6
        return $this->statusCode;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     *
101
     * @link http://tools.ietf.org/html/rfc7231#section-6
102
     * @link http://www.iana.org/assignments/http-status-code s/http-status-codes.xhtml
103
     * @param int $code The 3-digit integer result code to set.
104
     * @param string $reasonPhrase
105
     * @return self
106
     * @throws \InvalidArgumentException For invalid status code arguments.
107
     */
108 16
    public function withStatus($code, $reasonPhrase = '')
109
    {
110 16
        $instance = clone $this;
111 16
        $code = (int)$code;
112 16
        if (is_float($code) || $code < 100 || $code >= 600) {
113 1
            throw new \InvalidArgumentException(sprintf('Invalid status code %d', $code));
114
        }
115
116 15
        $instance->statusCode = $code;
117 15
        $instance->reasonPhrase = $reasonPhrase;
118 15
        return $instance;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     *
124
     * @link http://tools.ietf.org/html/rfc7231#section-6
125
     * @link http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
126
     * @return string Reason phrase; must return an empty string if none present.
127
     */
128 2
    public function getReasonPhrase()
129
    {
130 2
        if ($this->reasonPhrase === '') {
131 1
            return $this->messages[$this->statusCode];
132
        }
133
134 1
        return $this->reasonPhrase;
135
    }
136
}
137