Passed
Push — master ( 478a0c...9f2f7c )
by Zlatin
01:26
created

Uri::withUserInfo()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.243

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 15
ccs 7
cts 10
cp 0.7
crap 3.243
rs 9.4285
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
    /**
53
     * @param UriInterface|string $uri
54
     * @return UriInterface
55
     */
56 26
    public function __construct($uri = '')
57
    {
58
59 26
        if ($uri instanceof UriInterface) {
60 12
            return $uri;
61
        }
62
63 26
        $components = array_merge(array(
64 26
            'scheme' => '',
65 13
            'host' => '',
66 13
            'port' => '',
67 13
            'user' => '',
68 13
            'pass' => '',
69 13
            'path' => '',
70 13
            'query' => '',
71
            'fragment' => ''
72 26
            ), parse_url($uri));
73
74 26
        $this->scheme = $components['scheme'];
75 26
        $this->host = $components['host'];
76 26
        $this->port = $this->isStandartPort($components['port']);
77 26
        $this->path = $components['path'];
78 26
        $this->query = $components['query'];
79 26
        $this->fragment = $components['fragment'];
80 26
        $this->userInfo = $components['user'];
81
82 26
        if (isset($components['pass'])) {
83 26
            $this->userInfo .= ":{$components['pass']}";
84 13
        }
85 26
    }
86
87
    /**
88
     * @param int $port
89
     * @return int|null
90
     */
91 26
    public function isStandartPort($port)
92
    {
93 26
        if (!in_array($port, [self::$schemes['http'], self::$schemes['https']])) {
94 26
            return $port;
95
        }
96
        return null;
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function __toString()
103
    {
104
        $uri = $this->scheme;
105
106
        if ($this->userInfo !== null) {
107
            $uri .= "//{$this->userInfo}";
108
        }
109
110
        if ($this->port !== null && !in_array($this->port, [self::$schemes['http'], self::$schemes['https']])) {
111
            $uri .= ":{$this->port}";
112
        }
113
114
        $uri .= $this->path;
115
116
        if ($this->query !== null) {
117
            $uri .= "?{$this->query}";
118
        }
119
120
        if ($this->fragment !== null) {
121
            $uri .= "#{$this->fragment}";
122
        }
123
124
        return $uri;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getAuthority()
131
    {
132
        return ($this->userInfo ?: $this->userInfo) . $this->host . ($this->port ?: ":{$this->port}");
133
    }
134
135
    /**
136
     * @return string
137
     */
138 2
    public function getFragment()
139
    {
140 2
        return $this->fragment;
141
    }
142
143
    /**
144
     * @return string
145
     */
146 4
    public function getHost()
147
    {
148 4
        return $this->host;
149
    }
150
151
    /**
152
     * @return string
153
     */
154 4
    public function getPath()
155
    {
156 4
        return $this->path;
157
    }
158
159
    /**
160
     * @return null|int
161
     */
162 4
    public function getPort()
163
    {
164 4
        return $this->port;
165
    }
166
167
    /**
168
     * @return string
169
     */
170 4
    public function getQuery()
171
    {
172 4
        return $this->query;
173
    }
174
175
    /**
176
     * @return string
177
     */
178 4
    public function getScheme()
179
    {
180 4
        return $this->scheme;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 4
    public function getUserInfo()
187
    {
188 4
        return $this->userInfo;
189
    }
190
191
    /**
192
     * @param string $fragment
193
     * @return self
194
     */
195
    public function withFragment($fragment)
196
    {
197
        if ($fragment === $this->fragment) {
198
            return $this;
199
        }
200
201
        $clone = clone $this;
202
        $clone->fragment = $fragment;
203
204
        return $clone;
205
    }
206
207
    /**
208
     * @param string $host
209
     * @return self
210
     */
211 2
    public function withHost($host)
212
    {
213 2
        if ($host === $this->host) {
214
            return $this;
215
        }
216
217 2
        $clone = clone $this;
218 2
        $clone->host = $host;
219
220 2
        return $clone;
221
    }
222
223
    /**
224
     * @param string $path
225
     * @return self
226
     */
227 2
    public function withPath($path)
228
    {
229 2
        if ($path === $this->path) {
230
            return $this;
231
        }
232
233 2
        $clone = clone $this;
234 2
        $clone->path = $path;
235
236 2
        return $clone;
237
    }
238
239
    /**
240
     * @param int $port
241
     * @return self
242
     */
243 2
    public function withPort($port)
244
    {
245 2
        if ($port === $this->port) {
246
            return $this;
247
        }
248
249 2
        $clone = clone $this;
250 2
        $clone->port = $port;
251
252 2
        return $clone;
253
    }
254
255
    /**
256
     * @param string  $query
257
     * @return self
258
     */
259 2
    public function withQuery($query)
260
    {
261 2
        if ($query === $this->query) {
262
            return $this;
263
        }
264
265 2
        $clone = clone $this;
266 2
        $clone->query = $query;
267
268 2
        return $clone;
269
    }
270
271
    /**
272
     * @param string $scheme
273
     * @return self
274
     */
275 2
    public function withScheme($scheme)
276
    {
277 2
        if ($scheme === $this->scheme) {
278
            return $this;
279
        }
280
281 2
        $clone = clone $this;
282 2
        $clone->scheme = $scheme;
283
284 2
        return $clone;
285
    }
286
287
    /**
288
     * @param string $user
289
     * @param string|null $password
290
     * @return self
291
     */
292 2
    public function withUserInfo($user, $password = null)
293
    {
294 2
        $userInfo = $user;
295 2
        if (null !== $password) {
296
            $userInfo .= ":{$password}";
297
        }
298
299 2
        if ($userInfo === $this->userInfo) {
300
            return $this;
301
        }
302
303 2
        $clone = clone $this;
304 2
        $clone->userInfo = $userInfo;
305
306 2
        return $clone;
307
    }
308
}
309