HttpResponse   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 2
dl 0
loc 149
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setSession() 0 4 1
A removeSession() 0 4 1
A addCookie() 0 7 2
A removeCookie() 0 5 1
A getResponseBag() 0 4 1
A write() 0 4 1
A writeDebug() 0 9 2
A emptyResponse() 0 4 1
A addHeader() 0 4 1
A getHeaders() 0 13 3
A setResponseCode() 0 4 1
A getResponseCode() 0 4 1
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
    public function __construct()
29
    {
30
        $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
    public function getResponseBag()
89
    {
90
        return $this->response;
91
    }
92
93
    /**
94
     * Add an array, model or stdClass to be processed.
95
     *
96
     * @param mixed $object
97
     */
98
    public function write($object)
99
    {
100
        $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
    public function emptyResponse()
121
    {
122
        $this->response = new ResponseBag();
123
    }
124
125
    public function addHeader($header, $value)
126
    {
127
        $this->headers[$header][] = $value;
128
    }
129
130
    public function getHeaders()
131
    {
132
        $result = [];
133
        foreach ($this->headers as $header => $values) {
134
            $replace = true;
135
            foreach ($values as $value) {
136
                $result[] = [ "$header: $value", $replace ];
137
                $replace = false;
138
            }
139
        }
140
141
        return $result;
142
    }
143
144
    public function setResponseCode($code)
145
    {
146
        $this->responseCode = $code;
147
    }
148
149
    public function getResponseCode()
150
    {
151
        return $this->responseCode;
152
    }
153
}
154