Passed
Push — master ( f17b13...8bf1e0 )
by Radu
01:33
created

Response::sendHeaders()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
namespace WebServCo\Framework\Http;
3
4
class Response extends \WebServCo\Framework\AbstractResponse implements
5
    \WebServCo\Framework\Interfaces\ResponseInterface
6
{
7
    protected $statusText;
8
    protected $headers = [];
9
    protected $appendCharset;
10
    protected $charset;
11
12
    public function __construct($content = null, $statusCode = 200, array $headers = [])
13
    {
14
        $this->appendCharset = false;
15
        $this->setStatus($statusCode);
16
17
        $this->charset = 'utf-8';
18
19
        foreach ($headers as $name => $value) {
20
            $this->setHeader($name, $value);
21
        }
22
23
        $this->setContent($content);
24
    }
25
26
    public function getHeaders()
27
    {
28
        return $this->headers;
29
    }
30
31
    public function setStatus($statusCode)
32
    {
33
        $statusCodes = StatusCode::getSupported();
34
        if (!isset($statusCodes[$statusCode])) {
35
                throw new \WebServCo\Framework\Exceptions\ApplicationException(
36
                    sprintf('Invalid HTTP status code: %s', $statusCode)
37
                );
38
        }
39
        $this->statusCode = $statusCode;
40
        $this->statusText = $statusCodes[$statusCode];
41
    }
42
43
    public function setHeader($name, $value)
44
    {
45
        if ($this->appendCharset) {
46
            switch ($name) {
47
                case 'Content-Type':
48
                    $value .= '; charset=' . $this->charset;
49
                    break;
50
            }
51
        }
52
        $this->headers[$name] = $value;
53
    }
54
55
    public function send()
56
    {
57
        $this->sendHeaders();
58
        $this->sendContent();
59
        return $this->statusCode;
60
    }
61
62
    protected function sendHeader($name, $value, $statusCode)
63
    {
64
        header(
65
            sprintf('%s: %s', $name, $value),
66
            true,
67
            $statusCode
68
        );
69
        return true;
70
    }
71
72
    protected function sendHeaders()
73
    {
74
        foreach ($this->headers as $name => $value) {
75
            if (is_array($value)) {
76
                foreach ($value as $item) {
77
                    $this->sendHeader($name, $item, $this->statusCode);
78
                }
79
            } else {
80
                $this->sendHeader($name, $value, $this->statusCode);
81
            }
82
        }
83
84
        $this->sendHeader('Content-length', strlen($this->content), $this->statusCode);
85
86
        header(
87
            sprintf('HTTP/1.1 %s %s', $this->statusCode, $this->statusText),
88
            true,
89
            $this->statusCode
90
        );
91
    }
92
93
    protected function sendContent()
94
    {
95
        echo $this->content;
96
    }
97
}
98