Passed
Push — master ( d0ebac...b1c1f4 )
by Zlatin
02:16 queued 55s
created

Uri::createFromGlobals()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 15.3773

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 24
nop 0
dl 0
loc 31
ccs 12
cts 21
cp 0.5714
crap 15.3773
rs 4.909
c 0
b 0
f 0
1
<?php
2
namespace DevOp\Core\Http;
3
4
use Psr\Http\Message\UriInterface;
5
6
class Uri implements UriInterface
7
{
8
9
    /**
10
     * @var array
11
     */
12
    private static $schemes = array(
13
        'http' => 80,
14
        'https' => 443
15
    );
16
17
    /**
18
     * @var string|null
19
     */
20
    private $fragment;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $host;
26
27
    /**
28
     * @var string|null
29
     */
30
    private $path;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $port;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $query;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $scheme;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $userInfo;
51
52
    /**
53
     * @return \DevOp\Core\Http\Uri
54
     */
55 6
    public static function createFromGlobals()
56
    {
57
        
58 6
        $uri = new Uri();
59
        
60 6
        $scheme = 'http';
61 6
        if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') {
62
            $scheme .= 's';
63
        }
64
        
65 6
        $uri->withScheme($scheme);
66
        
67 6
        if (isset($_SERVER['REMOTE_PORT']) && (!isset(self::$schemes['http']) && !isset(self::$schemes['https']))) {
68
            $uri->withPort($_SERVER['REMOTE_PORT']);
69
        }
70
        
71 6
        if (isset($_SERVER['SERVER_NAME'])) {
72
            $host = $_SERVER['SERVER_NAME'];
73 6
        } else if (isset($_SERVER['SERVER_ADDR'])) {
74
            $host = $_SERVER['SERVER_ADDR'];
75
        } else {
76 6
            $host = '127.0.0.1';
77
        }
78
        
79 6
        $uri->withHost($host);
80
        
81 6
        if (isset($_SERVER['QUERY_STRING'])) {
82
            $uri->withQuery(ltrim($_SERVER['QUERY_STRING'], '?'));
83
        }
84
        
85 6
        return $uri;
86
    }
87
    
88
    /**
89
     * @return string
90
     */
91
    public function __toString()
92
    {
93
        $uri = $this->scheme;
94
        
95
        if ($this->userInfo !== null) {
96
            $uri .= "//{$this->userInfo}";
97
        }
98
        
99
        if ($this->port !== null && !in_array($this->port, [self::$schemes['http'], self::$schemes['https']])) {
100
            $uri .= ":{$this->port}";
101
        }
102
        
103
        $uri .= $this->path;
104
        
105
        if ($this->query !== null) {
106
            $uri .= "?{$this->query}";
107
        }
108
        
109
        if ($this->fragment !== null) {
110
            $uri .= "#{$this->fragment}";
111
        }
112
        
113
        return $uri;
114
    }
115
116
    /**
117
     * @return string
118
     */
119
    public function getAuthority()
120
    {
121
        return ($this->userInfo ?: $this->userInfo) . $this->host . ($this->port ?: ":{$this->port}");
122
    }
123
124
    /**
125
     * @return string
126
     */
127 2
    public function getFragment()
128
    {
129 2
        return $this->fragment;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getHost()
136
    {
137
        return $this->host;
138
    }
139
140
    /**
141
     * @return strig
0 ignored issues
show
Bug introduced by
The type DevOp\Core\Http\strig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
142
     */
143
    public function getPath()
144
    {
145
        return $this->path;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->path also could return the type string which is incompatible with the documented return type DevOp\Core\Http\strig.
Loading history...
146
    }
147
148
    /**
149
     * @return int
150
     */
151
    public function getPort()
152
    {
153
        return $this->port;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->port also could return the type string which is incompatible with the documented return type integer.
Loading history...
154
    }
155
156
    /**
157
     * @return strig
158
     */
159
    public function getQuery()
160
    {
161
        return $this->query;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->query also could return the type string which is incompatible with the documented return type DevOp\Core\Http\strig.
Loading history...
162
    }
163
164
    /**
165
     * @return strig
166
     */
167
    public function getScheme()
168
    {
169
        return $this->scheme;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->scheme also could return the type string which is incompatible with the documented return type DevOp\Core\Http\strig.
Loading history...
170
    }
171
172
    /**
173
     * @return strig
174
     */
175
    public function getUserInfo()
176
    {
177
        return $this->userInfo;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->userInfo also could return the type string which is incompatible with the documented return type DevOp\Core\Http\strig.
Loading history...
178
    }
179
180
    /**
181
     * @param string $fragment
182
     * @return \DevOp\Core\Http\Uri|$this
183
     */
184 2
    public function withFragment($fragment)
185
    {
186 2
        if ($fragment === $this->fragment) {
187
            return $this;
188
        }
189
190 2
        $clone = clone $this;
191 2
        $clone->fragment = $fragment;
192
193 2
        return $clone;
194
    }
195
196
    /**
197
     * @param string $host
198
     * @return \DevOp\Core\Http\Uri|$this
199
     */
200 6
    public function withHost($host)
201
    {
202 6
        if ($host === $this->host) {
203
            return $this;
204
        }
205
206 6
        $clone = clone $this;
207 6
        $clone->host = $host;
208
209 6
        return $clone;
210
    }
211
212
    /**
213
     * @param string $path
214
     * @return \DevOp\Core\Http\Uri|$this
215
     */
216
    public function withPath($path)
217
    {
218
        if ($path === $this->path) {
219
            return $this;
220
        }
221
222
        $clone = clone $this;
223
        $clone->path = $path;
224
225
        return $clone;
226
    }
227
228
    /**
229
     * @param int $port
230
     * @return \DevOp\Core\Http\Uri|$this
231
     */
232
    public function withPort($port)
233
    {
234
        if ($port === $this->port) {
0 ignored issues
show
introduced by
The condition $port === $this->port can never be true.
Loading history...
235
            return $this;
236
        }
237
238
        $clone = clone $this;
239
        $clone->port = $port;
240
241
        return $clone;
242
    }
243
244
    /**
245
     * @param string  $query
246
     * @return \DevOp\Core\Http\Uri|$this
247
     */
248
    public function withQuery($query)
249
    {
250
        if ($query === $this->query) {
251
            return $this;
252
        }
253
254
        $clone = clone $this;
255
        $clone->query = $query;
256
257
        return $clone;
258
    }
259
260
    /**
261
     * @param string $scheme
262
     * @return \DevOp\Core\Http\Uri|$this
263
     */
264 6
    public function withScheme($scheme)
265
    {
266 6
        if ($scheme === $this->scheme) {
267
            return $this;
268
        }
269
270 6
        $clone = clone $this;
271 6
        $clone->scheme = $scheme;
272
273 6
        return $clone;
274
    }
275
276
    /**
277
     * @param string $user
278
     * @param string|null $password
279
     * @return \DevOp\Core\Http\Uri|$this
280
     */
281
    public function withUserInfo($user, $password = null)
282
    {
283
        $userInfo = $user;
284
        if (null !== $password) {
285
            $userInfo .= ":{$password}";
286
        }
287
288
        if ($userInfo === $this->userInfo) {
289
            return $this;
290
        }
291
292
        $clone = clone $this;
293
        $clone->userInfo = $userInfo;
294
295
        return $clone;
296
    }
297
}
298