HttpResponse   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 12
Bugs 1 Features 3
Metric Value
c 12
b 1
f 3
dl 0
loc 241
wmc 18
lcom 1
cbo 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setStatusCode() 0 11 3
A getStatusCode() 0 4 1
A addHeader() 0 4 1
A setHeader() 0 6 1
A getHeaders() 0 10 1
A addCookie() 0 4 1
A deleteCookie() 0 6 1
A setContent() 0 4 1
A getContent() 0 4 1
A redirect() 0 5 1
A getRequestLineHeaders() 0 15 1
A getStandardHeaders() 0 12 3
A getCookieHeaders() 0 10 2
1
<?php
2
3
namespace Http;
4
5
class HttpResponse implements Response
6
{
7
    private $version = '1.1';
8
    private $statusCode = 200;
9
    private $statusText = 'OK';
10
    private $headers = [];
11
    private $cookies = [];
12
    private $content;
13
14
    private $statusTexts = [
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
        226 => 'IM Used',
28
        300 => 'Multiple Choices',
29
        301 => 'Moved Permanently',
30
        302 => 'Found',
31
        303 => 'See Other',
32
        304 => 'Not Modified',
33
        305 => 'Use Proxy',
34
        306 => 'Reserved',
35
        307 => 'Temporary Redirect',
36
        308 => 'Permanent Redirect',
37
        400 => 'Bad Request',
38
        401 => 'Unauthorized',
39
        402 => 'Payment Required',
40
        403 => 'Forbidden',
41
        404 => 'Not Found',
42
        405 => 'Method Not Allowed',
43
        406 => 'Not Acceptable',
44
        407 => 'Proxy Authentication Required',
45
        408 => 'Request Timeout',
46
        409 => 'Conflict',
47
        410 => 'Gone',
48
        411 => 'Length Required',
49
        412 => 'Precondition Failed',
50
        413 => 'Request Entity Too Large',
51
        414 => 'Request-URI Too Long',
52
        415 => 'Unsupported Media Type',
53
        416 => 'Requested Range Not Satisfiable',
54
        417 => 'Expectation Failed',
55
        418 => 'I\'m a teapot',
56
        422 => 'Unprocessable Entity',
57
        423 => 'Locked',
58
        424 => 'Failed Dependency',
59
        425 => 'Reserved for WebDAV advanced collections expired proposal',
60
        426 => 'Upgrade Required',
61
        428 => 'Precondition Required',
62
        429 => 'Too Many Requests',
63
        431 => 'Request Header Fields Too Large',
64
        500 => 'Internal Server Error',
65
        501 => 'Not Implemented',
66
        502 => 'Bad Gateway',
67
        503 => 'Service Unavailable',
68
        504 => 'Gateway Timeout',
69
        505 => 'HTTP Version Not Supported',
70
        506 => 'Variant Also Negotiates',
71
        507 => 'Insufficient Storage',
72
        508 => 'Loop Detected',
73
        510 => 'Not Extended',
74
        511 => 'Network Authentication Required',
75
    ];
76
77
    /**
78
     * Sets the HTTP status code.
79
     *
80
     * @param  integer $statusCode
81
     * @param  string  $statusText (optional)
82
     * @return void
83
     */
84
    public function setStatusCode($statusCode, $statusText = null)
85
    {
86
        if ($statusText === null
87
            && array_key_exists((int) $statusCode, $this->statusTexts)
88
        ) {
89
            $statusText = $this->statusTexts[$statusCode];
90
        }
91
92
        $this->statusCode = (int) $statusCode;
93
        $this->statusText = (string) $statusText;
94
    }
95
96
    /**
97
     * Returns the HTTP status code
98
     * @return int
99
     */
100
    public function getStatusCode()
101
    {
102
        return $this->statusCode;
103
    }
104
105
    /**
106
     * Adds a header with the given name.
107
     *
108
     * @param  string $name
109
     * @param  string $value
110
     * @return void
111
     */
112
    public function addHeader($name, $value)
113
    {
114
        $this->headers[$name][] = (string) $value;
115
    }
116
117
    /**
118
     * Sets a new header for the given name.
119
     *
120
     * Replaces all headers with the same names.
121
     *
122
     * @param  string $name
123
     * @param  string $value
124
     * @return void
125
     */
126
    public function setHeader($name, $value)
127
    {
128
        $this->headers[$name] = [
129
            (string) $value,
130
        ];
131
    }
132
133
    /**
134
     * Returns an array with the HTTP headers.
135
     *
136
     * @return array
137
     */
138
    public function getHeaders()
139
    {
140
        $headers = array_merge(
141
            $this->getRequestLineHeaders(),
142
            $this->getStandardHeaders(),
143
            $this->getCookieHeaders()
144
        );
145
146
        return $headers;
147
    }
148
149
    /**
150
     * Adds a new cookie.
151
     *
152
     * @param  Cookie $cookie
153
     * @return void
154
     */
155
    public function addCookie(Cookie $cookie)
156
    {
157
        $this->cookies[$cookie->getName()] = $cookie;
158
    }
159
160
    /**
161
     * Deletes a cookie.
162
     *
163
     * @param  Cookie $cookie
164
     * @return void
165
     */
166
    public function deleteCookie(Cookie $cookie)
167
    {
168
        $cookie->setValue('');
169
        $cookie->setMaxAge(-1);
170
        $this->cookies[$cookie->getName()] = $cookie;
171
    }
172
173
    /**
174
     * Sets the body content.
175
     *
176
     * @param  string $content
177
     * @return void
178
     */
179
    public function setContent($content)
180
    {
181
        $this->content = (string) $content;
182
    }
183
184
    /**
185
     * Returns the body content.
186
     *
187
     * @return string
188
     */
189
    public function getContent()
190
    {
191
        return $this->content;
192
    }
193
194
    /**
195
     * Sets the headers for a redirect.
196
     *
197
     * @param  string $url
198
     * @return void
199
     */
200
    public function redirect($url)
201
    {
202
        $this->setHeader('Location', $url);
203
        $this->setStatusCode(301);
204
    }
205
206
    private function getRequestLineHeaders()
207
    {
208
        $headers = [];
209
210
        $requestLine = sprintf(
211
            'HTTP/%s %s %s',
212
            $this->version,
213
            $this->statusCode,
214
            $this->statusText
215
        );
216
217
        $headers[] = trim($requestLine);
218
219
        return $headers;
220
    }
221
222
    private function getStandardHeaders()
223
    {
224
        $headers = [];
225
226
        foreach ($this->headers as $name => $values) {
227
            foreach ($values as $value) {
228
                $headers[] = "$name: $value";
229
            }
230
        }
231
232
        return $headers;
233
    }
234
235
    private function getCookieHeaders()
236
    {
237
        $headers = [];
238
239
        foreach ($this->cookies as $cookie) {
240
            $headers[] = 'Set-Cookie: ' . $cookie->getHeaderString();
241
        }
242
243
        return $headers;
244
    }
245
}
246