Completed
Push — master ( e33e66...44de45 )
by Tobias
03:12
created

Response::__construct()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

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