Passed
Push — master ( 20361a...82a2cb )
by Tobias
01:15
created

Url::withHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Dsn\Configuration;
6
7
/**
8
 * A "URL like" DSN string.
9
 *
10
 * Example:
11
 * - memcached://user:[email protected]?weight=50
12
 * - 127.0.0.1:80
13
 * - amqp://127.0.0.1/%2f/messages
14
 *
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
class Url extends Dsn
18
{
19
    use UserPasswordTrait;
20
21
    /**
22
     * @var string
23
     */
24
    private $host;
25
26
    /**
27
     * @var int|null
28
     */
29
    private $port;
30
31
    /**
32
     * @var string|null
33
     */
34
    private $path;
35
36
    public function __construct(?string $scheme, string $host, ?int $port = null, ?string $path = null, array $parameters = [], array $authentication = [])
37
    {
38
        $this->host = $host;
39
        $this->port = $port;
40
        $this->path = $path;
41
        $this->setAuthentication($authentication);
42
        parent::__construct($scheme, $parameters);
43
    }
44
45
    public function getHost(): string
46
    {
47
        return $this->host;
48
    }
49
50
    public function withHost(string $host): self
51
    {
52
        $new = clone $this;
53
        $new->host = $host;
54
55
        return $new;
56
    }
57
58
    public function getPort(): ?int
59
    {
60
        return $this->port;
61
    }
62
63
    public function withPort(?int $port): self
64
    {
65
        $new = clone $this;
66
        $new->port = $port;
67
68
        return $new;
69
    }
70
71
    public function getPath(): ?string
72
    {
73
        return $this->path;
74
    }
75
76
    public function withPath(?string $path): self
77
    {
78
        $new = clone $this;
79
        $new->path = $path;
80
81
        return $new;
82
    }
83
84
    /**
85
     * @var string
86
     */
87
    public function __toString()
88
    {
89
        $parameters = $this->getParameters();
90
        $scheme = $this->getScheme();
91
92
        return
93
            (empty($scheme) ? '' : $scheme.'://').
94
            $this->getUserInfoString().
95
            $this->getHost().
96
            (empty($this->port) ? '' : ':'.$this->port).
97
            ($this->getPath() ?? '').
98
            (empty($parameters) ? '' : '?'.http_build_query($parameters));
99
    }
100
}
101