Passed
Push — master ( 1b37b7...612acf )
by Tobias
01:49
created

Url::__toString()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 9
c 1
b 0
f 1
dl 0
loc 12
rs 9.9666
cc 4
nc 8
nop 0
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 getPort(): ?int
51
    {
52
        return $this->port;
53
    }
54
55
    public function getPath(): ?string
56
    {
57
        return $this->path;
58
    }
59
60
    /**
61
     * @var string
62
     */
63
    public function __toString()
64
    {
65
        $parameters = $this->getParameters();
66
        $scheme = $this->getScheme();
67
68
        return
69
            (empty($scheme) ? '' : $scheme.'://').
70
            $this->getUserInfoString().
71
            $this->getHost().
72
            (empty($this->port) ? '' : ':'.$this->port).
73
            ($this->getPath() ?? '').
74
            (empty($parameters) ? '' : '?'.http_build_query($parameters));
75
    }
76
}
77