Passed
Push — master ( 473203...f7ff8e )
by Radu
01:13
created

Response   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 48
dl 0
loc 100
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A sendContent() 0 3 1
A setStatus() 0 10 2
A setHeader() 0 10 3
A sendHeaders() 0 18 4
A sendHeader() 0 8 1
A getHeaders() 0 3 1
A getHeader() 0 6 2
A send() 0 5 1
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 getHeader($name)
27
    {
28
        if (array_key_exists($name, $this->headers)) {
29
            return $this->headers[$name];
30
        }
31
        return false;
32
    }
33
34
    public function getHeaders()
35
    {
36
        return $this->headers;
37
    }
38
39
    public function setStatus($statusCode)
40
    {
41
        $statusCodes = StatusCode::getSupported();
42
        if (!isset($statusCodes[$statusCode])) {
43
                throw new \WebServCo\Framework\Exceptions\ApplicationException(
44
                    sprintf('Invalid HTTP status code: %s', $statusCode)
45
                );
46
        }
47
        $this->statusCode = $statusCode;
48
        $this->statusText = $statusCodes[$statusCode];
49
    }
50
51
    public function setHeader($name, $value)
52
    {
53
        if ($this->appendCharset) {
54
            switch ($name) {
55
                case 'Content-Type':
56
                    $value .= '; charset=' . $this->charset;
57
                    break;
58
            }
59
        }
60
        $this->headers[$name] = $value;
61
    }
62
63
    public function send()
64
    {
65
        $this->sendHeaders();
66
        $this->sendContent();
67
        return $this->statusCode;
68
    }
69
70
    protected function sendHeader($name, $value, $statusCode)
71
    {
72
        header(
73
            sprintf('%s: %s', $name, $value),
74
            true,
75
            $statusCode
76
        );
77
        return true;
78
    }
79
80
    protected function sendHeaders()
81
    {
82
        foreach ($this->headers as $name => $value) {
83
            if (is_array($value)) {
84
                foreach ($value as $item) {
85
                    $this->sendHeader($name, $item, $this->statusCode);
86
                }
87
            } else {
88
                $this->sendHeader($name, $value, $this->statusCode);
89
            }
90
        }
91
92
        $this->sendHeader('Content-length', strlen($this->content), $this->statusCode);
93
94
        header(
95
            sprintf('HTTP/1.1 %s %s', $this->statusCode, $this->statusText),
96
            true,
97
            $this->statusCode
98
        );
99
    }
100
101
    protected function sendContent()
102
    {
103
        echo $this->content;
104
    }
105
}
106