Response::withStatus()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 4
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Psr7;
5
6
use InvalidArgumentException;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\StreamInterface;
9
use PTS\Psr7\Response\ServerMessageInterface;
10
use function sprintf;
11
12
class Response extends Message implements ServerMessageInterface, ResponseInterface
13
{
14
15
    use ServerMessage;
16
17
    /** @var array Map of standard HTTP status code/reason phrases */
18
    protected const 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
    protected string $reasonPhrase = '';
80
    protected int $statusCode = 200;
81
82
    /**
83
     * @param int $status Status code
84
     * @param array $headers Response headers
85
     * @param string|resource|StreamInterface|null $body Response body
86
     * @param string $version Protocol version
87
     * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
88
     */
89 38
    public function __construct(
90
        int $status = 200,
91
        array $headers = [],
92
        $body = null,
93
        string $version = '1.1',
94
        string $reason = null
95
    ) {
96
        // If we got no body, defer initialization of the stream until Response::getBody()
97 38
        if ('' !== $body && null !== $body) {
98 4
            $this->stream = Stream::create($body);
99
        }
100
101 38
        $this->statusCode = $status;
102 38
        $this->reasonPhrase = $reason ?? self::PHRASES[$this->statusCode] ?? '';
103 38
        $this->protocol = $version;
104 38
        $this->setHeaders($headers);
105 38
    }
106
107 10
    public function getStatusCode(): int
108
    {
109 10
        return $this->statusCode;
110
    }
111
112 9
    public function getReasonPhrase(): string
113
    {
114 9
        return $this->reasonPhrase;
115
    }
116
117 3
    public function withStatus($code, $reasonPhrase = ''): self
118
    {
119 3
        if ($code < 100 || $code > 599) {
120 1
            $message = sprintf(
121 1
                'Status code has to be an integer between 100 and 599. A status code of %d was given',
122 1
                $code
123
            );
124 1
            throw new InvalidArgumentException($message);
125
        }
126
127 2
        $this->statusCode = $code;
128 2
        $this->reasonPhrase = $reasonPhrase !== '' ? $reasonPhrase : (self::PHRASES[$this->statusCode] ?? '');
129 2
        return $this;
130
    }
131
}