Completed
Pull Request — master (#8)
by Mārtiņš
03:18
created

RequestBuilder   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 98.46%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 18
c 10
b 0
f 0
lcom 1
cbo 5
dl 0
loc 145
ccs 64
cts 65
cp 0.9846
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 14 2
A addContentParser() 0 4 1
A buildInstance() 0 7 1
A applyContentParsers() 0 18 4
A alterParameters() 0 11 2
A applyParams() 0 12 2
A applyWebContext() 0 9 3
A applyHeaders() 0 14 3
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 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 $type => $parser) {
72 4
            if ($header->contains($type)) {
73 4
                $parameters += $this->alterParameters($parser, $type, $header);
74 3
            }
75 4
        }
76
77 4
        $instance->setParameters($parameters, true);
78 4
    }
79
80
    /**
81
     * @param callable $parser
82
     * @param string $type
83
     * @param Headers\ContentType $header
84
     */
85 3
    private function alterParameters($parser, $type, $header)
86
    {
87 3
        $result = call_user_func($parser, $header);
88
89 3
        if (false === is_array($result)) {
90 1
            $message = "Parser for '$type' did not return a 'name => value' array of parameters";
91 1
            trigger_error($message, \E_USER_WARNING);
92
        }
93
94 2
        return $result;
95
    }
96
97
98
    /**
99
     * @param Request $instance
100
     * @param array[] $params
101
     */
102 3
    protected function applyParams($instance, $params)
103
    {
104 3
        $instance->setParameters($params['get']);
105 3
        $instance->setParameters($params['post']);
106 3
        $instance->setUploadedFiles($params['files']);
107
108 3
        $this->applyWebContext($instance, $params['server']);
109
110 3
        foreach ($params['cookies'] as $name => $value) {
111 1
            $instance->addCookie($name, $value);
112 3
        }
113 3
    }
114
115
116
    /**
117
     * @param Request $instance
118
     * @param array[] $params
119
     */
120 1
    protected function applyWebContext($instance, $params)
121
    {
122 1
        if (isset($params['REQUEST_METHOD'])) {
123 1
            $instance->setMethod($params['REQUEST_METHOD']);
124 1
        }
125 1
        if (isset($params['REMOTE_ADDR'])) {
126 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...
127 1
        }
128 1
    }
129
130
131
    /**
132
     * @param Request $instance
133
     * @param array $params
134
     */
135 2
    protected function applyHeaders($instance, $params)
136
    {
137 2
        if (array_key_exists('HTTP_ACCEPT', $params)) {
138 1
            $header = new Headers\Accept($params['HTTP_ACCEPT']);
139 1
            $header->prepare();
140 1
            $instance->setAcceptHeader($header);
141 1
        }
142
143 2
        if (array_key_exists('CONTENT_TYPE', $params)) {
144 1
            $header = new Headers\ContentType($params['CONTENT_TYPE']);
145 1
            $header->prepare();
146 1
            $instance->setContentTypeHeader($header);
147 1
        }
148 2
    }
149
}
150