Passed
Branch master (b52c46)
by refat
04:18
created

Request::prepareUrl()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 0
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace System\Http;
4
5
use System\Application;
6
7
class Request
8
{
9
    /**
10
     * Application Object
11
     *
12
     * @var \System\Application
13
     */
14
    private $app;
15
16
    /**
17
     * Url
18
     *
19
     * @var string
20
     */
21
    private $url;
22
23
    /**
24
     * Base Url
25
     *
26
     * @var string
27
     */
28
    private $baseUrl;
29
30
    /**
31
     * Host
32
     *
33
     * @var string
34
     */
35
    private $host;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param \System\Application $app
41
     */
42
    public function __construct(Application $app)
43
    {
44
        $this->app = $app;
45
    }
46
47
    /**
48
     * Prepare url
49
     *
50
     * @return void
51
     */
52
    public function prepareUrl()
53
    {
54
        $script = dirname($this->server('SCRIPT_NAME'));
55
56
        $requestUri = $this->server('REQUEST_URI');
57
58
        if (strpos($requestUri, '?')) {
59
            list($requestUri) = explode('?', $requestUri);
60
        }
61
62
        $this->url = cleanUrl($script, $requestUri);
63
64
        $REQUEST_PROTOCOL = $this->app->http->isSecure() ? 'https' : 'http';
0 ignored issues
show
Documentation introduced by
The property http does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
65
66
        $this->host = $REQUEST_PROTOCOL . '://' . $this->server('HTTP_HOST');
67
68
        $this->baseUrl = $this->host . $requestUri;
69
    }
70
71
    /**
72
     * Get value from spcefic request type
73
     *
74
     * @param array $requestType
75
     * @param string $key
76
     * @return mixed
77
     */
78
    private function getValueOfRequest($requestType, $key)
79
    {
80
        $value = array_get($requestType, $key);
81
82
        if (is_array($value)) {
83
            $value = array_filter($value);
84
        } else {
85
            $value = trim($value);
86
        }
87
88
        return $value;
89
    }
90
91
    /**
92
     * Get value from $_GET by the given key
93
     *
94
     * @param string $key
95
     * @return mixed
96
     */
97
    public function get($key = null)
98
    {
99
        if ($key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
100
            return $this->getValueOfRequest($_GET, $key);
101
        }
102
        return $_GET;
103
    }
104
105
    /**
106
     * Get value from $_POST by the given key
107
     *
108
     * @param string $key
109
     * @return mixed
110
     */
111
    public function post($key = null)
112
    {
113
        if ($key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
114
            return $this->getValueOfRequest($_POST, $key);
115
        }
116
        return $_POST;
117
    }
118
119
    /**
120
     * Get value from $_FILES by the given key
121
     *
122
     * @return mixed
123
     */
124
    public function file($key = null)
125
    {
126
        if ($key) {
127
            return $this->getValueOfRequest($_FILES, $key);
128
        }
129
        return $_FILES;
130
    }
131
132
    /**
133
     * Set value To $_POST For the given key
134
     *
135
     * @param string $key
136
     * @param mixed $value
137
     * @return void
138
     */
139
    public function setPost($key, $value)
140
    {
141
        $_POST[$key] = $value;
142
    }
143
144
    /**
145
     * Set value To $_GET For the given key
146
     *
147
     * @param string $key
148
     * @param mixed $value
149
     * @return void
150
     */
151
    public function setGet($key, $value)
152
    {
153
        $_GET[$key] = $value;
154
    }
155
156
    /**
157
     * Set value To $_FILES For the given key
158
     *
159
     * @param string $key
160
     * @param mixed $value
161
     * @return void
162
     */
163
    public function setFile($key, $value)
164
    {
165
        $_FILES[$key] = $value;
166
    }
167
168
    /**
169
     * Get value from $_SERVER by the given key
170
     *
171
     * @param string $key
172
     * @return string
173
     */
174
    public function server($key)
175
    {
176
        return array_get($_SERVER, $key);
177
    }
178
179
    /**
180
     * Get current request method
181
     *
182
     * @return string
183
     */
184
    public function method()
185
    {
186
        return $this->server('REQUEST_METHOD');
187
    }
188
189
    /**
190
     * Get the referer link
191
     *
192
     * @return string
193
     */
194
    public function referer()
195
    {
196
        return $this->server('HTTP_REFERER');
197
    }
198
199
    /**
200
     * Get full url of the script
201
     *
202
     * @return string
203
     */
204
    public function baseUrl()
205
    {
206
        return $this->baseUrl;
207
    }
208
209
    /**
210
     * Get only relative url (clean url)
211
     *
212
     * @return string
213
     */
214
    public function url()
215
    {
216
        return $this->url;
217
    }
218
219
    /**
220
     * Get only host
221
     *
222
     * @return string
223
     */
224
    public function host()
225
    {
226
        return $this->host;
227
    }
228
229
    /**
230
     * Check if the request to the admin panel
231
     *
232
     * @return bool
233
     */
234
    public function isRequestToAdminManagement()
235
    {
236
        $url = $this->url;
237
        $url = ltrim($url, '/');
238
        $url = explode('/', $url)[0];
239
240
        return $url == 'admin';
241
    }
242
243
    /**
244
     * Check the request method
245
     *
246
     * @return bool
247
     */
248
    public function isMatchingRequestMethod($methods = ['GET'])
249
    {
250
        if (!is_array($methods)) {
251
            $methods = [$methods];
252
        }
253
254
        if (empty($methods)) {
255
            $methods = ['GET'];
256
        }
257
258
        foreach ($methods as $method) {
259
            if ($this->method() == strtoupper($method)) {
260
                return true;
261
            }
262
        }
263
264
        return false;
265
    }
266
267
    /**
268
     * Check if the request can be Continued
269
     * @property object $load
270
     * @return bool
271
     */
272
    public function canRequestContinue($middlewares)
273
    {
274
        if (empty($middlewares)) {
275
            return true;
276
        }
277
278
        foreach ($middlewares as $middleware) {
279
            $output = $this->app->load->middleware($middleware)->handle();
0 ignored issues
show
Documentation introduced by
The property load does not exist on object<System\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
280
281
            if (!$output) {
282
                return false;
283
            }
284
        }
285
286
        return true;
287
    }
288
}
289