Completed
Push — master ( b5ed08...526d1c )
by Mārtiņš
8s
created

RequestBuilder::addContentParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Fracture\Http;
4
5
class RequestBuilder
6
{
7
8
    private $defaults = [
9
        'get'    => [],
10
        'post'   => [],
11
        'server' => [],
12
        'files'  => [],
13
        'cookies'=> [],
14
    ];
15
16
    private $parsers = [];
17
18
19
    /**
20
     * @param array[] $params
21
     * @return \Fracture\Routing\Routable
22
     */
23 15
    public function create($params)
24
    {
25 15
        $params += $this->defaults;
26
27 15
        $instance = $this->buildInstance();
28 15
        $this->applyHeaders($instance, $params['server']);
29 15
        $this->applyParams($instance, $params);
30 15
        if ($instance->getMethod() !== 'get') {
31 14
            $this->applyContentParsers($instance);
32 13
        }
33 14
        $instance->prepare();
34
35 14
        return $instance;
36
    }
37
38
39
    /**
40
     * @param string $type
41
     * @param callback $parser
42
     */
43 5
    public function addContentParser($type, $parser)
44
    {
45 5
        $this->parsers[$type] = $parser;
46 5
    }
47
48
49 2
    protected function buildInstance()
50
    {
51 2
        $fileBuilder = new UploadedFileBuilder;
52 2
        $fileBagBuilder = new FileBagBuilder($fileBuilder);
53
54 2
        return new Request($fileBagBuilder);
55
    }
56
57
58
    /**
59
     * @param Request $instance
60
     */
61 7
    protected function applyContentParsers($instance)
62
    {
63 7
        $parameters = [];
64
65 7
        $header = $instance->getContentTypeHeader();
66
67 7
        if ($header === null) {
68 2
            return;
69
        }
70
71 5
        foreach ($this->parsers as $value => $parser) {
72 4
            if ($header->contains($value)) {
73 4
                $parameters += $this->alterParameters($parser, $value, $header);
74 3
            }
75 4
        }
76
77 4
        $instance->setParameters($parameters, true);
78 4
    }
79
80
81 3
    private function alterParameters($parser, $value, $header)
82
    {
83 3
        $result = call_user_func($parser, $header);
84
85 3
        if (false === is_array($result)) {
86 1
            $message = "Parser for '$value' did not return a 'name => value' array of parameters";
87 1
            trigger_error($message, \E_USER_WARNING);
88
        }
89
90 2
        return $result;
91
    }
92
93
94
    /**
95
     * @param Request $instance
96
     * @param array[] $params
97
     */
98 3
    protected function applyParams($instance, $params)
99
    {
100 3
        $instance->setParameters($params['get']);
101 3
        $instance->setParameters($params['post']);
102 3
        $instance->setUploadedFiles($params['files']);
103
104 3
        $this->applyWebContext($instance, $params['server']);
105
106 3
        foreach ($params['cookies'] as $name => $value) {
107 1
            $instance->addCookie(new Cookie($name, $value));
108 3
        }
109 3
    }
110
111
112
    /**
113
     * @param Request $instance
114
     * @param array[] $params
115
     */
116 1
    protected function applyWebContext($instance, $params)
117
    {
118 1
        if (isset($params['REQUEST_METHOD'])) {
119 1
            $instance->setMethod($params['REQUEST_METHOD']);
120 1
        }
121 1
        if (isset($params['REMOTE_ADDR'])) {
122 1
            $instance->setAddress($params['REMOTE_ADDR']);
0 ignored issues
show
Documentation introduced by
$params['REMOTE_ADDR'] is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
123 1
        }
124 1
    }
125
126
127
    /**
128
     * @param Request $instance
129
     * @param array $params
130
     */
131 2
    public function applyHeaders($instance, $params)
132
    {
133 2
        if (array_key_exists('HTTP_ACCEPT', $params)) {
134 1
            $header = new Headers\Accept($params['HTTP_ACCEPT']);
135 1
            $header->prepare();
136 1
            $instance->setAcceptHeader($header);
137 1
        }
138
139 2
        if (array_key_exists('CONTENT_TYPE', $params)) {
140 1
            $header = new Headers\ContentType($params['CONTENT_TYPE']);
141 1
            $header->prepare();
142 1
            $instance->setContentTypeHeader($header);
143 1
        }
144 2
    }
145
}
146