Passed
Pull Request — master (#193)
by Wilmer
16:42 queued 01:44
created

Dsn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 5
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Connection;
6
7
final class Dsn
8
{
9
    private ?string $databaseName;
10
    private string $driver;
11
    private ?string $host;
12
    private ?string $port;
13
    private array $options;
14
15
    public function __construct(string $driver, string $host, string $databaseName, string $port = null, array $options = [])
16
    {
17
        $this->driver = $driver;
18
        $this->host = $host;
19
        $this->databaseName = $databaseName;
20
        $this->port = $port;
21
        $this->options = $options;
22
    }
23
24
    /**
25
     * @return string the Data Source Name, or DSN, contains the information required to connect to the database.
26
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
27
     *
28
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
29
     * `key=value` and concatenated by `;`. For example:
30
     *
31
     * ```php
32
     * $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306');
33
     * $connection = new Connection($this->cache, $this->logger, $this->profiler, $dsn->asString());
34
     * ```
35
     *
36
     * With autowired container-di:
37
     *
38
     * ```php
39
     * $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306');
40
     * $connection = new Connection('dsn' => $dsn->asString());
41
     * ```
42
     *
43
     * Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306`.
44
     */
45
    public function asString(): string
46
    {
47
        if ($this->driver === 'sqlsrv') {
48
            $dsn = $this->buildDsnSqlSrv();
49
        } else {
50
            $dsn = $this->buildDsn();
51
        }
52
53
        $parts = [];
54
55
        foreach ($this->options as $key => $value) {
56
            $parts[] = "$key=$value";
57
        }
58
59
        if (!empty($parts)) {
60
            $dsn .= ';' . implode(';', $parts);
61
        }
62
63
        return $dsn;
64
    }
65
66
    public function __toString(): string
67
    {
68
        return $this->asString();
69
    }
70
71
    public function getDriver(): string
72
    {
73
        return $this->driver;
74
    }
75
76
    private function buildDsn(): string
77
    {
78
        $dsn = "$this->driver:" . "host=$this->host" . ';' . "dbname=$this->databaseName";
79
80
        if ($this->port !== null) {
81
            $dsn .= ';' . "port=$this->port";
82
        }
83
84
        return $dsn;
85
    }
86
87
    private function buildDsnSqlSrv(): string
88
    {
89
        $dsn = "$this->driver:" . "Server=$this->host" . ";Database=$this->databaseName";
90
91
        if ($this->port !== null) {
92
            $dsn = "$this->driver:" . "Server=$this->host," . "$this->port" . ";Database=$this->databaseName";
93
        }
94
95
        return $dsn;
96
    }
97
}
98