HttpResponse   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Test Coverage

Coverage 54.29%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 27
c 3
b 0
f 0
dl 0
loc 153
ccs 19
cts 35
cp 0.5429
rs 10
wmc 16

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