Response::__toString()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 0
1
<?php
2
3
namespace Saxulum\HttpClient;
4
5
class Response extends AbstractMessage
6
{
7
    /**
8
     * @var int
9
     */
10
    protected $statusCode;
11
12
    /**
13
     * @var string
14
     */
15
    protected $statusMessage;
16
17
    const STATUS_MESSAGE_100 = 'Continue';
18
    const STATUS_MESSAGE_101 = 'Switching Protocols';
19
    const STATUS_MESSAGE_102 = 'Processing';
20
21
    const STATUS_MESSAGE_200 = 'OK';
22
    const STATUS_MESSAGE_201 = 'Created';
23
    const STATUS_MESSAGE_202 = 'Accepted';
24
    const STATUS_MESSAGE_203 = 'Non-Authoritative Information';
25
    const STATUS_MESSAGE_204 = 'No Content';
26
    const STATUS_MESSAGE_205 = 'Reset Content';
27
    const STATUS_MESSAGE_206 = 'Partial Content';
28
    const STATUS_MESSAGE_207 = 'Multi-Status';
29
    const STATUS_MESSAGE_208 = 'Already Reported';
30
    const STATUS_MESSAGE_226 = 'IM Used';
31
32
    const STATUS_MESSAGE_300 = 'Multiple Choices';
33
    const STATUS_MESSAGE_301 = 'Moved Permanently';
34
    const STATUS_MESSAGE_302 = 'Found';
35
    const STATUS_MESSAGE_303 = 'See Other';
36
    const STATUS_MESSAGE_304 = 'Not Modified';
37
    const STATUS_MESSAGE_305 = 'Use Proxy';
38
    const STATUS_MESSAGE_306 = '(reserved)';
39
    const STATUS_MESSAGE_307 = 'Temporary Redirect';
40
    const STATUS_MESSAGE_308 = 'Permanent Redirect';
41
42
    const STATUS_MESSAGE_400 = 'Bad Request';
43
    const STATUS_MESSAGE_401 = 'Unauthorized';
44
    const STATUS_MESSAGE_402 = 'Payment Required';
45
    const STATUS_MESSAGE_403 = 'Forbidden';
46
    const STATUS_MESSAGE_404 = 'Not Found';
47
    const STATUS_MESSAGE_405 = 'Method Not Allowed';
48
    const STATUS_MESSAGE_406 = 'Not Acceptable';
49
    const STATUS_MESSAGE_407 = 'Proxy Authentication Required';
50
    const STATUS_MESSAGE_408 = 'Request Time-out';
51
    const STATUS_MESSAGE_409 = 'Conflict';
52
    const STATUS_MESSAGE_410 = 'Gone';
53
    const STATUS_MESSAGE_411 = 'Length Required';
54
    const STATUS_MESSAGE_412 = 'Precondition Failed';
55
    const STATUS_MESSAGE_413 = 'Request Entity Too Large';
56
    const STATUS_MESSAGE_414 = 'Request-URL Too Long';
57
    const STATUS_MESSAGE_415 = 'Unsupported Media Type';
58
    const STATUS_MESSAGE_416 = 'Requested range not satisfiable';
59
    const STATUS_MESSAGE_417 = 'Expectation Failed';
60
    const STATUS_MESSAGE_418 = 'I’m a teapot';
61
    const STATUS_MESSAGE_420 = 'Policy Not Fulfilled';
62
    const STATUS_MESSAGE_421 = 'There are too many connections from your internet address';
63
    const STATUS_MESSAGE_422 = 'Unprocessable Entity';
64
    const STATUS_MESSAGE_423 = 'Locked';
65
    const STATUS_MESSAGE_424 = 'Failed Dependency';
66
    const STATUS_MESSAGE_425 = 'Unordered Collection';
67
    const STATUS_MESSAGE_426 = 'Upgrade Required';
68
    const STATUS_MESSAGE_428 = 'Precondition Required';
69
    const STATUS_MESSAGE_429 = 'Too Many Requests';
70
    const STATUS_MESSAGE_431 = 'Request Header Fields Too Large';
71
    const STATUS_MESSAGE_444 = 'No Response';
72
    const STATUS_MESSAGE_449 = 'The request should be retried after doing the appropriate action';
73
    const STATUS_MESSAGE_451 = 'Unavailable For Legal Reasons';
74
75
    const STATUS_MESSAGE_500 = 'Internal Server Error';
76
    const STATUS_MESSAGE_501 = 'Not Implemented';
77
    const STATUS_MESSAGE_502 = 'Bad Gateway';
78
    const STATUS_MESSAGE_503 = 'Service Unavailable';
79
    const STATUS_MESSAGE_504 = 'Gateway Time-out';
80
    const STATUS_MESSAGE_505 = 'HTTP Version not supported';
81
    const STATUS_MESSAGE_506 = 'Variant Also Negotiates';
82
    const STATUS_MESSAGE_507 = 'Insufficient Storage';
83
    const STATUS_MESSAGE_508 = 'Loop Detected';
84
    const STATUS_MESSAGE_509 = 'Bandwidth Limit Exceeded';
85
    const STATUS_MESSAGE_510 = 'Not Extended';
86
87
    /**
88
     * @param string      $protocolVersion
89
     * @param int         $statusCode
90
     * @param string      $statusMessage
91
     * @param array       $headers
92
     * @param string|null $content
93
     */
94
    public function __construct(
95
        $protocolVersion,
96
        $statusCode,
97
        $statusMessage,
98
        array $headers,
99
        $content
100
    ) {
101
        $this->protocolVersion = $protocolVersion;
102
        $this->statusCode = $statusCode;
103
        $this->statusMessage = $statusMessage;
104
        $this->headers = $headers;
105
        $this->content = $content;
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function getStatusCode()
112
    {
113
        return $this->statusCode;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getStatusMessage()
120
    {
121
        return $this->statusMessage;
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function __toString()
128
    {
129
        $response = "HTTP/{$this->getProtocolVersion()} {$this->getStatusCode()} {$this->getStatusMessage()}\r\n";
130
        foreach ($this->getHeaders() as $headerName => $headerValue) {
131
            $response .= "{$headerName}: {$headerValue}\r\n";
132
        }
133
134
        if (null !== $this->getContent()) {
135
            $response .= "\r\n{$this->getContent()}\r\n";
136
        }
137
138
        $response .= "\r\n";
139
140
        return $response;
141
    }
142
}
143