Passed
Pull Request — master (#30)
by Wilmer
15:31 queued 03:48
created

Dsn::asString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
final class Dsn
8
{
9
    private string $databaseName;
10
    private string $driver;
11
    private string $server;
12
    private string $port;
13
14 408
    public function __construct(string $driver, string $server, string $databaseName, string $port = '1433')
15
    {
16 408
        $this->driver = $driver;
17 408
        $this->server = $server;
18 408
        $this->databaseName = $databaseName;
19 408
        $this->port = $port;
20 408
    }
21
22
    /**
23
     * @return string the Data Source Name, or DSN, contains the information required to connect to the database.
24
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
25
     *
26
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
27
     * `key=value` and concatenated by `;`. For example:
28
     *
29
     * ```php
30
     * $dsn = new MssqlDsn('sqlsrv', 'localhost', 'yiitest', '1433');
31
     * $connection = new MssqlConnection($this->cache, $this->logger, $this->profiler, $dsn->asString());
32
     * ```
33
     *
34
     * Will result in the DSN string `sqlsrv:Server=localhost,1433;Database=yiitest`.
35
     */
36
    public function asString(): string
37 408
    {
38
        $dsn = "$this->driver:" . "Server=$this->server" . ";Database=$this->databaseName";
39 408
40
        if ($this->port !== null) {
41
            $dsn = "$this->driver:" . "Server=$this->server," . "$this->port" . ";Database=$this->databaseName";
42 1
        }
43
44 1
        return $dsn;
45
    }
46
47
    public function __toString(): string
48
    {
49
        return $this->asString();
50
    }
51
52
    public function getDriver(): string
53
    {
54
        return $this->driver;
55
    }
56
}
57