Completed
Push — master ( e40312...157dcf )
by Pierre
03:05
created

Request::getCliParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 9
cp 0.6667
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3.3332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nymfonya\Component\Http;
6
7
use Nymfonya\Component\Http\Interfaces\HeadersInterface;
8
use Nymfonya\Component\Http\Interfaces\RequestInterface;
9
use Nymfonya\Component\Http\Session;
10
11
class Request extends Session implements RequestInterface
12
{
13
14
    protected $server;
15
    protected $method;
16
    protected $contentType;
17
    protected $isCli;
18
    protected $params;
19
20
    /**
21
     * headers list
22
     *
23
     * @var Headers
24
     */
25
    protected $headerManager;
26
27
    /**
28
     * instanciate
29
     *
30
     */
31 41
    public function __construct()
32
    {
33 41
        $sapiName = php_sapi_name();
34 41
        $this->setIsCli(
35 41
            $sapiName == self::_CLI
36 41
                || $sapiName == self::_CLID
37
        );
38 41
        $this->headerManager = new Headers();
39 41
        $this->server = $_SERVER;
40 41
        $this->method = $this->getMethod();
41 41
        $this->setContentType(self::APPLICATION_JSON);
42 41
        $this->setParams();
43 41
        parent::__construct();
44 41
        $this->setHeaders();
45
    }
46
47
    /**
48
     * returns header manager
49
     *
50
     * @return Headers
51
     */
52 1
    public function getHeaderManager(): HeadersInterface
53
    {
54 1
        return $this->headerManager;
55
    }
56
57
    /**
58
     * returns http method
59
     *
60
     * @return string
61
     */
62 41
    public function getMethod(): string
63
    {
64 41
        return ($this->isCli)
65 41
            ? self::METHOD_TRACE
66 41
            : $this->getServer(self::REQUEST_METHOD);
67
    }
68
69
    /**
70
     * returns http param for a given key
71
     *
72
     * @return array
73
     */
74 3
    public function getParams(): array
75
    {
76 3
        return $this->params;
77
    }
78
79
    /**
80
     * returns http param for a given key
81
     *
82
     * @return string
83
     */
84 1
    public function getParam(string $key): string
85
    {
86 1
        return isset($this->params[$key])
87 1
            ? $this->params[$key]
88 1
            : '';
89
    }
90
91
    /**
92
     * returns active route
93
     *
94
     * @return string
95
     */
96 1
    public function getRoute(): string
97
    {
98 1
        return $this->getServer(self::SCRIPT_URL);
99
    }
100
101
    /**
102
     * return php script filename
103
     *
104
     * @return string
105
     */
106 3
    public function getFilename(): string
107
    {
108 3
        return $this->getServer(self::SCRIPT_FILENAME);
109
    }
110
111
    /**
112
     * return request uri
113
     *
114
     * @return string
115
     */
116 1
    public function getUri(): string
117
    {
118 1
        return ($this->isCli())
119 1
            ? $this->getArgs()
120 1
            : $this->getServer(self::REQUEST_URI);
121
    }
122
123
    /**
124
     * return request host
125
     *
126
     * @return string
127
     */
128 1
    public function getHost(): string
129
    {
130 1
        return $this->getServer(self::HTTP_HOST);
131
    }
132
133
    /**
134
     * return request client ip
135
     *
136
     * @return string
137
     */
138 1
    public function getIp(): string
139
    {
140 1
        return $this->getServer(self::REMOTE_ADDR);
141
    }
142
143
    /**
144
     * return request content type
145
     *
146
     * @return string
147
     */
148 1
    public function getContentType(): string
149
    {
150 1
        return $this->contentType;
151
    }
152
153
    /**
154
     * return request accept encoding
155
     *
156
     * @return string
157
     */
158 1
    public function getAcceptEncoding(): string
159
    {
160 1
        $headers = $this->getHeaders();
161 1
        return isset($headers[HeadersInterface::ACCEPT_ENCODING])
162
            ? $headers[HeadersInterface::ACCEPT_ENCODING]
163 1
            : '';
164
    }
165
166
    /**
167
     * return request headers
168
     *
169
     * @return array
170
     */
171 1
    public function getHeaders(): array
172
    {
173 1
        return $this->headerManager->get();
174
    }
175
176
    /**
177
     * return request headers
178
     *
179
     * @return RequestInterface
180
     */
181 1
    protected function setHeaders(): RequestInterface
182
    {
183 1
        $this->headerManager->addMany(
184 1
            $this->isCli ? [] : getallheaders()
185
        );
186 1
        return $this;
187
    }
188
189
    /**
190
     * build uri from cli args
191
     *
192
     * @return string
193
     */
194 1
    protected function getArgs(): string
195
    {
196 1
        return (true === isset($this->server[self::_ARGV][1]))
197
            ? (string) $this->server[self::_ARGV][1]
198 1
            : '';
199
    }
200
201
    /**
202
     * return server value for a given key
203
     *
204
     * @param string $key
205
     * @return string
206
     */
207 1
    protected function getServer(string $key): string
208
    {
209 1
        return (true === isset($this->server[$key]))
210 1
            ? (string) $this->server[$key]
211 1
            : '';
212
    }
213
214
    /**
215
     * isJsonAppContentType
216
     *
217
     * @return bool
218
     */
219 1
    protected function isJsonContentType(): bool
220
    {
221 1
        return strpos(
222 1
            strtolower($this->contentType),
223 1
            self::APPLICATION_JSON
224 1
        ) !== false;
225
    }
226
227
    /**
228
     * getInput
229
     *
230
     * @return array
231
     */
232 2
    protected function getInput(): array
233
    {
234 2
        $input = [];
235 2
        $inputContent = file_get_contents('php://input');
236 2
        if ($this->isJsonContentType()) {
237 2
            $input = json_decode($inputContent, true);
238 2
            if (json_last_error() !== 0) {
239 2
                $input = [];
240
            }
241
        } else {
242 1
            parse_str($inputContent, $input);
243
        }
244 2
        return $input;
245
    }
246
247
    /**
248
     * set method
249
     * essentially for testing purposes
250
     *
251
     * @param string $method
252
     * @return RequestInterface
253
     */
254 2
    protected function setMethod(string $method): RequestInterface
255
    {
256 2
        $this->method = $method;
257 2
        return $this;
258
    }
259
260
    /**
261
     * set true if we are running from cli
262
     * essentially for testing purposes
263
     *
264
     * @param boolean $isCli
265
     * @return RequestInterface
266
     */
267 1
    protected function setIsCli(bool $isCli): RequestInterface
268
    {
269 1
        $this->isCli = $isCli;
270 1
        if (false === $this->isCli()) {
271 1
            $this->startSession($this->getFilename());
272
        }
273 1
        return $this;
274
    }
275
276
    /**
277
     * return true id sapi mode
278
     * essentially for testing purposes
279
     *
280
     * @return boolean
281
     */
282 41
    public function isCli(): bool
283
    {
284 41
        return $this->isCli;
285
    }
286
287
    /**
288
     * set content type
289
     *
290
     * @param string $contentType
291
     * @return RequestInterface
292
     */
293 2
    protected function setContentType(string $contentType = ''): RequestInterface
294
    {
295 2
        $this->contentType = (empty($contentType))
296 1
            ? $this->getServer(HeadersInterface::CONTENT_TYPE)
297 2
            : $contentType;
298 2
        return $this;
299
    }
300
301
    /**
302
     * get params in cli mode
303
     *
304
     * @return array
305
     */
306 3
    protected function getCliParams(): array
307
    {
308 3
        $params = $this->getInput();
309 3
        if ($this->isCli) {
310 3
            $queryString = parse_url($this->getArgs(), PHP_URL_QUERY);
311 3
            if (is_null($queryString)) {
312 3
                return [];
313
            }
314
            parse_str($queryString, $queryParams);
315
            $params = array_merge($params, $queryParams);
316
        }
317
        return $params;
318
    }
319
320
    /**
321
     * set an entry in params for key value
322
     *
323
     * @param string $key
324
     * @param string $value
325
     * @return RequestInterface
326
     */
327 1
    protected function setParam(string $key, string $value): RequestInterface
328
    {
329 1
        $this->params[$key] = $value;
330 1
        return $this;
331
    }
332
333
    /**
334
     * set http params
335
     *
336
     * @param array $params
337
     * @return RequestInterface
338
     */
339 1
    protected function setParams(array $params = []): RequestInterface
340
    {
341 1
        if (!empty($params)) {
342 1
            $this->params = $params;
343 1
            return $this;
344
        }
345 1
        if ($this->method === self::METHOD_GET) {
346 1
            $this->params = $_GET;
347 1
        } elseif ($this->method === self::METHOD_POST) {
348 1
            $this->params = ($this->isJsonContentType())
349 1
                ? $this->getInput()
350 1
                : $_POST;
351 1
        } elseif ($this->method === self::METHOD_TRACE) {
352 1
            $this->params = $this->getCliParams();
353
        } else {
354 1
            $this->params = $this->getInput();
355
        }
356 1
        return $this;
357
    }
358
}
359