Completed
Push — master ( 0a7f71...7f7c4d )
by Oleg
03:31
created

Request::getRequestPayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php /** MicroRequest */
2
3
namespace Micro\Web;
4
5
/**
6
 * Request class file.
7
 *
8
 * @author Oleg Lunegov <[email protected]>
9
 * @link https://github.com/lugnsk/micro
10
 * @copyright Copyright &copy; 2013 Oleg Lunegov
11
 * @license /LICENSE
12
 * @package Micro
13
 * @subpackage Web
14
 * @version 1.0
15
 * @since 1.0
16
 */
17
class Request implements IRequest
18
{
19
    /** @var bool $cli Is running as CLI */
20
    protected $cli;
21
    /** @var array $data Data from request */
22
    protected $data;
23
24
25
    /**
26
     * Constructor Request
27
     *
28
     * @access public
29
     *
30
     * @result void
31
     */
32
    public function __construct()
33
    {
34
        $this->cli = php_sapi_name() === 'cli';
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function isCli()
41
    {
42
        return $this->cli;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function isAjax()
0 ignored issues
show
Coding Style introduced by
isAjax uses the super-global variable $_SERVER 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...
49
    {
50
        return !empty($_SERVER['HTTP_X_REQUEST_WITH']) && $_SERVER['HTTP_X_REQUEST_WITH'] === 'XMLHttpRequest';
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function getMethod()
0 ignored issues
show
Coding Style introduced by
getMethod uses the super-global variable $_SERVER 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...
57
    {
58
        return $_SERVER['REQUEST_METHOD'];
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getUserIP()
0 ignored issues
show
Coding Style introduced by
getUserIP uses the super-global variable $_SERVER 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...
65
    {
66
        return !empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getBrowser($agent = null)
0 ignored issues
show
Coding Style introduced by
getBrowser uses the super-global variable $_SERVER 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...
73
    {
74
        return get_browser($agent ?: $_SERVER['HTTP_USER_AGENT'], true);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function getArguments()
81
    {
82
        global $argv;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
83
84
        return $argv;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function getFiles($className = '\Micro\web\Uploader')
0 ignored issues
show
Coding Style introduced by
getFiles uses the super-global variable $_FILES 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...
91
    {
92
        if (!is_array($_FILES)) {
93
            return false;
94
        }
95
96
        return  new $className($_FILES);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function getStorage($name)
0 ignored issues
show
Coding Style introduced by
getStorage uses the super-global variable $GLOBALS 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...
103
    {
104
        return $GLOBALS[$name];
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function setStorage($name, array $data = [])
0 ignored issues
show
Coding Style introduced by
setStorage uses the super-global variable $GLOBALS 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...
111
    {
112
        $GLOBALS[$name] = $data;
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function query($name)
119
    {
120
        return $this->getVar($name, '_GET');
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126
    public function getVar($name, $storage)
0 ignored issues
show
Coding Style introduced by
getVar uses the super-global variable $GLOBALS 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...
127
    {
128
        return array_key_exists($name, $GLOBALS[$storage]) ? $GLOBALS[$storage][$name] : null;
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function post($name)
135
    {
136
        return $this->getVar($name, '_POST');
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function cookie($name)
143
    {
144
        return $this->getVar($name, '_COOKIE');
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    public function session($name)
151
    {
152
        return $this->getVar($name, '_SESSION');
153
    }
154
155
    /**
156
     * Get value by key from server storage
157
     *
158
     * @access public
159
     *
160
     * @param string $name Key name
161
     *
162
     * @return bool
163
     */
164
    public function server($name)
165
    {
166
        return $this->getVar($name, '_SERVER');
167
    }
168
169
    /**
170
     * @inheritdoc
171
     */
172
    public function setQuery($name, $value)
173
    {
174
        $this->setVar($name, $value, '_GET');
175
    }
176
177
    /**
178
     * @inheritdoc
179
     */
180
    public function setVar($name, $value, $storage)
0 ignored issues
show
Coding Style introduced by
setVar uses the super-global variable $GLOBALS 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...
181
    {
182
        $GLOBALS[$storage][$name] = $value;
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188
    public function setPost($name, $value)
189
    {
190
        $this->setVar($name, $value, '_POST');
191
    }
192
193
    /**
194
     * @inheritdoc
195
     */
196
    public function setCookie($name, $value)
197
    {
198
        $this->setVar($name, $value, '_COOKIE');
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    public function setSession($name, $value)
205
    {
206
        $this->setVar($name, $value, '_SESSION');
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212
    public function unsetQuery($name)
213
    {
214
        $this->unsetVar($name, '_GET');
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220
    public function unsetVar($name, $storage)
0 ignored issues
show
Coding Style introduced by
unsetVar uses the super-global variable $GLOBALS 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...
221
    {
222
        unset($GLOBALS[$storage][$name]);
223
    }
224
225
    /**
226
     * @inheritdoc
227
     */
228
    public function unsetPost($name)
229
    {
230
        $this->unsetVar($name, '_POST');
231
    }
232
233
    /**
234
     * @inheritdoc
235
     */
236
    public function unsetSession($name)
237
    {
238
        $this->unsetVar($name, '_SESSION');
239
    }
240
241
    /**
242
     * @inheritdoc
243
     */
244
    public function getRequestPayload()
245
    {
246
        return file_get_contents('php://input');
247
    }
248
}
249