Passed
Push — develop ( 91c1d2...446210 )
by Schlaefer
41s
created

Response::createResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * the Response class
5
 */
6
7
namespace Phile\Core;
8
9
use Interop\Http\Factory\ResponseFactoryInterface;
10
use Zend\Diactoros\Response\HtmlResponse;
11
use Zend\Diactoros\Response\RedirectResponse;
12
use Zend\Diactoros\Response as Psr7Response;
13
14
/**
15
 * the Response class is responsible for sending a HTTP response to the client
16
 *
17
 * Response is chainable and can be used anywhere:
18
 *
19
 *     (new Respose)->setBody('Hello World')->send();
20
 *
21
 * After send() Phile is terminated.
22
 *
23
 * @author  PhileCMS
24
 * @link    https://philecms.com
25
 * @license http://opensource.org/licenses/MIT
26
 * @package Phile
27
 */
28
class Response implements ResponseFactoryInterface
29
{
30
    /**
31
     * @var string HTTP body
32
     */
33
    protected $body = '';
34
35
    /**
36
     * @var string charset
37
     */
38
    protected $charset = 'utf-8';
39
40
    /**
41
     * @var array HTTP-headers
42
     */
43
    protected $headers = [];
44
45
    /**
46
     * @var int HTTP status code
47
     */
48
    protected $statusCode = 200;
49
50
    /**
51
     * Creates a PSR-7 response
52
     */
53 1
    public function createResponse($code = 200)
54
    {
55 1
        return new Psr7Response('php://memory', $code);
56
    }
57
58
    /**
59
     * Creates PSR-7 HTML response
60
     */
61 3
    public function createHtmlResponse($body, $code = 200)
62
    {
63 3
        return new HtmlResponse($body, $code);
64
    }
65
66
    /**
67
     * Creates PSR-7 redirect response
68
     */
69 1
    public function createRedirectResponse($url, $code = 302)
70
    {
71 1
        return new RedirectResponse($url, $code);
72
    }
73
74
    /**
75
     * redirect to another URL
76
     *
77
     * @param string $url        URL
78
     * @param int    $statusCode
79
     */
80 1
    public function redirect($url, $statusCode = 302)
81
    {
82 1
        $this->setStatusCode($statusCode)
83 1
            ->setHeader('Location', $url, true)
84 1
            ->setBody('')
85 1
            ->send()
86 1
            ->stop();
87 1
    }
88
89
    /**
90
     * set the response body
91
     *
92
     * @param  $body
93
     * @return $this
94
     */
95 2
    public function setBody($body)
96
    {
97 2
        $this->body = $body;
98 2
        return $this;
99
    }
100
101
    /**
102
     * set the response character-set
103
     *
104
     * @param  $charset
105
     * @return $this
106
     */
107 5
    public function setCharset($charset)
108
    {
109 5
        $this->charset = $charset;
110 5
        return $this;
111
    }
112
113
    /**
114
     * set a response HTTP-header
115
     *
116
     * @param  string $key
117
     * @param  string $value
118
     * @param  bool   $clear clear out any existing headers
119
     * @return $this
120
     */
121 1
    public function setHeader($key, $value, $clear = false)
122
    {
123 1
        if ($clear) {
124
            $this->headers = [];
125
        }
126 1
        $this->headers[$key] = "$key: $value";
127 1
        return $this;
128
    }
129
130
    /**
131
     * set the response HTTP status code
132
     *
133
     * @param  $code
134
     * @return $this
135
     */
136
    public function setStatusCode($code)
137
    {
138
        $this->statusCode = $code;
139
        return $this;
140
    }
141
142
    /**
143
     * sends the HTTP response
144
     *
145
     * @return $this
146
     */
147 3
    public function send()
148
    {
149 3
        if (!isset($this->headers['Content-Type'])) {
150 3
            $this->setHeader('Content-Type', 'text/html; charset=' . $this->charset);
151
        }
152 3
        $this->outputHeader();
153 3
        http_response_code($this->statusCode);
154 3
        echo $this->body;
155 3
        return $this;
156
    }
157
158
    /**
159
     * helper for easy testing
160
     */
161
    public function stop()
162
    {
163
        die();
164
    }
165
166
    /**
167
     * output all set response headers
168
     */
169 2
    protected function outputHeader()
170
    {
171 2
        foreach ($this->headers as $header) {
172
            header($header);
173
        }
174 2
    }
175
}
176