Completed
Push — master ( 738f49...96678e )
by Tobias
19:54 queued 09:55
created

Response::withStatus()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 7
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 3
nop 2
crap 6
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 View Code Duplication
        if ('' !== $body && null !== $body) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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) || $code < 100 || $code > 599) {
130 5
            throw new \InvalidArgumentException('Status code has to be an integer between 100 and 599');
131 5
        }
132 3
133
        $new = clone $this;
134 5
        $new->statusCode = (int) $code;
135
        if ('' == $reasonPhrase && isset(self::$phrases[$new->statusCode])) {
136 5
            $reasonPhrase = self::$phrases[$new->statusCode];
137
        }
138
        $new->reasonPhrase = $reasonPhrase;
139
140
        return $new;
141
    }
142
}
143