Completed
Push — master ( 15c2d3...c9166c )
by Mārtiņš
7s
created

Request   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Coupling/Cohesion

Components 6
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 15
Bugs 0 Features 1
Metric Value
wmc 30
c 15
b 0
f 1
lcom 6
cbo 1
dl 0
loc 200
ccs 80
cts 80
cp 1
rs 9.0909

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setParameters() 0 12 3
A getParameter() 0 8 2
A setMethod() 0 7 2
A getMethod() 0 4 1
A setAcceptHeader() 0 4 1
A getAcceptHeader() 0 4 1
A setContentTypeHeader() 0 4 1
A getContentTypeHeader() 0 4 1
A setUploadedFiles() 0 8 2
A getUpload() 0 8 2
A addCookie() 0 4 1
A getCookie() 0 8 2
A resolveUri() 0 9 2
A adjustUriSegments() 0 10 2
A setUri() 0 6 1
A sanitizeUri() 0 9 1
A getUri() 0 4 1
A setAddress() 0 8 2
A getAddress() 0 4 1
1
<?php
2
3
namespace Fracture\Http;
4
5
class Request implements Routable
6
{
7
8
    private $acceptHeader = null;
9
10
    private $contentTypeHeader = null;
11
12
    private $method = null;
13
14
    private $parameters = [];
15
16
    private $files = null;
17
18
    private $cookies = [];
19
20
    private $fileBagBuilder = null;
21
22
    private $address = null;
23
24
    private $uri = null;
25
26
27 6
    public function __construct(FileBagBuilder $fileBagBuilder = null)
28
    {
29 6
        $this->fileBagBuilder = $fileBagBuilder;
30 6
    }
31
32
33 3
    public function setParameters(array $list, $override = false)
34
    {
35 3
        $duplicates = array_intersect_key($list, $this->parameters);
36
37
        // checks of parameters with overlapping names
38 3
        if (false === $override && count($duplicates) > 0) {
39 1
            $message = implode("', '", array_keys($duplicates));
40 1
            $message = "You are trying to override following parameter(s): '$message'";
41 1
            trigger_error($message, \E_USER_WARNING);
42
        }
43 3
        $this->parameters = $list + $this->parameters;
44 3
    }
45
46
47
    /**
48
     * @param string $name
49
     * @return mixed
50
     */
51 3
    public function getParameter($name)
52
    {
53 3
        if (array_key_exists($name, $this->parameters)) {
54 1
            return $this->parameters[$name];
55
        }
56
57 2
        return null;
58
    }
59
60
61 1
    public function setMethod($value)
62
    {
63 1
        $method = strtolower($value);
64 1
        if (in_array($method, ['get', 'post', 'put', 'delete', 'head', 'options', 'trace'])) {
65 1
            $this->method = $method;
66
        }
67 1
    }
68
69
70 1
    public function getMethod()
71
    {
72 1
        return $this->method;
73
    }
74
75
76 1
    public function setAcceptHeader(Headers\Accept  $header)
77
    {
78 1
        $this->acceptHeader = $header;
79 1
    }
80
81
82 1
    public function getAcceptHeader()
83
    {
84 1
        return $this->acceptHeader;
85
    }
86
87
88 1
    public function setContentTypeHeader(Headers\ContentType $header)
89
    {
90 1
        $this->contentTypeHeader = $header;
91 1
    }
92
93
94 1
    public function getContentTypeHeader()
95
    {
96 1
        return $this->contentTypeHeader;
97
    }
98
99
100 2
    public function setUploadedFiles($list)
101
    {
102 2
        if ($this->fileBagBuilder !== null) {
103 1
            $list = $this->fileBagBuilder->create($list);
104
        }
105
106 2
        $this->files = $list;
107 2
    }
108
109
110 2
    public function getUpload($name)
111
    {
112 2
        if (isset($this->files[$name])) {
113 1
            return $this->files[$name];
114
        }
115
116 1
        return null;
117
    }
118
119
120 1
    public function addCookie($name, $value)
121
    {
122 1
        $this->cookies[$name] = $value;
123 1
    }
124
125
126 1
    public function getCookie($name)
127
    {
128 1
        if (array_key_exists($name, $this->cookies)) {
129 1
            return $this->cookies[$name];
130
        }
131
132 1
        return null;
133
    }
134
135
136 19
    protected function resolveUri($uri)
137
    {
138 19
        $parts = explode('/', $uri);
139 19
        $segments = [];
140 19
        foreach ($parts as $element) {
141 19
            $segments = $this->adjustUriSegments($segments, $element);
142
        }
143 19
        return implode('/', $segments);
144
    }
145
146
147
    /**
148
     * Method for handling '../' in URL query
149
     */
150 19
    private function adjustUriSegments($list, $item)
151
    {
152 19
        if ($item === '..') {
153 4
            array_pop($list);
154 4
            return $list;
155
        }
156 18
        array_push($list, $item);
157
158 18
        return $list;
159
    }
160
161
162 19
    public function setUri($uri)
163
    {
164 19
        $uri = $this->sanitizeUri($uri);
165 19
        $uri = $this->resolveUri($uri);
166 19
        $this->uri = '/' . $uri;
167 19
    }
168
169
170 19
    private function sanitizeUri($uri)
171
    {
172 19
        $uri = explode('?', $uri)[0];
173
        // to remove './' at the start of $uri
174 19
        $uri = '/' . $uri;
175 19
        $uri = preg_replace(['#(/)+#', '#/(\./)+#'], '/', $uri);
176 19
        $uri = trim($uri, '/');
177 19
        return $uri;
178
    }
179
180
181 19
    public function getUri()
182
    {
183 19
        return $this->uri;
184
    }
185
186
187
    /**
188
     * @param string $address
189
     */
190 2
    public function setAddress($address)
191
    {
192 2
        if (filter_var($address, FILTER_VALIDATE_IP) === false) {
193 1
            $address = null;
194
        }
195
196 2
        $this->address = $address;
197 2
    }
198
199
200 2
    public function getAddress()
201
    {
202 2
        return $this->address;
203
    }
204
}
205