Passed
Push — master ( 1282f8...92a01c )
by Schlaefer
03:31
created

Response::setHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

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