Completed
Push — master ( 95d224...c6b730 )
by Jay
04:33
created

Response::phpStrLen()   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
            $strLen = $this->phpStrLen($this->getBody());
102 1
            if ($strLen) {
103 1
                $this->setHeader($this->getContentLengthHeader($strLen));
104 1
            }
105 1
            foreach ($this->getHeaders() as $headers) {
106 1
                foreach ($headers as $header) {
107
                    /* @var $header Response\Header */
108 1
                    $header->render();
109 1
                }
110 1
            }
111 1
        }
112 3
        echo $this->getBody();
113 3
        if ($this->getReturnCode()) {
114 1
            $this->exitPhp($this->getReturnCode());
115 1
        }
116 3
    }
117
118
    /**
119
     * @param int $size
120
     * @return Response\Header\ContentLength
121
     * @codeCoverageIgnore
122
     */
123
    protected function getContentLengthHeader($size)
124
    {
125
        return new Response\Header\ContentLength((int)$size);
126
    }
127
128
    /**
129
     * @param string $string
130
     * @return int
131
     * @codeCoverageIgnore
132
     */
133
    protected function phpStrLen($string)
134
    {
135
        return strlen($string);
136
    }
137
138
    /**
139
     * @param int $returnCode
140
     * @codeCoverageIgnore
141
     */
142
    protected function exitPhp($returnCode = 0)
143
    {
144
        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...
145
    }
146
147
    /**
148
     * Define a PHP Cli return code - 0 (default) is success, another error code > 0 for whatever
149
     * @param int $returnCode
150
     * @return $this
151
     */
152 2
    public function setReturnCode($returnCode = 0)
153
    {
154 2
        $this->returnCode = (int)$returnCode;
155 2
        return $this;
156
    }
157
158
    /**
159
     * Get defined PHP Cli return code - 0 (default) is success, another error code > 0 for whatever
160
     * @return int
161
     */
162 4
    public function getReturnCode()
163
    {
164 4
        return (int)$this->returnCode;
165
    }
166
}
167