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

HttpResponse::getResponseBag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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
    /**
126
     * Undocumented function
127
     *
128
     * @param string $header
129
     * @param string|array $value
130
     * @return void
131
     */
132 1
    public function addHeader($header, $value)
133
    {
134 1
        $this->headers[$header] = $value;
135
    }
136
137 5
    public function getHeaders()
138
    {
139 5
        return $this->headers;
140
    }
141
142 1
    public function setResponseCode($code)
143
    {
144 1
        $this->responseCode = $code;
145
    }
146
147 5
    public function getResponseCode()
148
    {
149 5
        return $this->responseCode;
150
    }
151
}
152