Response   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 79
c 1
b 0
f 0
dl 0
loc 128
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A withStatus() 0 9 3
A __construct() 0 18 5
A getReasonPhrase() 0 3 1
A getStatusCode() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace One\Http;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\StreamInterface;
7
8
class Response extends Message implements ResponseInterface
9
{
10
    /**
11
     * array Map of standard HTTP status code/reason phrases
12
     * @var array<string>
13
     */
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
75
    /** @var string */
76
    private $reasonPhrase = '';
77
78
    /** @var int */
79
    private $statusCode = 200;
80
81
    /**
82
     * @param int                                  $status  Status code
83
     * @param array                                $headers Response headers
84
     * @param string|resource|StreamInterface|null $body    Response body
85
     * @param string                               $version Protocol version
86
     * @param string|null                          $reason  Reason phrase (when empty a default will be used based on the status code)
87
     */
88
    public function __construct(
89
        int $status = 200,
90
        array $headers = [],
91
        $body = null,
92
        string $version = '1.1',
93
        $reason = null
94
    ) {
95
        $this->statusCode = (int) $status;
96
        if ($body !== '' && $body !== null) {
97
            $this->stream = \One\stream_for($body);
98
        }
99
        $this->setHeaders($headers);
100
        if (empty($reason) && isset(self::$phrases[$this->statusCode])) {
101
            $this->reasonPhrase = self::$phrases[$this->statusCode];
102
        } else {
103
            $this->reasonPhrase = (string) $reason;
104
        }
105
        $this->protocol = $version;
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111
    public function getStatusCode()
112
    {
113
        return $this->statusCode;
114
    }
115
116
    /**
117
     * @inheritdoc
118
     */
119
    public function getReasonPhrase()
120
    {
121
        return $this->reasonPhrase;
122
    }
123
124
    /**
125
     * @inheritdoc
126
     */
127
    public function withStatus($code, $reasonPhrase = '')
128
    {
129
        $new = clone $this;
130
        $new->statusCode = (int) $code;
131
        if ($reasonPhrase === '' && isset(self::$phrases[$new->statusCode])) {
132
            $reasonPhrase = self::$phrases[$new->statusCode];
133
        }
134
        $new->reasonPhrase = $reasonPhrase;
135
        return $new;
136
    }
137
}
138