Passed
Push — master ( 08919b...7317a9 )
by Oleg
04:28
created

Request::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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/linpax/microphp-framework
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 $files $_FILES from request */
22
    protected $files;
23
24
25
    /**
26
     * Constructor Request
27
     *
28
     * @access public
29
     *
30
     * @param array $files
31
     *
32
     * @result void
33
     */
34
    public function __construct(array $files = [])
35
    {
36
        $this->cli = PHP_SAPI === 'cli';
37
38
        $this->files = $files;
39
    }
40
41
    /**
42
     * Get flag of running as CLI
43
     *
44
     * @access public
45
     *
46
     * @return bool
47
     */
48
    public function isCli()
49
    {
50
        return $this->cli;
51
    }
52
53
    /**
54
     * Check request is AJAX ?
55
     *
56
     * @access public
57
     *
58
     * @return bool
59
     */
60
    public function isAjax()
61
    {
62
        return strtolower(filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest';
63
    }
64
65
    /**
66
     * Get request method
67
     *
68
     * @access public
69
     *
70
     * @return string
71
     */
72
    public function getMethod()
73
    {
74
        return filter_input(INPUT_SERVER, 'REQUEST_METHOD');
75
    }
76
77
    /**
78
     * Get user IP-address
79
     *
80
     * @access public
81
     *
82
     * @return string
83
     */
84
    public function getUserIP()
85
    {
86
        return ($ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR')) ? $ip : '127.0.0.1';
87
    }
88
89
    /**
90
     * Get browser data from user user agent string
91
     *
92
     * @access public
93
     *
94
     * @param null|string $agent User agent string
95
     *
96
     * @return mixed
97
     */
98
    public function getBrowser($agent = null)
99
    {
100
        return get_browser($agent ?: filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'), true);
101
    }
102
103
    /**
104
     * Get arguments from command line
105
     *
106
     * @access public
107
     *
108
     * @param string $char -a .. -z option char
109
     * @param string $name --optionName_string
110
     * @param bool|null $required Required value?
111
     *
112
     * @return mixed
113
     */
114
    public function getOption($char = '', $name = '', $required = null)
115
    {
116
        if (!$char && !$name) {
117
            return false;
118
        }
119
120
        if ($char && (1 < strlen($char) || 1 !== preg_match('/^\w$/', $char))) {
121
            return false;
122
        }
123
124
        if ($name && (1 !== preg_match('/^\w+$/', $name))) {
125
            return false;
126
        }
127
128
        switch ($required) {
129
            case true:
130
                $char = $char ? $char.':' : $char;
131
                $name = $name ? $name.':' : $name;
132
                break;
133
            case false:
134
                $char = $char ? $char.'::' : $char;
135
                $name = $name ? $name.'::' : $name;
136
                break;
137
        }
138
139
        $argv = ($opts = getopt($char, [$name])) ? array_shift($opts) : [];
140
141
        return is_array($argv) ? array_shift($argv) : $argv;
142
    }
143
144
    /**
145
     * Get files mapper
146
     *
147
     * @access public
148
     *
149
     * @param string $className Class name of mapper
150
     *
151
     * @return mixed
152
     */
153
    public function getFiles($className = '\Micro\Web\Uploader')
154
    {
155
        return $this->files ? new $className($this->files) : false;
156
    }
157
158
    /**
159
     * Get value by key from query storage
160
     *
161
     * @access public
162
     *
163
     * @param string $name Key name
164
     * @param integer $filter
165
     * @param mixed $options
166
     *
167
     * @return bool
168
     */
169
    public function query($name, $filter = FILTER_DEFAULT, $options = null)
170
    {
171
        return filter_input(INPUT_GET, $name, $filter, $options);
172
    }
173
174
    /**
175
     * Get value by key from post storage
176
     *
177
     * @access public
178
     *
179
     * @param string $name Key name
180
     * @param integer $filter
181
     * @param mixed $options
182
     *
183
     * @return mixed
184
     */
185
    public function post($name, $filter = FILTER_DEFAULT, $options = null)
186
    {
187
        return filter_input(INPUT_POST, $name, $filter, $options);
188
    }
189
190
    /**
191
     * Get value by key from cookie storage
192
     *
193
     * @access public
194
     *
195
     * @param string $name Key name
196
     * @param integer $filter
197
     * @param mixed $options
198
     *
199
     * @return bool
200
     */
201
    public function cookie($name, $filter = FILTER_DEFAULT, $options = null)
202
    {
203
        return filter_input(INPUT_COOKIE, $name, $filter, $options);
204
    }
205
206
    /**
207
     * Get value by key from session storage
208
     *
209
     * @access public
210
     *
211
     * @param string $name Key name
212
     * @param integer $filter
213
     * @param mixed $options
214
     *
215
     * @return bool
216
     */
217
    public function session($name, $filter = FILTER_DEFAULT, $options = null)
218
    {
219
        return filter_input(INPUT_SESSION, $name, $filter, $options);
220
    }
221
222
    /**
223
     * Unset value by key from session storage
224
     *
225
     * @access public
226
     *
227
     * @param string $name Key name
228
     *
229
     * @return void
230
     */
231
    public function unsetSession($name)
0 ignored issues
show
Coding Style introduced by
unsetSession 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...
232
    {
233
        if (array_key_exists($name, $_SESSION)) {
234
            unset($_SESSION[$name]);
235
        }
236
    }
237
238
239
    /**
240
     * Get value by key from server storage
241
     *
242
     * @access public
243
     *
244
     * @param string $name Key name
245
     * @param integer $filter
246
     * @param mixed $options
247
     *
248
     * @return bool
249
     */
250
    public function server($name, $filter = FILTER_DEFAULT, $options = null)
251
    {
252
        return filter_input(INPUT_SERVER, $name, $filter, $options);
253
    }
254
255
    /**
256
     * Get RequestPayload (RAW DATA)
257
     *
258
     * @return string|bool
259
     */
260
    public function requestPayload()
261
    {
262
        return file_get_contents('php://input');
263
    }
264
265
    /**
266
     * Set value into session storage
267
     *
268
     * @access public
269
     *
270
     * @param string $name Key name
271
     * @param string $value Key value
272
     *
273
     * @return void
274
     */
275
    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...
276
    {
277
        $_SESSION[$name] = $value;
278
    }
279
}
280