Request   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 202
rs 10
c 0
b 0
f 0
wmc 23
lcom 2
cbo 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValueOfRequest() 0 11 2
A get() 0 7 2
A post() 0 7 2
A file() 0 7 2
A server() 0 7 2
A setPost() 0 4 1
A setGet() 0 4 1
A setFile() 0 4 1
A setServer() 0 4 1
A method() 0 4 1
A isRequestToAdminManagement() 0 9 1
A isMatchingRequestMethod() 0 7 2
A canRequestContinue() 0 13 4
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
     * Constructor
18
     *
19
     * @param \System\Application $app
20
     */
21
    public function __construct(Application $app)
22
    {
23
        $this->app = $app;
24
    }
25
26
    /**
27
     * Get value from spcefic request type
28
     *
29
     * @param array $requestType
30
     * @param string $key
31
     * @return mixed
32
     */
33
    private function getValueOfRequest($requestType, $key)
34
    {
35
        $value = array_get($requestType, $key);
36
37
        if (is_array($value)) {
38
            $value = array_filter($value);
39
        } else {
40
            $value = trim($value);
41
        }
42
        return $value;
43
    }
44
45
    /**
46
     * Get value from $_GET by the given key
47
     *
48
     * @param string $key
49
     * @return mixed
50
     */
51
    public function get(string $key = null)
52
    {
53
        if ($key !== null) {
54
            return $this->getValueOfRequest($_GET, $key);
55
        }
56
        return $_GET;
57
    }
58
59
    /**
60
     * Get value from $_POST by the given key
61
     *
62
     * @param string $key
63
     * @return mixed
64
     */
65
    public function post(string $key = null)
66
    {
67
        if ($key !== null) {
68
            return $this->getValueOfRequest($_POST, $key);
69
        }
70
        return $_POST;
71
    }
72
73
    /**
74
     * Get value from $_FILES by the given key
75
     *
76
     * @param string $key
77
     * @return mixed
78
     */
79
    public function file(string $key = null)
80
    {
81
        if ($key !== null) {
82
            return $this->getValueOfRequest($_FILES, $key);
83
        }
84
        return $_FILES;
85
    }
86
87
    /**
88
     * Get value from $_SERVER by the given key
89
     *
90
     * @param string $key
91
     * @return string
92
     */
93
    public function server(string $key)
94
    {
95
        if ($key !== null) {
96
            return $this->getValueOfRequest($_SERVER, $key);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getValueOfRequest($_SERVER, $key); of type array|string adds the type array to the return on line 96 which is incompatible with the return type documented by System\Http\Request::server of type string.
Loading history...
97
        }
98
        return $_SERVER;
99
    }
100
101
    /**
102
     * Set value To $_POST For the given key
103
     *
104
     * @param string $key
105
     * @param mixed $value
106
     * @return void
107
     */
108
    public function setPost(string $key, $value)
109
    {
110
        $_POST[$key] = $value;
111
    }
112
113
    /**
114
     * Set value To $_GET For the given key
115
     *
116
     * @param string $key
117
     * @param mixed $value
118
     * @return void
119
     */
120
    public function setGet(string $key, $value)
121
    {
122
        $_GET[$key] = $value;
123
    }
124
125
    /**
126
     * Set value To $_FILES For the given key
127
     *
128
     * @param string $key
129
     * @param mixed $value
130
     * @return void
131
     */
132
    public function setFile(string $key, $value)
133
    {
134
        $_FILES[$key] = $value;
135
    }
136
137
    /**
138
     * Set value To $_SERVER For the given key
139
     *
140
     * @param string $key
141
     * @param mixed $value
142
     * @return void
143
     */
144
    public function setServer(string $key, $value)
145
    {
146
        $_SERVER[$key] = $value;
147
    }
148
149
    /**
150
     * Get current request method
151
     *
152
     * @return string
153
     */
154
    public function method()
155
    {
156
        return $this->server('REQUEST_METHOD');
157
    }
158
159
    /**
160
     * Check if the request to the admin panel
161
     *
162
     * @return bool
163
     */
164
    public function isRequestToAdminManagement()
165
    {
166
        $url = $this->app->url->parameters();
0 ignored issues
show
Documentation introduced by
The property url 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...
167
        ;
168
        $url = ltrim($url, '/');
169
        $url = explode('/', $url)[0];
170
171
        return $url == 'admin';
172
    }
173
174
    /**
175
     * Check the request method
176
     *
177
     * @param string|array $methods
0 ignored issues
show
Documentation introduced by
There is no parameter named $methods. Did you maybe mean $method?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
178
     * @return bool
179
     */
180
    public function isMatchingRequestMethod($method)
181
    {
182
        if ($this->method() == strtoupper($method)) {
183
            return true;
184
        }
185
        return false;
186
    }
187
188
    /**
189
     * Check if the request can be Continued
190
     *
191
     * @property object $load
192
     * @param array $middlewares
193
     * @return bool
194
     */
195
    public function canRequestContinue(array $middlewares)
196
    {
197
        if (!empty($middlewares)) {
198
            foreach ($middlewares as $middleware) {
199
                $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...
200
201
                if (!$output) {
202
                    return false;
203
                }
204
            }
205
        }
206
        return true;
207
    }
208
}
209