Passed
Push — master ( d1dcd8...de396c )
by Pierre
02:29
created

Request::setIsCli()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
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
        if ($this->isCli) {
184 1
            $this->headerManager->addMany([]);
185
        } else {
186
            $headers = getallheaders();
187
            $auth = $this->getServer(
188
                HeadersInterface::REDIRECT_AUTHORIZATION
189
            );
190
            if (!empty($auth)) {
191
                $headers[HeadersInterface::AUTHORIZATION] = $auth;
192
            }
193
            $this->headerManager->addMany($headers);
194
        }
195 1
        return $this;
196
    }
197
198
    /**
199
     * build uri from cli args
200
     *
201
     * @return string
202
     */
203 1
    protected function getArgs(): string
204
    {
205 1
        return (true === isset($this->server[self::_ARGV][1]))
206
            ? (string) $this->server[self::_ARGV][1]
207 1
            : '';
208
    }
209
210
    /**
211
     * return server value for a given key
212
     *
213
     * @param string $key
214
     * @return string
215
     */
216 1
    protected function getServer(string $key): string
217
    {
218 1
        return (true === isset($this->server[$key]))
219 1
            ? (string) $this->server[$key]
220 1
            : '';
221
    }
222
223
    /**
224
     * isJsonAppContentType
225
     *
226
     * @return bool
227
     */
228 1
    protected function isJsonContentType(): bool
229
    {
230 1
        return strpos(
231 1
            strtolower($this->contentType),
232 1
            self::APPLICATION_JSON
233 1
        ) !== false;
234
    }
235
236
    /**
237
     * getInput
238
     *
239
     * @return array
240
     */
241 2
    protected function getInput(): array
242
    {
243 2
        $input = [];
244 2
        $inputContent = file_get_contents('php://input');
245 2
        if ($this->isJsonContentType()) {
246 2
            $input = json_decode($inputContent, true);
247 2
            if (json_last_error() !== 0) {
248 2
                $input = [];
249
            }
250
        } else {
251 1
            parse_str($inputContent, $input);
252
        }
253 2
        return $input;
254
    }
255
256
    /**
257
     * set method
258
     * essentially for testing purposes
259
     *
260
     * @param string $method
261
     * @return RequestInterface
262
     */
263 2
    protected function setMethod(string $method): RequestInterface
264
    {
265 2
        $this->method = $method;
266 2
        return $this;
267
    }
268
269
    /**
270
     * set true if we are running from cli
271
     * essentially for testing purposes
272
     *
273
     * @param boolean $isCli
274
     * @return RequestInterface
275
     */
276 1
    protected function setIsCli(bool $isCli): RequestInterface
277
    {
278 1
        $this->isCli = $isCli;
279 1
        if (false === $this->isCli()) {
280 1
            $this->startSession($this->getFilename());
281
        }
282 1
        return $this;
283
    }
284
285
    /**
286
     * return true id sapi mode
287
     * essentially for testing purposes
288
     *
289
     * @return boolean
290
     */
291 41
    public function isCli(): bool
292
    {
293 41
        return $this->isCli;
294
    }
295
296
    /**
297
     * set content type
298
     *
299
     * @param string $contentType
300
     * @return RequestInterface
301
     */
302 2
    protected function setContentType(string $contentType = ''): RequestInterface
303
    {
304 2
        $this->contentType = (empty($contentType))
305 1
            ? $this->getServer(HeadersInterface::CONTENT_TYPE)
306 2
            : $contentType;
307 2
        return $this;
308
    }
309
310
    /**
311
     * get params in cli mode
312
     *
313
     * @return array
314
     */
315 3
    protected function getCliParams(): array
316
    {
317 3
        $params = $this->getInput();
318 3
        if ($this->isCli) {
319 3
            $queryString = parse_url($this->getArgs(), PHP_URL_QUERY);
320 3
            if (is_null($queryString)) {
321 3
                return [];
322
            }
323
            parse_str($queryString, $queryParams);
324
            $params = array_merge($params, $queryParams);
325
        }
326
        return $params;
327
    }
328
329
    /**
330
     * set an entry in params for key value
331
     *
332
     * @param string $key
333
     * @param string $value
334
     * @return RequestInterface
335
     */
336 1
    protected function setParam(string $key, string $value): RequestInterface
337
    {
338 1
        $this->params[$key] = $value;
339 1
        return $this;
340
    }
341
342
    /**
343
     * set http params
344
     *
345
     * @param array $params
346
     * @return RequestInterface
347
     */
348 1
    protected function setParams(array $params = []): RequestInterface
349
    {
350 1
        if (!empty($params)) {
351 1
            $this->params = $params;
352 1
            return $this;
353
        }
354 1
        if ($this->method === self::METHOD_GET) {
355 1
            $this->params = $_GET;
356 1
        } elseif ($this->method === self::METHOD_POST) {
357 1
            $this->params = ($this->isJsonContentType())
358 1
                ? $this->getInput()
359 1
                : $_POST;
360 1
        } elseif ($this->method === self::METHOD_TRACE) {
361 1
            $this->params = $this->getCliParams();
362
        } else {
363 1
            $this->params = $this->getInput();
364
        }
365 1
        return $this;
366
    }
367
}
368