Passed
Pull Request — master (#442)
by Wilmer
02:36
created

Dsn   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 16
c 3
b 0
f 0
dl 0
loc 96
ccs 15
cts 21
cp 0.7143
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabaseName() 0 3 1
A __toString() 0 3 1
A getOptions() 0 3 1
A getHost() 0 3 1
A getPort() 0 3 1
A getDriver() 0 3 1
A asString() 0 19 4
A __construct() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Connection;
6
7
use Stringable;
8
9
/**
10
 * The Dsn class is typically used to parse a DSN string, which is a string that contains all the necessary information
11
 * to connect to a database, such as the database driver, hostname, database name, port and options.
12
 *
13
 * It also allows you to access individual components of the DSN, such as the driver or the database name.
14
 */
15 956
final class Dsn implements Stringable
16
{
17 956
    /**
18 956
     * @psalm-param string[] $options
19 956
     */
20 956
    public function __construct(
21 956
        private string $driver,
22 956
        private string $host,
23
        private string $databaseName,
24
        private string|null $port = null,
25
        private array $options = []
26
    ) {
27
    }
28
29
    /**
30
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
31
     *
32
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
33
     *
34
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
35
     * `key=value` and concatenated by `;`. For example:
36
     *
37
     * ```php
38 956
     * $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306');
39
     * $connection = new Connection($this->cache, $this->logger, $this->profiler, $dsn->getDsn());
40 956
     * ```
41
     *
42 956
     * Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306`.
43 956
     */
44
    public function asString(): string
45
    {
46 956
        $dsn = "$this->driver:" . "host=$this->host" . ';' . "dbname=$this->databaseName";
47
48 956
        if ($this->port !== null) {
49
            $dsn .= ';' . "port=$this->port";
50
        }
51
52 956
        $parts = [];
53
54
        foreach ($this->options as $key => $value) {
55
            $parts[] = "$key=$value";
56 956
        }
57
58
        if (!empty($parts)) {
59
            $dsn .= ';' . implode(';', $parts);
60
        }
61
62
        return $dsn;
63
    }
64
65
    /**
66
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
67
     */
68
    public function __toString(): string
69
    {
70
        return $this->asString();
71
    }
72
73
    /**
74
     * @return string The database name to connect to.
75
     */
76
    public function getDatabaseName(): string
77
    {
78
        return $this->databaseName;
79
    }
80
81
    /**
82
     * @return string The database driver name.
83
     */
84
    public function getDriver(): string
85
    {
86
        return $this->driver;
87
    }
88
89
    /**
90
     * @return string The database host name or IP address.
91
     */
92
    public function getHost(): string
93
    {
94
        return $this->host;
95
    }
96
97
    /**
98
     * @return array The database connection options. Default value to an empty array.
99
     */
100
    public function getOptions(): array
101
    {
102
        return $this->options;
103
    }
104
105
    /**
106
     * @return string|null The database port. Null if not set.
107
     */
108
    public function getPort(): string|null
109
    {
110
        return $this->port;
111
    }
112
}
113