Completed
Push — master ( 651f7b...929e4f )
by Anton
01:01 queued 40s
created

RequestFactory::fromGlobals()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 5
dl 0
loc 25
ccs 14
cts 14
cp 1
crap 5
rs 9.2088
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Request;
12
13
use Bluz\Http\RequestMethod;
14
use Bluz\Proxy\Request;
15
use Zend\Diactoros\ServerRequest;
16
use Zend\Diactoros\ServerRequestFactory;
17
18
/**
19
 * Request Factory
20
 *
21
 * @package  Bluz\Request
22
 * @author   Anton Shevchuk
23
 */
24
class RequestFactory extends ServerRequestFactory
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 604
    public static function fromGlobals(
30
        array $server = null,
31
        array $query = null,
32
        array $body = null,
33
        array $cookies = null,
34
        array $files = null
35
    ) : ServerRequest {
36 604
37 604
        $request = parent::fromGlobals($server, $query, $body, $cookies, $files);
38 604
39 604
        $contentType = current($request->getHeader('Content-Type'));
40 604
41 604
        // support header like "application/json" and "application/json; charset=utf-8"
42 604
        if (false !== $contentType && false !== stripos($contentType, Request::TYPE_JSON)) {
43 604
            $input = file_get_contents('php://input');
44 604
            $data = (array) json_decode($input);
45 604
        } elseif ($request->getMethod() === RequestMethod::POST) {
46
            $data = $_POST;
47
        } else {
48 604
            $input = file_get_contents('php://input');
49
            parse_str($input, $data);
50 604
        }
51
52
        return $request->withParsedBody($body ?: $data);
53 604
    }
54
}
55