Test Failed
Pull Request — master (#19)
by Flo
04:39
created

Request::getParam()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 9
nc 16
nop 1
1
<?php
2
/**
3
 * Class Request
4
 *
5
 * @package Faulancer\Http
6
 * @author Florian Knapp <[email protected]>
7
 */
8
namespace Faulancer\Http;
9
10
use Faulancer\Exception\InvalidArgumentException;
11
12
/**
13
 * Class Request
14
 */
15
class Request extends AbstractHttp
16
{
17
18
    /**
19
     * The current path string
20
     * @var string
21
     */
22
    protected $uri = '';
23
24
    /**
25
     * The current method
26
     * @var string
27
     */
28
    protected $method = '';
29
30
    /**
31
     * Custom headers
32
     * @var array
33
     */
34
    protected $headers = [];
35
36
    /**
37
     * The current query string
38
     * @var string
39
     */
40
    protected $query = '';
41
42
    /**
43
     * @var string
44
     */
45
    protected $body = '';
46
47
    /**
48
     * Set attributes automatically
49
     *
50
     * @return self
51
     */
52
    public function createFromHeaders()
53
    {
54
        $uri = $_SERVER['REQUEST_URI'];
55
56 View Code Duplication
        if (strpos($_SERVER['REQUEST_URI'], '?') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            $uri = explode('?', $_SERVER['REQUEST_URI']);
58
            $this->setQuery($uri[1]);
59
            $uri = $uri[0];
60
        }
61
62
        $this->setUri($uri);
63
        $this->setMethod($_SERVER['REQUEST_METHOD']);
64
65
        return $this;
66
    }
67
68
    /**
69
     * @param array $headers
70
     */
71
    public function setHeaders(array $headers = [])
72
    {
73
        $this->headers = $headers;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function getHeaders()
80
    {
81
        return $this->headers;
82
    }
83
84
    /**
85
     * Set uri path
86
     *
87
     * @param string $uri
88
     */
89
    public function setUri(string $uri)
90
    {
91 View Code Duplication
        if (strpos($uri, '?') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            $uri = explode('?', $uri);
93
            $this->setQuery($uri[1]);
94
            $uri = $uri[0];
95
        }
96
97
        $this->uri = $uri;
98
    }
99
100
    /**
101
     * Get uri path
102
     *
103
     * @return string
104
     */
105
    public function getUri() :string
106
    {
107
        return $this->uri;
108
    }
109
110
    /**
111
     * Set method
112
     *
113
     * @param string $method
114
     */
115
    public function setMethod(string $method)
116
    {
117
        $this->method = $method;
118
    }
119
120
    /**
121
     * Get method
122
     *
123
     * @return string
124
     */
125
    public function getMethod() :string
126
    {
127
        $serverRequestMethod = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : '';
128
        return empty($this->method) ? $serverRequestMethod : $this->method;
129
    }
130
131
    /**
132
     * Set query string
133
     *
134
     * @param string $query
135
     */
136
    public function setQuery(string $query)
137
    {
138
        $this->query = $query;
139
    }
140
141
    /**
142
     * Get query string
143
     *
144
     * @return string
145
     */
146
    public function getQuery() :string
147
    {
148
        return $this->query;
149
    }
150
151
    /**
152
     * @param array $body
153
     */
154
    public function setBody($body)
155
    {
156
       $this->body = $body;
0 ignored issues
show
Documentation Bug introduced by
It seems like $body of type array is incompatible with the declared type string of property $body.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
157
    }
158
159
    /**
160
     * @return string
161
     */
162
    public function getBody()
163
    {
164
        return $this->body;
165
    }
166
167
    /**
168
     * Determine if it's a post request
169
     *
170
     * @return boolean
171
     */
172
    public function isPost() :bool
173
    {
174
        return $this->getMethod() === 'POST';
175
    }
176
177
    /**
178
     * Determine if it's a get request
179
     *
180
     * @return boolean
181
     */
182
    public function isGet() :bool
183
    {
184
        return $this->getMethod() === 'GET';
185
    }
186
187
    /**
188
     * @param string $key
189
     * @return mixed
190
     * @throws InvalidArgumentException
191
     */
192
    public function getParam(string $key)
193
    {
194
        $post = !empty($_POST) ? $_POST : [];
195
        $get  = !empty($_GET) ? $_GET : [];
196
197
        if (!empty($this->getQuery())) {
198
            parse_str($this->getQuery(), $get);
199
        }
200
201
        $combined = $post + $get;
202
203
        if (!empty($combined[$key])) {
204
            return $combined[$key];
205
        }
206
207
        return null;
208
    }
209
210
    /**
211
     * Return the post data
212
     *
213
     * @return array
214
     */
215
    public function getPostData() :array
216
    {
217
        return empty($_POST) ? [] : $_POST;
218
    }
219
220
    /**
221
     * @param $data
222
     * @return void
223
     */
224
    public function setPostData($data)
225
    {
226
        $_POST = $data;
227
    }
228
229
}