Completed
Push — master ( 8533e0...93a57d )
by Zlatin
14:50
created

Uri   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Test Coverage

Coverage 69.16%

Importance

Changes 0
Metric Value
dl 0
loc 289
ccs 74
cts 107
cp 0.6916
rs 8.6
c 0
b 0
f 0
wmc 37

17 Methods

Rating   Name   Duplication   Size   Complexity  
A withFragment() 0 10 2
A withQuery() 0 10 2
A withPort() 0 10 2
A getPath() 0 3 1
A getUserInfo() 0 3 1
A getQuery() 0 3 1
A withUserInfo() 0 15 3
A getScheme() 0 3 1
A getFragment() 0 3 1
B __toString() 0 23 6
A withScheme() 0 10 2
A getHost() 0 3 1
A withHost() 0 10 2
A getPort() 0 3 1
A withPath() 0 10 2
A getAuthority() 0 3 3
B __construct() 0 27 6
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 $scheme;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $userInfo;
26
27
    /**
28
     * @var string|null
29
     */
30
    private $host;
31
32
    /**
33
     * @var int|null
34
     */
35
    private $port;
36
37
    /**
38
     * @var string
39
     */
40
    private $path = '';
41
42
    /**
43
     * @var string|null
44
     */
45
    private $query;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $fragment;
51
52 26
    /**
53
     * @param string|UriInterface $uri
54
     * @return UriInterface
55 26
     * @throws \InvalidArgumentException
56 12
     */
57
    public function __construct($uri = '')
58
    {
59 26
60
        if ($uri instanceof UriInterface) {
61
            return $uri;
62
        }
63 26
64 26
        if (!is_string($uri)) {
0 ignored issues
show
introduced by
The condition ! is_string($uri) can never be true.
Loading history...
65 14
            throw new \InvalidArgumentException('Argument must be a string.');
66 7
        }
67 26
68 14
        $components = parse_url($uri);
69 7
70 26
        $this->scheme = $components['scheme'];
71 14
        $this->host = $components['host'];
72 7
73 26
        $this->path = $components['path'];
74 26
        $this->query = $components['query'];
75 13
        $this->fragment = $components['fragment'];
76 26
        $this->userInfo = $components['user'];
77 14
78 7
        if (isset($components['pass'])) {
79 26
            $this->userInfo .= ":{$components['pass']}";
80 14
        }
81 7
82 26
        if (isset($components['port']) && !in_array($components['port'], [self::$schemes['http'], self::$schemes['https']])) {
83 14
            $this->port = $components['port'];
84 7
        }
85 26
    }
86 14
87 7
    /**
88 26
     * @return string
89
     */
90
    public function __toString()
91
    {
92
        $uri = $this->scheme;
93
94
        if ($this->userInfo !== null) {
95
            $uri .= "//{$this->userInfo}";
96
        }
97
98
        if ($this->port !== null && !in_array($this->port, [self::$schemes['http'], self::$schemes['https']])) {
99
            $uri .= ":{$this->port}";
100
        }
101
102
        $uri .= $this->path;
103
104
        if ($this->query !== null) {
105
            $uri .= "?{$this->query}";
106
        }
107
108
        if ($this->fragment !== null) {
109
            $uri .= "#{$this->fragment}";
110
        }
111
112
        return $uri;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getAuthority()
119
    {
120
        return ($this->userInfo ?: $this->userInfo) . $this->host . ($this->port ?: ":{$this->port}");
121
    }
122
123
    /**
124
     * @return string
125
     */
126
    public function getFragment()
127
    {
128
        return $this->fragment;
129 2
    }
130
131 2
    /**
132
     * @return string
133
     */
134
    public function getHost()
135
    {
136
        return $this->host;
137 4
    }
138
139 4
    /**
140
     * @return string
141
     */
142
    public function getPath()
143
    {
144
        return $this->path;
145 4
    }
146
147 4
    /**
148
     * @return null|int
149
     */
150
    public function getPort()
151
    {
152
        return $this->port;
153 4
    }
154
155 4
    /**
156
     * @return string
157
     */
158
    public function getQuery()
159
    {
160
        return $this->query;
161 4
    }
162
163 4
    /**
164
     * @return string
165
     */
166
    public function getScheme()
167
    {
168
        return $this->scheme;
169 4
    }
170
171 4
    /**
172
     * @return string
173
     */
174
    public function getUserInfo()
175
    {
176
        return $this->userInfo;
177 4
    }
178
179 4
    /**
180
     * @param string $fragment
181
     * @return \DevOp\Core\Http\Uri|$this
182
     */
183
    public function withFragment($fragment)
184
    {
185
        if ($fragment === $this->fragment) {
186
            return $this;
187
        }
188
189
        $clone = clone $this;
190
        $clone->fragment = $fragment;
191
192
        return $clone;
193
    }
194
195
    /**
196
     * @param string $host
197
     * @return \DevOp\Core\Http\Uri|$this
198
     */
199
    public function withHost($host)
200
    {
201
        if ($host === $this->host) {
202 2
            return $this;
203
        }
204 2
205
        $clone = clone $this;
206
        $clone->host = $host;
207
208 2
        return $clone;
209 2
    }
210
211 2
    /**
212
     * @param string $path
213
     * @return \DevOp\Core\Http\Uri|$this
214
     */
215
    public function withPath($path)
216
    {
217
        if ($path === $this->path) {
218 2
            return $this;
219
        }
220 2
221
        $clone = clone $this;
222
        $clone->path = $path;
223
224 2
        return $clone;
225 2
    }
226
227 2
    /**
228
     * @param int $port
229
     * @return \DevOp\Core\Http\Uri|$this
230
     */
231
    public function withPort($port)
232
    {
233
        if ($port === $this->port) {
234 2
            return $this;
235
        }
236 2
237
        $clone = clone $this;
238
        $clone->port = $port;
239
240 2
        return $clone;
241 2
    }
242
243 2
    /**
244
     * @param string  $query
245
     * @return \DevOp\Core\Http\Uri|$this
246
     */
247
    public function withQuery($query)
248
    {
249
        if ($query === $this->query) {
250 2
            return $this;
251
        }
252 2
253
        $clone = clone $this;
254
        $clone->query = $query;
255
256 2
        return $clone;
257 2
    }
258
259 2
    /**
260
     * @param string $scheme
261
     * @return \DevOp\Core\Http\Uri|$this
262
     */
263
    public function withScheme($scheme)
264
    {
265
        if ($scheme === $this->scheme) {
266 2
            return $this;
267
        }
268 2
269
        $clone = clone $this;
270
        $clone->scheme = $scheme;
271
272 2
        return $clone;
273 2
    }
274
275 2
    /**
276
     * @param string $user
277
     * @param string|null $password
278
     * @return \DevOp\Core\Http\Uri|$this
279
     */
280
    public function withUserInfo($user, $password = null)
281
    {
282
        $userInfo = $user;
283 2
        if (null !== $password) {
284
            $userInfo .= ":{$password}";
285 2
        }
286 2
287
        if ($userInfo === $this->userInfo) {
288
            return $this;
289
        }
290 2
291
        $clone = clone $this;
292
        $clone->userInfo = $userInfo;
293
294 2
        return $clone;
295 2
    }
296
}
297