DbConfig::getDsn()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.1768
c 0
b 0
f 0
ccs 15
cts 15
cp 1
cc 5
nc 5
nop 0
crap 5
1
<?php
2
3
namespace ORM;
4
5
use PDO;
6
7
/**
8
 * Describes a database configuration
9
 *
10
 * @package ORM
11
 * @author  Thomas Flori <[email protected]>
12
 */
13
class DbConfig
14
{
15
    /** Dabase Type (mysql, pgsql or sqlite)
16
     * @var string */
17
    public $type;
18
19
    /** Database name or path for sqlite
20
     * @var string */
21
    public $name;
22
23
    /** Hostname or ip address
24
     * @var string */
25
    public $host = 'localhost';
26
27
    /** Port for DBMS (defaults to 3306 for mysql and 5432 for pgsql)
28
     * @var string  */
29
    public $port;
30
31
    /** Database user
32
     * @var string */
33
    public $user = 'root';
34
35
    /** Database password
36
     * @var string */
37
    public $pass;
38
39
    /** PDO attributes
40
     * @var array */
41
    public $attributes = [];
42
43
    /**
44
     * Constructor
45
     *
46
     * The constructor gets all parameters to establish a database connection and configure PDO instance.
47
     *
48
     * Example:
49
     *
50
     * ```php
51
     * $dbConfig = new DbConfig('mysql', 'my_db', 'my_user', 'my_secret', null, null, [
52
     *     \PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
53
     * ]);
54
     * ```
55
     *
56
     * @param string $type       Type of database (currently supported: `mysql`, `pgsql` and `sqlite`)
57
     * @param string $name       The name of the database or the path for sqlite
58
     * @param string $user       Username to use for connection
59
     * @param string $pass       Password
60
     * @param string $host       Hostname or IP address - defaults to `localhost`
61
     * @param string $port       Port - default ports (mysql: 3306, pgsql: 5432)
62 7
     * @param array  $attributes Array of PDO attributes
63
     */
64
    public function __construct(
65
        $type,
66
        $name,
67
        $user = null,
68
        $pass = null,
69
        $host = null,
70
        $port = null,
71
        $attributes = []
72 7
    ) {
73 7
74
        $this->type = $type;
75 7
        $this->name = $name;
76 7
77
        $this->user = $user ?: $this->user;
78 7
        $this->pass = $pass;
79
80 7
        $this->host = $host ?: $this->host;
81
82
        $this->attributes = $attributes;
83 7
84 2
        switch ($type) {
85 2
            case 'mysql':
86 2
                $this->port = $port ?: '3306';
87 2
                isset($this->attributes[PDO::ATTR_EMULATE_PREPARES])
88 2
                    || $this->attributes[PDO::ATTR_EMULATE_PREPARES] = false;
89 2
                isset($this->attributes[PDO::MYSQL_ATTR_INIT_COMMAND])
90
                    || $this->attributes[PDO::MYSQL_ATTR_INIT_COMMAND] = "SET sql_mode ='ANSI_QUOTES', NAMES utf8";
91 5
                break;
92 1
93 1
            case 'pgsql':
94
                $this->port = $port ?: '5432';
95
                break;
96 4
97
            default:
98 7
                $this->port = $port;
99
        }
100
    }
101
102
    /**
103
     * Get the data source name
104
     *
105 8
     * @return string
106
     */
107 8
    public function getDsn()
108
    {
109 8
        $dsn = $this->type . ':';
110 8
111 2
        switch ($this->type) {
112 1
            case 'mysql':
113
                if ($this->host[0] === '/') {
114 1
                    $dsn .= 'unix_socket=' . $this->host;
115
                } else {
116
                    $dsn .= 'host=' . $this->host . ';port=' . $this->port;
117 2
                }
118 2
119
                $dsn .= ';dbname=' . $this->name;
120 6
                break;
121 5
122 5
            case 'sqlite':
123
                $dsn .= $this->name;
124 1
                break;
125
126 1
            case 'pgsql':
127 1
            default:
128
                $dsn .= 'host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->name;
129
                break;
130 8
        }
131
132
        return $dsn;
133
    }
134
}
135