Completed
Pull Request — master (#5)
by Joao
07:19
created

HttpResponse::addHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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
    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)
0 ignored issues
show
Coding Style introduced by
setSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
removeSession uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
removeCookie uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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. In case the error it will showed and the result a node called "debug" will be added.
105
     *
106
     * @param string $key
107
     * @param mixed $string
108
     */
109
    public function writeDebug($key, $string)
110
    {
111
        if (is_null($this->responseDebug)) {
112
            $this->responseDebug = new ResponseBag();
113
            $this->response->add($this->responseDebug);
114
        }
115
        $this->responseDebug->add(['debug' => [$key => $string]]);
116
        ErrorHandler::getInstance()->addExtraInfo($key, serialize($string));
117
    }
118
119
    public function emptyResponse()
120
    {
121
        $this->response = new ResponseBag();
122
    }
123
124
    public function addHeader($header, $value)
125
    {
126
        $this->headers[$header][] = $value;
127
    }
128
129
    public function getHeaders()
130
    {
131
        $result = [];
132
        foreach ($this->headers as $header => $values) {
133
            $replace = true;
134
            foreach ($values as $value) {
135
                $result[] = [ "$header: $value", $replace ];
136
                $replace = false;
137
            }
138
        }
139
140
        return $result;
141
    }
142
143
    public function setResponseCode($code)
144
    {
145
        $this->responseCode = $code;
146
    }
147
148
    public function getResponseCode()
149
    {
150
        return $this->responseCode;
151
    }
152
}
153