Passed
Push — psr-implementation ( 26c20f...5f91a5 )
by Charis
02:10 queued 17s
created

Response::getReasonPhrase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php 
2
3
namespace One\Http;
4
5
use One\Http\Message;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\StreamInterface;
8
use function One\stream_for;
9
10
class Response extends Message implements ResponseInterface
11
{
12
13
     /** @var array Map of standard HTTP status code/reason phrases */
14
    private static $phrases = [
15
        100 => 'Continue',
16
        101 => 'Switching Protocols',
17
        102 => 'Processing',
18
        200 => 'OK',
19
        201 => 'Created',
20
        202 => 'Accepted',
21
        203 => 'Non-Authoritative Information',
22
        204 => 'No Content',
23
        205 => 'Reset Content',
24
        206 => 'Partial Content',
25
        207 => 'Multi-status',
26
        208 => 'Already Reported',
27
        300 => 'Multiple Choices',
28
        301 => 'Moved Permanently',
29
        302 => 'Found',
30
        303 => 'See Other',
31
        304 => 'Not Modified',
32
        305 => 'Use Proxy',
33
        306 => 'Switch Proxy',
34
        307 => 'Temporary Redirect',
35
        400 => 'Bad Request',
36
        401 => 'Unauthorized',
37
        402 => 'Payment Required',
38
        403 => 'Forbidden',
39
        404 => 'Not Found',
40
        405 => 'Method Not Allowed',
41
        406 => 'Not Acceptable',
42
        407 => 'Proxy Authentication Required',
43
        408 => 'Request Time-out',
44
        409 => 'Conflict',
45
        410 => 'Gone',
46
        411 => 'Length Required',
47
        412 => 'Precondition Failed',
48
        413 => 'Request Entity Too Large',
49
        414 => 'Request-URI Too Large',
50
        415 => 'Unsupported Media Type',
51
        416 => 'Requested range not satisfiable',
52
        417 => 'Expectation Failed',
53
        418 => 'I\'m a teapot',
54
        422 => 'Unprocessable Entity',
55
        423 => 'Locked',
56
        424 => 'Failed Dependency',
57
        425 => 'Unordered Collection',
58
        426 => 'Upgrade Required',
59
        428 => 'Precondition Required',
60
        429 => 'Too Many Requests',
61
        431 => 'Request Header Fields Too Large',
62
        451 => 'Unavailable For Legal Reasons',
63
        500 => 'Internal Server Error',
64
        501 => 'Not Implemented',
65
        502 => 'Bad Gateway',
66
        503 => 'Service Unavailable',
67
        504 => 'Gateway Time-out',
68
        505 => 'HTTP Version not supported',
69
        506 => 'Variant Also Negotiates',
70
        507 => 'Insufficient Storage',
71
        508 => 'Loop Detected',
72
        511 => 'Network Authentication Required',
73
    ];
74
    /** @var string */
75
    private $reasonPhrase = '';
76
    /** @var int */
77
    private $statusCode = 200;
78
    /**
79
     * @param int                                  $status  Status code
80
     * @param array                                $headers Response headers
81
     * @param string|null|resource|StreamInterface $body    Response body
82
     * @param string                               $version Protocol version
83
     * @param string|null                          $reason  Reason phrase (when empty a default will be used based on the status code)
84
     */
85
    public function __construct(
86
        $status = 200,
87
        array $headers = [],
88
        $body = null,
89
        $version = '1.1',
90
        $reason = null
91
    ) {
92
        $this->statusCode = (int) $status;
93
        if ($body !== '' && $body !== null) {
94
            $this->stream = stream_for($body);
95
        }
96
        $this->setHeaders($headers);
97
        if ($reason == '' && isset(self::$phrases[$this->statusCode])) {
98
            $this->reasonPhrase = self::$phrases[$this->statusCode];
99
        } else {
100
            $this->reasonPhrase = (string) $reason;
101
        }
102
        $this->protocol = $version;
103
    }
104
105
    public function getStatusCode()
106
    {
107
        return $this->statusCode;
108
    }
109
110
    public function getReasonPhrase()
111
    {
112
        return $this->reasonPhrase;
113
    }
114
115
    public function withStatus($code, $reasonPhrase = '')
116
    {
117
        $new = clone $this;
118
        $new->statusCode = (int) $code;
119
        if ($reasonPhrase == '' && isset(self::$phrases[$new->statusCode])) {
120
            $reasonPhrase = self::$phrases[$new->statusCode];
121
        }
122
        $new->reasonPhrase = $reasonPhrase;
123
        return $new;
124
    }
125
}
126