Passed
Pull Request — master (#56)
by Yasin
03:56 queued 02:00
created

Response::__construct()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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