Completed
Push — master ( c1864c...4b5d3a )
by Rasmus
02:53
created

PDOProvider::getProtocol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace mindplay\sql\framework\pdo;
4
5
use InvalidArgumentException;
6
use PDO;
7
8
/**
9
 * This class implements a provider layer over PDO.
10
 *
11
 * You don't need to use this layer - the framework itself has no dependency on it, but you
12
 * may find it useful as a means of addressing certain issues with `PDO`.
13
 *
14
 * For one, `PDO` instances cannot be created without immediately opening the connection - this
15
 * provider does not construct `PDO` and open the connection until you ask for the `PDO` instance.
16
 *
17
 * Also, `PDO` has no ability to report properties like database-name, username, etc. if you need them.
18
 */
19
class PDOProvider
20
{
21
    /**
22
     * @var string
23
     */
24
    private $protocol;
25
26
    /**
27
     * @var string
28
     */
29
    private $dsn;
30
31
    /**
32
     * @var string
33
     */
34
    private $username;
35
36
    /**
37
     * @var string
38
     */
39
    private $password;
40
41
    /**
42
     * @var array
43
     */
44
    private $options;
45
46
    /**
47
     * @var string
48
     */
49
    private $host;
50
51
    /**
52
     * @var string
53
     */
54
    private $port;
55
56
    /**
57
     * @var PDO|null
58
     */
59
    private $pdo;
60
61
    const PROTOCOL_POSTGRES = "pgsql";
62
    const PROTOCOL_MYSQL    = "mysql";
63
64
    /**
65
     * @param string      $protocol database protocol name (one of the PROTOCOL_* class constants)
66
     * @param string      $dbname   name of the database to connect to
67
     * @param string      $username
68
     * @param string      $password
69
     * @param array|null  $options  PDO constructor options
70
     * @param string|null $host     optional hostname; defaults to "localhost"
71
     * @param int|null    $port     optional port-number; defaults to the standard port-number for the given $db type
72
     */
73 1
    public function __construct($protocol, $dbname, $username, $password, $options = null, $host = null, $port = null)
74
    {
75 1
        static $default_port = [
76
            self::PROTOCOL_MYSQL    => 3306,
77
            self::PROTOCOL_POSTGRES => 5432,
78
        ];
79
80 1
        if (! isset($default_port[$protocol])) {
81 1
            throw new InvalidArgumentException("unsupported DBMS type: {$protocol}");
82
        }
83
84 1
        if ($port === null) {
85
            $port = $default_port[$protocol];
86
        }
87
88 1
        $this->protocol = $protocol;
89 1
        $this->dsn = "{$protocol}:host={$host};port={$port};dbname={$dbname}";
90 1
        $this->username = $username;
91 1
        $this->password = $password;
92 1
        $this->options = $options ?: [];
93 1
        $this->host = $host ?: "localhost";
94 1
        $this->port = $port;
95 1
    }
96
97
    /**
98
     * @return PDO
99
     */
100
    public function getPDO()
101
    {
102
        if (! isset($this->pdo)) {
103
            $this->pdo = new PDO($this->dsn, $this->username, $this->password, $this->options);
104
        }
105
106
        return $this->pdo;
107
    }
108
109
    /**
110
     * @return string database procol name (one of the PROTOCOL_* class constants)
111
     */
112 1
    public function getProtocol()
113
    {
114 1
        return $this->protocol;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 1
    public function getDSN()
121
    {
122 1
        return $this->dsn;
123
    }
124
125
    /**
126
     * @return string
127
     */
128 1
    public function getUsername()
129
    {
130 1
        return $this->username;
131
    }
132
133
    /**
134
     * @return string
135
     */
136 1
    public function getPassword()
137
    {
138 1
        return $this->password;
139
    }
140
141
    /**
142
     * @return array
143
     */
144 1
    public function getOptions()
145
    {
146 1
        return $this->options;
147
    }
148
149
    /**
150
     * @return string
151
     */
152 1
    public function getHost()
153
    {
154 1
        return $this->host;
155
    }
156
157
    /**
158
     * @return string
159
     */
160 1
    public function getPort()
161
    {
162 1
        return $this->port;
163
    }
164
}
165