Passed
Pull Request — master (#166)
by Wilmer
03:34
created

Dsn::__toString()   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
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Mssql;
6
7
use Yiisoft\Db\Connection\AbstractDsn;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Db\Connection\AbstractDsn was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 SQL Server, such as the database driver, host, database name, port.
12
 *
13
 * It also allows you to access individual components of the DSN, such as the driver, host, database name or port.
14
 *
15
 * @link https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php
16
 */
17
final class Dsn extends AbstractDsn
18
{
19 617
    public function __construct(
20
        private string $driver,
21
        private string $host,
22
        private string $databaseName,
23
        private string $port = '1433'
24
    ) {
25 617
        parent::__construct($driver, $host, $databaseName, $port);
26
    }
27
28
    /**
29
     * @return string the Data Source Name, or DSN, contains the information required to connect to the database.
30
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
31
     *
32
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
33
     * `key=value` and concatenated by `;`. For example:
34
     *
35
     * ```php
36
     * $dsn = new Dsn('sqlsrv', 'localhost', 'yiitest', '1433');
37
     * $db = new ConnectionPDO(new PDODriver($dsn->asString(), 'username', 'password'), $queryCache, $schemaCache);
38
     * ```
39
     *
40
     * Will result in the DSN string `sqlsrv:Server=localhost,1433;Database=yiitest`.
41
     */
42 617
    public function asString(): string
43
    {
44 617
        return match ($this->port) {
45 617
            '' => "$this->driver:" . "Server=$this->host;" . "Database=$this->databaseName",
46 617
            default => "$this->driver:" . "Server=$this->host,$this->port;" . "Database=$this->databaseName",
47 617
        };
48
    }
49
}
50