Passed
Pull Request — master (#533)
by Wilmer
02:11
created

AbstractDsnSocket   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A asString() 0 15 3
A getOptions() 0 3 1
A __toString() 0 3 1
A __construct() 0 6 1
A getUnixSocket() 0 3 1
A getDriver() 0 3 1
A getDatabaseName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Connection;
6
7
use Stringable;
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, such as the database driver, unix socket, database name, options.
12
 *
13
 * It also allows you to access individual components of the DSN, such as the driver or the database name.
14
 */
15
abstract class AbstractDsnSocket implements DsnInterface, Stringable
16
{
17
    public function __construct(
18
        private string $driver,
19
        private string $unixSocket,
20
        private string $databaseName,
21
        private array $options = []
22
    ) {
23
    }
24
25
    public function asString(): string
26
    {
27
        $dsn = "$this->driver:" . "unix_socket=$this->unixSocket" . ';' . "dbname=$this->databaseName";
28
29
        $parts = [];
30
31
        foreach ($this->options as $key => $value) {
32
            $parts[] = "$key=$value";
33
        }
34
35
        if (!empty($parts)) {
36
            $dsn .= ';' . implode(';', $parts);
37
        }
38
39
        return $dsn;
40
    }
41
42
    /**
43
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
44
     */
45
    public function __toString(): string
46
    {
47
        return $this->asString();
48
    }
49
50
    /**
51
     * @return string The database name to connect to.
52
     */
53
    public function getDatabaseName(): string
54
    {
55
        return $this->databaseName;
56
    }
57
58
    /**
59
     * @return string The database driver to use.
60
     */
61
    public function getDriver(): string
62
    {
63
        return $this->driver;
64
    }
65
66
    /**
67
     * @return string The unix socket to connect to.
68
     */
69
    public function getUnixSocket(): string
70
    {
71
        return $this->unixSocket;
72
    }
73
74
    /**
75
     * @return array The options to use.
76
     */
77
    public function getOptions(): array
78
    {
79
        return $this->options;
80
    }
81
}
82