Response::getReasonPhrase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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