Completed
Push — master ( e3dcd8...d89f57 )
by Rasmus
02:20
created

PDOProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.38%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 12
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 146
ccs 27
cts 32
cp 0.8438
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
A getPDO() 0 8 2
A getDBMS() 0 4 1
A getDSN() 0 4 1
A getUsername() 0 4 1
A getPassword() 0 4 1
A getOptions() 0 4 1
A getHost() 0 4 1
A getPort() 0 4 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 $type;
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 DBMS_POSTGRES = "pgsql";
62
    const DBMS_MYSQL    = "mysql";
63
64
    /**
65
     * @param string   $dbms    database type; one of the DBMS_* class constants
66
     * @param string   $dbname  name of the database to connect to
67
     * @param string   $username
68
     * @param string   $password
69
     * @param array    $options PDO constructor options
70
     * @param string   $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($dbms, $dbname, $username, $password, $options = [], $host = 'localhost', $port = null)
74
    {
75 1
        static $default_port = [
76
            self::DBMS_MYSQL    => 3306,
77
            self::DBMS_POSTGRES => 5432,
78
        ];
79
80 1
        if (!isset($default_port[$dbms])) {
81 1
            throw new InvalidArgumentException("unsupported DBMS type: {$dbms}");
82
        }
83
84 1
        if ($port === null) {
85
            $port = $default_port[$dbms];
86
        }
87
        
88 1
        $this->type = $dbms;
89 1
        $this->dsn = "{$dbms}: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;
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 one of the DBMS_* class constants
111
     */
112 1
    public function getDBMS()
113
    {
114 1
        return $this->type;
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