Completed
Push — master ( 916621...e2c0cd )
by Jay
03:47
created

Response::getContentLengthHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
namespace FMUP;
3
4
/**
5
 * Class Response
6
 * @package FMUP
7
 */
8
class Response
9
{
10
    use Sapi\OptionalTrait;
11
    /**
12
     * @var array
13
     */
14
    private $headers = array();
15
    /**
16
     * @var string
17
     */
18
    private $body;
19
    /**
20
     * @var int
21
     */
22
    private $returnCode = 0;
23
24
    /**
25
     * Add a header to send in response
26
     *
27
     * @param Response\Header $header
28
     * @return $this
29
     */
30 6
    public function addHeader(Response\Header $header)
31
    {
32 6
        if (!array_key_exists($header->getType(), $this->headers)) {
33 6
            $this->setHeader($header);
34 6
        } else {
35 4
            array_push($this->headers[$header->getType()], $header);
36
        }
37 6
        return $this;
38
    }
39
40
    /**
41
     * Get all headers defined
42
     * @return array
43
     */
44 7
    public function getHeaders()
45
    {
46 7
        return $this->headers;
47
    }
48
49
    /**
50
     * Define a specific header
51
     * @param Response\Header $header
52
     * @return $this
53
     */
54 7
    public function setHeader(Response\Header $header)
55
    {
56 7
        $this->headers[$header->getType()] = array($header);
57 7
        return $this;
58
    }
59
60
    /**
61
     * Clear headers or a specific one
62
     * @param string|null $name
63
     * @return $this
64
     */
65 1
    public function clearHeader($name = null)
66
    {
67 1
        if (!is_null($name)) {
68 1
            unset($this->headers[$name]);
69 1
        } else {
70 1
            $this->headers = array();
71
        }
72 1
        return $this;
73
    }
74
75
    /**
76
     * Define the body of the Response
77
     * @param string $body
78
     * @return $this
79
     */
80 8
    public function setBody($body)
81
    {
82 8
        $this->body = (string)$body;
83 8
        return $this;
84
    }
85
86
    /**
87
     * Retrieve defined body
88
     * @return string
89
     */
90 9
    public function getBody()
91
    {
92 9
        return (string)$this->body;
93
    }
94
95
    /**
96
     * Sends header and response
97
     */
98 3
    public function send()
99
    {
100 3
        if ($this->getSapi()->get() != Sapi::CLI) {
101 1
            $this->setHeader($this->getContentLengthHeader(mb_strlen($this->getBody())));
102 1
            foreach ($this->getHeaders() as $headers) {
103 1
                foreach ($headers as $header) {
104
                    /* @var $header Response\Header */
105 1
                    $header->render();
106 1
                }
107 1
            }
108 1
        }
109 3
        echo $this->getBody();
110 3
        if ($this->getReturnCode()) {
111 1
            $this->exitPhp($this->getReturnCode());
112 1
        }
113 3
    }
114
115
    /**
116
     * @param int $size
117
     * @return Response\Header\ContentLength
118
     * @codeCoverageIgnore
119
     */
120
    protected function getContentLengthHeader($size)
121
    {
122
        return new Response\Header\ContentLength((int)$size);
123
    }
124
125
    /**
126
     * @param int $returnCode
127
     * @codeCoverageIgnore
128
     */
129
    protected function exitPhp($returnCode = 0)
130
    {
131
        exit((int)$returnCode);
0 ignored issues
show
Coding Style Compatibility introduced by
The method exitPhp() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
132
    }
133
134
    /**
135
     * Define a PHP Cli return code - 0 (default) is success, another error code > 0 for whatever
136
     * @param int $returnCode
137
     * @return $this
138
     */
139 2
    public function setReturnCode($returnCode = 0)
140
    {
141 2
        $this->returnCode = (int)$returnCode;
142 2
        return $this;
143
    }
144
145
    /**
146
     * Get defined PHP Cli return code - 0 (default) is success, another error code > 0 for whatever
147
     * @return int
148
     */
149 4
    public function getReturnCode()
150
    {
151 4
        return (int)$this->returnCode;
152
    }
153
}
154