ServerRequest::getQueryParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
namespace Fyuze\Http\Message;
3
4
use Psr\Http\Message\ServerRequestInterface;
5
use Psr\Http\Message\UriInterface;
6
7
class ServerRequest extends Request implements ServerRequestInterface
8
{
9
10
    /**
11
     * @var
12
     */
13
    protected $serverParams;
14
15
    /**
16
     * @var
17
     */
18
    protected $cookieParams;
19
20
    /**
21
     * @var
22
     */
23
    protected $queryParams;
24
25
    /**
26
     * @var
27
     */
28
    protected $uploadedFiles;
29
30
    /**
31
     * @var
32
     */
33
    protected $parsedBody;
34
35
    /**
36
     * @var
37
     */
38
    protected $attributes = [];
39
40
    /**
41
     * ServerRequest constructor.
42
     * @param string $uri
43
     * @param string $method
44
     * @param array $server
45
     */
46 28
    public function __construct($server = [])
47
    {
48 28
        $this->serverParams = $server;
49
    }
50
51
    /**
52
     * @param string $uri
53
     * @param string $method
54
     * @return static
55
     */
56 17
    public static function create($uri = '/', $method = 'GET')
57
    {
58 17
        $server = array_replace([
59 17
            'SERVER_NAME' => 'localhost',
60 17
            'SERVER_PORT' => 80,
61 17
            'HTTP_HOST' => 'localhost',
62 17
            'HTTP_USER_AGENT' => 'Fyuze/0.1.x',
63 17
            'REMOTE_ADDR' => '127.0.0.1',
64 17
            'SERVER_PROTOCOL' => 'HTTP/1.1',
65 17
            'REQUEST_TIME' => time(),
66 17
            'REQUEST_URI' => (string)$uri,
67 17
            'REQUEST_METHOD' => $method
68 17
        ], $_SERVER);
69
70
71
        /** @todo need a better way to handle this */
72 17
        if (mb_stripos($server['REQUEST_URI'], '/index.php') === 0) {
73 4
            $server['REQUEST_URI'] = str_replace('/index.php', '', $server['REQUEST_URI']);
74
        }
75
76 17
        return (new static(
77 17
            $server
78 17
        ))
79 17
            ->withMethod($method)
80 17
            ->withUri(new Uri($server['REQUEST_URI']))
81 17
            ->withCookieParams($_COOKIE)
82 17
            ->withQueryParams($_GET)
83 17
            ->withParsedBody($_POST)
84 17
            ->withUploadedFiles($_FILES);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     *
90
     * @return array
91
     */
92 2
    public function getServerParams()
93
    {
94 2
        return $this->serverParams;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     *
100
     * @return array
101
     */
102 1
    public function getCookieParams()
103
    {
104 1
        return $this->cookieParams;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * @param array $cookies Array of key/value pairs representing cookies.
111
     * @return self
112
     */
113 18
    public function withCookieParams(array $cookies)
114
    {
115 18
        return $this->_clone('cookieParams', $cookies);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     *
121
     * @return array
122
     */
123 1
    public function getQueryParams()
124
    {
125 1
        return $this->queryParams;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     *
131
     * @param array $query Array of query string arguments, typically from
132
     *     $_GET.
133
     * @return self
134
     */
135 18
    public function withQueryParams(array $query)
136
    {
137 18
        return $this->_clone('queryParams', $query);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     *
143
     * @return array An array tree of UploadedFileInterface instances; an empty
144
     *     array MUST be returned if no data is present.
145
     */
146 1
    public function getUploadedFiles()
147
    {
148 1
        $this->uploadedFiles;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     *
154
     * @param array An array tree of UploadedFileInterface instances.
0 ignored issues
show
Bug introduced by
The type Fyuze\Http\Message\An was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
155
     * @return self
156
     * @throws \InvalidArgumentException if an invalid structure is provided.
157
     */
158 18
    public function withUploadedFiles(array $uploadedFiles)
159
    {
160 18
        return $this->_clone('uploadedFiles', $uploadedFiles);
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     *
166
     * @return null|array|object The deserialized body parameters, if any.
167
     *     These will typically be an array or object.
168
     */
169 2
    public function getParsedBody()
170
    {
171 2
        return $this->parsedBody;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     *
177
     * @param null|array|object $data The deserialized body data. This will
178
     *     typically be in an array or object.
179
     * @return self
180
     * @throws \InvalidArgumentException if an unsupported argument type is
181
     *     provided.
182
     */
183 18
    public function withParsedBody($data)
184
    {
185 18
        return $this->_clone('parsedBody', $data);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     *
191
     * @return array Attributes derived from the request.
192
     */
193 1
    public function getAttributes()
194
    {
195 1
        return $this->attributes;
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     *
201
     * @see getAttributes()
202
     * @param string $name The attribute name.
203
     * @param mixed $default Default value to return if the attribute does not exist.
204
     * @return mixed
205
     */
206 1
    public function getAttribute($name, $default = null)
207
    {
208 1
        if (array_key_exists($name, $this->attributes) === false) {
209 1
            return $default;
210
        }
211
212 1
        return $this->attributes[$name];
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     *
218
     * @see getAttributes()
219
     * @param string $name The attribute name.
220
     * @param mixed $value The value of the attribute.
221
     * @return self
222
     */
223 1
    public function withAttribute($name, $value)
224
    {
225 1
        $instance = clone($this);
226 1
        $instance->attributes[$name] = $value;
227 1
        return $instance;
228
    }
229
230
    /**
231
     * {@inheritdoc}
232
     *
233
     * @see getAttributes()
234
     * @param string $name The attribute name.
235
     * @return self
236
     */
237 1
    public function withoutAttribute($name)
238
    {
239 1
        if (array_key_exists($name, $this->attributes) === false) {
240 1
            return clone $this;
241
        }
242
243 1
        $instance = clone $this;
244 1
        unset($instance->attributes[$name]);
245 1
        return $instance;
246
    }
247
}
248