These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | public function __construct() |
||
19 | { |
||
20 | $this->emptyResponse(); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Add a value in session |
||
25 | * |
||
26 | * @param string $name |
||
27 | * @param string $value |
||
28 | */ |
||
29 | public function setSession($name, $value) |
||
0 ignored issues
–
show
|
|||
30 | { |
||
31 | $_SESSION[$name] = $value; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Remove a value in this session |
||
36 | * |
||
37 | * @param string $name |
||
38 | */ |
||
39 | public function removeSession($name) |
||
0 ignored issues
–
show
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...
|
|||
40 | { |
||
41 | unset($_SESSION[$name]); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Add a cookie value |
||
46 | * |
||
47 | * @param string $name |
||
48 | * @param string $value |
||
49 | * @param int $expire (seconds from now) |
||
50 | * @param int $path (directory into domain in which the cookie will be available on ) |
||
51 | * @param string $domain |
||
52 | */ |
||
53 | public function addCookie($name, $value, $expire = null, $path = null, $domain = null) |
||
54 | { |
||
55 | if (!is_null($expire)) { |
||
56 | $expire = time() + $expire; |
||
57 | } |
||
58 | setcookie($name, $value, $expire, $path, $domain); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Delete a cookie |
||
63 | * |
||
64 | * @param string $name |
||
65 | */ |
||
66 | public function removeCookie($name) |
||
0 ignored issues
–
show
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...
|
|||
67 | { |
||
68 | setcookie($name, null, time() - 3600); |
||
69 | unset($_COOKIE[$name]); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * ResponseBag is a collection of objects will be returned to the client. RestServer call handle the ResponseBag to |
||
74 | * return the proper output. Avoid to use it directly here. Prefer the methods write or writeDebug; |
||
75 | * |
||
76 | * @return ResponseBag |
||
77 | */ |
||
78 | public function getResponseBag() |
||
79 | { |
||
80 | return $this->response; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Add an array, model or stdClass to be processed. |
||
85 | * |
||
86 | * @param mixed $object |
||
87 | */ |
||
88 | public function write($object) |
||
89 | { |
||
90 | $this->response->add($object); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Added informations for debug purposes only. In case the error it will showed and the result a node called "debug" will be added. |
||
95 | * |
||
96 | * @param string $key |
||
97 | * @param mixed $string |
||
98 | */ |
||
99 | public function writeDebug($key, $string) |
||
100 | { |
||
101 | if (is_null($this->responseDebug)) { |
||
102 | $this->responseDebug = new ResponseBag(); |
||
103 | $this->response->add($this->responseDebug); |
||
104 | } |
||
105 | $this->responseDebug->add(['debug' => [$key => $string]]); |
||
106 | ErrorHandler::getInstance()->addExtraInfo($key, serialize($string)); |
||
107 | } |
||
108 | |||
109 | public function emptyResponse() |
||
110 | { |
||
111 | $this->response = new ResponseBag(); |
||
112 | } |
||
113 | } |
||
114 |
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: