Passed
Push — fix-phpdoc-more-test ( 63ce51...5bc98c )
by Wilmer
49:58
created

Dsn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
eloc 1
c 1
b 1
f 1
nc 1
nop 5
dl 0
loc 8
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
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, host, database name, port, options.
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-oci.connection.php
16
 */
17
final class Dsn extends AbstractDsn
18
{
19
    /**
20
     * @psalm-param string[] $options
21
     */
22 2
    public function __construct(
23
        private string $driver,
24
        private string $host,
25
        private string $databaseName,
26
        private string $port = '1521',
27
        private array $options = []
28
    ) {
29 2
        parent::__construct($driver, $host, $databaseName, $port, $options);
30
    }
31
32
    /**
33
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
34
     *
35
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
36
     *
37
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
38
     * `key=value` and concatenated by `;`. For example:
39
     *
40
     * ```php
41
     * $dsn = new Dsn('oci', '127.0.0.1', 'yiitest', '3306');
42
     * $connection = new Connection($this->cache, $this->logger, $this->profiler, $dsn->getDsn());
43
     * ```
44
     *
45
     * Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306`.
46
     */
47 2
    public function asString(): string
48
    {
49 2
        $dsn = match ($this->port) {
50 2
            '' => "$this->driver:" . "dbname=$this->host/$this->databaseName",
51 2
            default => "$this->driver:" . "dbname=$this->host:$this->port/$this->databaseName",
52 2
        };
53
54 2
        $parts = [];
55
56 2
        foreach ($this->options as $key => $value) {
57 2
            $parts[] = "$key=$value";
58
        }
59
60 2
        if (!empty($parts)) {
61 2
            $dsn .= ';' . implode(';', $parts);
62
        }
63
64 2
        return $dsn;
65
    }
66
}
67