RequestBuilder   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 0 Features 0
Metric Value
wmc 18
c 11
b 0
f 0
lcom 1
cbo 5
dl 0
loc 146
ccs 55
cts 55
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
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
A create() 0 14 2
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 18
    public function create($params)
24
    {
25 18
        $params += $this->defaults;
26
27 18
        $instance = $this->buildInstance();
28 18
        $this->applyHeaders($instance, $params['server']);
29 18
        $this->applyParams($instance, $params);
30
31 18
        if ($instance->getMethod() !== 'get') {
32 17
            $this->applyContentParsers($instance);
33
        }
34
35 17
        return $instance;
36
    }
37
38
39
    /**
40
     * @param string $type
41
     * @param callback $parser
42
     */
43 8
    public function addContentParser($type, $parser)
44
    {
45 8
        $this->parsers[$type] = $parser;
46 8
    }
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 10
    protected function applyContentParsers($instance)
62
    {
63 10
        $parameters = [];
64
65 10
        $header = $instance->getContentTypeHeader();
66
67 10
        if ($header === null) {
68 2
            return;
69
        }
70
71 8
        foreach ($this->parsers as $type => $parser) {
72 7
            if ($header->match($type)) {
73 7
                $parameters += $this->alterParameters($parser, $type, $header, $instance);
74
            }
75
        }
76
77 7
        $instance->setParameters($parameters, true);
78 7
    }
79
80
    /**
81
     * @param callable $parser
82
     * @param string $type
83
     * @param Headers\ContentType $header
84
     * @param Request $instance
85
     */
86 3
    private function alterParameters($parser, $type, $header, $instance)
87
    {
88 3
        $result = call_user_func($parser, $header, $instance);
89
90 3
        if (false === is_array($result)) {
91 1
            $message = "Parser for '$type' did not return a 'name => value' array of parameters";
92 1
            trigger_error($message, \E_USER_WARNING);
93
        }
94
95 2
        return $result;
96
    }
97
98
99
    /**
100
     * @param Request $instance
101
     * @param array[] $params
102
     */
103 3
    protected function applyParams($instance, $params)
104
    {
105 3
        $instance->setParameters($params['get']);
106 3
        $instance->setParameters($params['post']);
107 3
        $instance->setUploadedFiles($params['files']);
108
109 3
        $this->applyWebContext($instance, $params['server']);
110
111 3
        foreach ($params['cookies'] as $name => $value) {
112 1
            $instance->addCookie($name, $value);
113
        }
114 3
    }
115
116
117
    /**
118
     * @param Request $instance
119
     * @param array $params
120
     */
121 1
    protected function applyWebContext($instance, $params)
122
    {
123 1
        if (isset($params['REQUEST_METHOD'])) {
124 1
            $instance->setMethod($params['REQUEST_METHOD']);
125
        }
126 1
        if (isset($params['REMOTE_ADDR'])) {
127 1
            $instance->setAddress($params['REMOTE_ADDR']);
128
        }
129 1
    }
130
131
132
    /**
133
     * @param Request $instance
134
     * @param array $params
135
     */
136 2
    protected function applyHeaders($instance, $params)
137
    {
138 2
        if (array_key_exists('HTTP_ACCEPT', $params)) {
139 1
            $header = new Headers\Accept($params['HTTP_ACCEPT']);
140 1
            $header->prepare();
141 1
            $instance->setAcceptHeader($header);
142
        }
143
144 2
        if (array_key_exists('CONTENT_TYPE', $params)) {
145 1
            $header = new Headers\ContentType($params['CONTENT_TYPE']);
146 1
            $header->prepare();
147 1
            $instance->setContentTypeHeader($header);
148
        }
149 2
    }
150
}
151