Dsn   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 16
eloc 27
c 1
b 0
f 1
dl 0
loc 109
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getScheme() 0 3 1
A getPassword() 0 3 1
A getParameters() 0 3 1
A withScheme() 0 6 1
A getPort() 0 3 1
A withParameter() 0 6 1
A getPath() 0 3 1
A withoutParameter() 0 6 1
A __toString() 0 8 3
A getParameter() 0 3 2
A getHost() 0 3 1
A getUser() 0 3 1
1
<?php
2
3
namespace Nyholm\Dsn\Configuration;
4
5
/**
6
 * Base DSN object.
7
 *
8
 * Example:
9
 * - null://
10
 * - redis:?host[h1]&host[h2]&host[/foo:]
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class Dsn
15
{
16
    /**
17
     * @var string|null
18
     */
19
    private $scheme;
20
21
    /**
22
     * @var array
23
     */
24
    private $parameters = [];
25
26
    public function __construct(?string $scheme, array $parameters = [])
27
    {
28
        $this->scheme = $scheme;
29
        $this->parameters = $parameters;
30
    }
31
32
    public function getScheme(): ?string
33
    {
34
        return $this->scheme;
35
    }
36
37
    /**
38
     * @return static
39
     */
40
    public function withScheme(?string $scheme)
41
    {
42
        $new = clone $this;
43
        $new->scheme = $scheme;
44
45
        return $new;
46
    }
47
48
    public function getParameters(): array
49
    {
50
        return $this->parameters;
51
    }
52
53
    /**
54
     * @param mixed|null $default
55
     *
56
     * @return mixed
57
     */
58
    public function getParameter(string $key, $default = null)
59
    {
60
        return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
61
    }
62
63
    /**
64
     * @param mixed $value
65
     *
66
     * @return static
67
     */
68
    public function withParameter(string $key, $value)
69
    {
70
        $new = clone $this;
71
        $new->parameters[$key] = $value;
72
73
        return $new;
74
    }
75
76
    /**
77
     * @return static
78
     */
79
    public function withoutParameter(string $key)
80
    {
81
        $new = clone $this;
82
        unset($new->parameters[$key]);
83
84
        return $new;
85
    }
86
87
    public function getHost(): ?string
88
    {
89
        return null;
90
    }
91
92
    public function getPort(): ?int
93
    {
94
        return null;
95
    }
96
97
    public function getPath(): ?string
98
    {
99
        return null;
100
    }
101
102
    public function getUser(): ?string
103
    {
104
        return null;
105
    }
106
107
    public function getPassword(): ?string
108
    {
109
        return null;
110
    }
111
112
    /**
113
     * @var string
114
     */
115
    public function __toString()
116
    {
117
        $parameters = $this->getParameters();
118
        $scheme = $this->getScheme();
119
120
        return
121
            (empty($scheme) ? '' : $scheme.'://').
122
            (empty($parameters) ? '' : '?'.http_build_query($parameters));
123
    }
124
}
125