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

RequestFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 14
cts 15
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromGlobals() 0 25 5
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