Dsn   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 41
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A asString() 0 15 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Connection\AbstractDsn;
8
9
/**
10
 * Implement a Data Source Name (DSN) for an MSSQL Server.
11
 *
12
 * @link https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php
13
 */
14
final class Dsn extends AbstractDsn
15
{
16 1094
    public function __construct(
17
        private string $driver,
18
        private string $host,
19
        private string|null $databaseName = null,
20
        private string $port = '1433'
21
    ) {
22 1094
        parent::__construct($driver, $host, $databaseName, $port);
23
    }
24
25
    /**
26
     * @return string the Data Source Name, or DSN, has the information required to connect to the database.
27
     * Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
28
     *
29
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
30
     * `key=value` and concatenated by `;`. For example:
31
     *
32
     * ```php
33
     * $dsn = new Dsn('sqlsrv', 'localhost', 'yiitest', '1433');
34
     * $driver = new Driver($dsn->asString(), 'username', 'password');
35
     * $db = new Connection($driver, $schemaCache);
36
     * ```
37
     *
38
     * Will result in the DSN string `sqlsrv:Server=localhost,1433;Database=yiitest`.
39
     */
40 1094
    public function asString(): string
41
    {
42 1094
        if ($this->port !== '') {
43 1093
            $server = "Server=$this->host,$this->port;";
44
        } else {
45 1
            $server = "Server=$this->host;";
46
        }
47
48 1094
        if (!empty($this->databaseName)) {
49 1090
            $dsn = "$this->driver:" . $server . "Database=$this->databaseName";
50
        } else {
51 4
            $dsn = "$this->driver:" . $server;
52
        }
53
54 1094
        return $dsn;
55
    }
56
}
57