Passed
Pull Request — master (#11)
by Joao
07:11
created

HttpResponse   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 56.41%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 147
ccs 22
cts 39
cp 0.5641
rs 10
c 3
b 0
f 0
wmc 17

13 Methods

Rating   Name   Duplication   Size   Complexity  
A addHeader() 0 3 1
A addCookie() 0 6 2
A removeSession() 0 3 1
A getResponseCode() 0 3 1
A write() 0 3 1
A __construct() 0 3 1
A getHeaders() 0 12 3
A getResponseBag() 0 3 1
A removeCookie() 0 4 1
A emptyResponse() 0 3 1
A setResponseCode() 0 3 1
A setSession() 0 3 1
A writeDebug() 0 8 2
1
<?php
2
3
namespace ByJG\RestServer;
4
5
class HttpResponse
6
{
7
8
    /**
9
     * @var ResponseBag
10
     */
11
    protected $response;
12
13
    /**
14
     * @var ResponseBag
15
     */
16
    protected $responseDebug;
17
18
    /**
19
     * @var array
20
     */
21
    protected $headers = [];
22
23
    /**
24
     * @var int
25
     */
26
    protected $responseCode = 200;
27
28 9
    public function __construct()
29
    {
30 9
        $this->emptyResponse();
31
    }
32
33
    /**
34
     * Add a value in session
35
     *
36
     * @param string $name
37
     * @param string $value
38
     */
39
    public function setSession($name, $value)
40
    {
41
        $_SESSION[$name] = $value;
42
    }
43
44
    /**
45
     * Remove a value in this session
46
     *
47
     * @param string $name
48
     */
49
    public function removeSession($name)
50
    {
51
        unset($_SESSION[$name]);
52
    }
53
54
    /**
55
     * Add a cookie value
56
     *
57
     * @param string $name
58
     * @param string $value
59
     * @param int $expire (seconds from now)
60
     * @param int $path (directory into domain in which the cookie will be available on )
61
     * @param string $domain
62
     */
63
    public function addCookie($name, $value, $expire = null, $path = null, $domain = null)
64
    {
65
        if (!is_null($expire)) {
66
            $expire = time() + $expire;
67
        }
68
        setcookie($name, $value, $expire, $path, $domain);
69
    }
70
71
    /**
72
     * Delete a cookie
73
     *
74
     * @param string $name
75
     */
76
    public function removeCookie($name)
77
    {
78
        setcookie($name, null, time() - 3600);
79
        unset($_COOKIE[$name]);
80
    }
81
82
    /**
83
     * ResponseBag is a collection of objects will be returned to the  client. RestServer call handle the ResponseBag to
84
     * return the proper output. Avoid to use it directly here. Prefer the methods write or writeDebug;
85
     *
86
     * @return ResponseBag
87
     */
88 6
    public function getResponseBag()
89
    {
90 6
        return $this->response;
91
    }
92
93
    /**
94
     * Add an array, model or stdClass to be processed.
95
     *
96
     * @param mixed $object
97
     */
98 4
    public function write($object)
99
    {
100 4
        $this->response->add($object);
101
    }
102
103
    /**
104
     * Added informations for debug purposes only.
105
     * In case the error it will showed and the result a node called "debug" will be added.
106
     *
107
     * @param string $key
108
     * @param mixed $string
109
     */
110
    public function writeDebug($key, $string)
111
    {
112
        if (is_null($this->responseDebug)) {
113
            $this->responseDebug = new ResponseBag();
114
            $this->response->add($this->responseDebug);
115
        }
116
        $this->responseDebug->add(['debug' => [$key => $string]]);
117
        ErrorHandler::getInstance()->addExtraInfo($key, serialize($string));
118
    }
119
120 9
    public function emptyResponse()
121
    {
122 9
        $this->response = new ResponseBag();
123
    }
124
125 1
    public function addHeader($header, $value)
126
    {
127 1
        $this->headers[$header][] = $value;
128
    }
129
130 5
    public function getHeaders()
131
    {
132 5
        $result = [];
133 5
        foreach ($this->headers as $header => $values) {
134 1
            $replace = true;
135 1
            foreach ($values as $value) {
136 1
                $result[] = [ "$header: $value", $replace ];
137 1
                $replace = false;
138
            }
139
        }
140
141 5
        return $result;
142
    }
143
144 1
    public function setResponseCode($code)
145
    {
146 1
        $this->responseCode = $code;
147
    }
148
149 5
    public function getResponseCode()
150
    {
151 5
        return $this->responseCode;
152
    }
153
}
154