Passed
Branch master (5430e1)
by Wilmer
12:07 queued 08:14
created

Dsn   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
wmc 2
eloc 6
c 1
b 1
f 1
dl 0
loc 32
ccs 7
cts 7
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A asString() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Yiisoft\Db\Connection\AbstractDsn;
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, database name.
12
 *
13
 * It also allows you to access individual components of the DSN, such as the driver, database name.
14
 *
15
 * @link https://www.php.net/manual/en/ref.pdo-sqlite.connection.php
16
 */
17
final class Dsn extends AbstractDsn
18
{
19
    /**
20
     * @psalm-param string[] $options
21
     */
22 582
    public function __construct(private string $driver, private string $databaseName = '')
23
    {
24 582
        parent::__construct($driver, '', $databaseName);
25
    }
26
27
    /**
28
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
29
     *
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('sqlite', __DIR__ . '/data/test.sq3');
37
     * $pdoDriver = new PDODriver($dsn->asString());
38
     * $db = new ConnectionPDO($pdoDriver, $queryCache, $schemaCache);
39
     * ```
40
     *
41
     * Will result in the DSN string `sqlite:/path/to/data/test.sq3`.
42
     */
43 582
    public function asString(): string
44
    {
45 582
        return match ($this->databaseName) {
46 582
            'memory' => $this->driver . '::memory:',
47 582
            '' => $this->driver . ':',
48 582
            default => $this->driver . ':' . $this->databaseName,
49 582
        };
50
    }
51
}
52