Passed
Pull Request — master (#37)
by Eugene
05:55
created

Dsn::getPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client;
15
16
final class Dsn
17
{
18
    private $host;
19
    private $port;
20
    private $path;
21
    private $connectionUri;
22
    private $username;
23
    private $password;
24
    private $isTcp = false;
25
    private $options;
26
27 136
    private function __construct(string $connectionUri)
28
    {
29 136
        $this->connectionUri = $connectionUri;
30 136
    }
31
32 170
    public static function parse(string $dsn) : self
33
    {
34 170
        if (0 === \strpos($dsn, 'unix://') && isset($dsn[7])) {
35 64
            return self::parseUds($dsn);
36
        }
37
38 106
        if (false === $parsed = \parse_url($dsn)) {
39 6
            self::throwParseError($dsn);
40
        }
41 100
        if (!isset($parsed['scheme'], $parsed['host']) || 'tcp' !== $parsed['scheme']) {
42 22
            self::throwParseError($dsn);
43
        }
44 78
        if (isset($parsed['path']) && '/' !== $parsed['path']) {
45 2
            self::throwParseError($dsn);
46
        }
47
48 76
        $self = new self('tcp://'.$parsed['host'].':'.($parsed['port'] ?? '3301'));
49 76
        $self->host = $parsed['host'];
50 76
        $self->port = $parsed['port'] ?? 3301;
51 76
        $self->isTcp = true;
52
53 76
        if (isset($parsed['user'])) {
54 6
            $self->username = \rawurldecode($parsed['user']);
55 6
            $self->password = isset($parsed['pass']) ? \rawurldecode($parsed['pass']) : '';
56
        }
57
58 76
        if (isset($parsed['query'])) {
59 52
            \parse_str($parsed['query'], $self->options);
60
        }
61
62 76
        return $self;
63
    }
64
65 64
    private static function parseUds(string $dsn) : self
66
    {
67 64
        $parts = \explode('@', \substr($dsn, 7), 2);
68 64
        if (isset($parts[1])) {
69 8
            $parsed = \parse_url($parts[1]);
70 8
            $authority = \explode(':', $parts[0]);
71
        } else {
72 56
            $parsed = \parse_url($parts[0]);
73
        }
74
75 64
        if (false === $parsed) {
76 2
            self::throwParseError($dsn);
77
        }
78 62
        if (isset($parsed['host'])) {
79 2
            self::throwParseError($dsn);
80
        }
81
82 60
        $self = new self('unix://'.$parsed['path']);
83 60
        $self->path = \rawurldecode($parsed['path']);
84
85 60
        if (isset($authority)) {
86 8
            $self->username = \rawurldecode($authority[0]);
87 8
            $self->password = isset($authority[1]) ? \rawurldecode($authority[1]) : '';
88
        }
89
90 60
        if (isset($parsed['query'])) {
91 46
            \parse_str($parsed['query'], $self->options);
92
        }
93
94 60
        return $self;
95
    }
96
97 34
    public function getConnectionUri() : string
98
    {
99 34
        return $this->connectionUri;
100
    }
101
102 34
    public function getHost() : ?string
103
    {
104 34
        return $this->host;
105
    }
106
107 34
    public function getPort() : ?int
108
    {
109 34
        return $this->port;
110
    }
111
112 34
    public function getPath() : ?string
113
    {
114 34
        return $this->path;
115
    }
116
117 14
    public function getUsername() : ?string
118
    {
119 14
        return $this->username;
120
    }
121
122 14
    public function getPassword() : ?string
123
    {
124 14
        return $this->password;
125
    }
126
127 34
    public function isTcp() : bool
128
    {
129 34
        return $this->isTcp;
130
    }
131
132 30
    public function getString(string $name, ?string $default = null) : ?string
133
    {
134 30
        return $this->options[$name] ?? $default;
135
    }
136
137 42
    public function getBool(string $name, ?bool $default = null) : ?bool
138
    {
139 42
        if (!isset($this->options[$name])) {
140 6
            return $default;
141
        }
142
143 36
        if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE)) {
144 8
            throw new \TypeError(\sprintf('DSN option "%s" must be of the type bool.', $name));
145
        }
146
147 28
        return $value;
148
    }
149
150 30
    public function getInt(string $name, ?int $default = null) : ?int
151
    {
152 30
        if (!isset($this->options[$name])) {
153 6
            return $default;
154
        }
155
156 24
        if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE)) {
157 16
            throw new \TypeError(\sprintf('DSN option "%s" must be of the type int.', $name));
158
        }
159
160 8
        return $value;
161
    }
162
163 34
    private static function throwParseError(string $dsn) : void
164
    {
165 34
        throw new \InvalidArgumentException(\sprintf('Unable to parse DSN "%s".', $dsn));
166
    }
167
}
168