Completed
Push — master ( 69c880...49fe21 )
by WEBEWEB
04:44
created

AbstractDatabase   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 184
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
connect() 0 1 ?
A getConnection() 0 6 2
A getDatabase() 0 3 1
A getPassword() 0 3 1
A getUsername() 0 3 1
A prepareBinding() 0 7 2
A prepareInsert() 0 16 1
A prepareUpdate() 0 19 2
A setDatabase() 0 4 1
A setPassword() 0 4 1
A setUsername() 0 4 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Database;
13
14
use Exception;
15
use PDO;
16
17
/**
18
 * Abstract database.
19
 *
20
 * @author NdC/WBW <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Database
22
 */
23
abstract class AbstractDatabase {
24
25
    /**
26
     * Connection.
27
     *
28
     * @var PDO
29
     */
30
    private $connection;
31
32
    /**
33
     * Database.
34
     *
35
     * @var string
36
     */
37
    private $database;
38
39
    /**
40
     * Password.
41
     *
42
     * @var string
43
     */
44
    private $password;
45
46
    /**
47
     * Username.
48
     *
49
     * @var string
50
     */
51
    private $username;
52
53
    /**
54
     * Constructor.
55
     */
56
    protected function __construct() {
57
        // NOTHING TO DO.
58
    }
59
60
    /**
61
     * Connect.
62
     *
63
     * @return PDO Returns the connection.
64
     * @throws Exception Throws an exception if the connection failed.
65
     */
66
    abstract protected function connect();
67
68
    /**
69
     * Get the connection.
70
     *
71
     * @return PDO Returns the connection.
72
     * @throws Exception Throws an exception if the connection failed.
73
     */
74
    final public function getConnection() {
75
        if (null === $this->connection) {
76
            $this->connection = $this->connect();
77
        }
78
        return $this->connection;
79
    }
80
81
    /**
82
     * Get the database.
83
     *
84
     * @return string Returns the database.
85
     */
86
    final public function getDatabase() {
87
        return $this->database;
88
    }
89
90
    /**
91
     * Get the paswword.
92
     *
93
     * @return string Returns the password.
94
     */
95
    final public function getPassword() {
96
        return $this->password;
97
    }
98
99
    /**
100
     * Get the username.
101
     *
102
     * @return string Returns the username.
103
     */
104
    final public function getUsername() {
105
        return $this->username;
106
    }
107
108
    /**
109
     * Prepare a binding.
110
     *
111
     * @param array $fields The fields.
112
     * @return array Returns the binding as key => :key.
113
     */
114
    final public function prepareBinding(array $fields) {
115
        $output = [];
116
        foreach ($fields as $current) {
117
            $output[$current] = ":" . $current;
118
        }
119
        return $output;
120
    }
121
122
    /**
123
     * Prepare an INSERT SQL query.
124
     *
125
     * @param string $table The table.
126
     * @param array $values The values [field => value].
127
     * @return string Returns the INSERT SQL query.
128
     */
129
    final public function prepareInsert($table, array $values) {
130
131
        // Initialize the query.
132
        $query = [];
133
134
        $query[] = "INSERT INTO ";
135
        $query[] = $table;
136
        $query[] = " (";
137
        $query[] = implode(", ", array_keys($values));
138
        $query[] = ") VALUES (";
139
        $query[] = implode(", ", array_values($values));
140
        $query[] = ")";
141
142
        // Return the query.
143
        return implode("", $query);
144
    }
145
146
    /**
147
     * Prepare an UPDATE SQL query.
148
     *
149
     * @param string $table The table.
150
     * @param array $values The values [field => value]
151
     * @return string Returns the UPDATE SQL query.
152
     */
153
    final public function prepareUpdate($table, array $values) {
154
155
        // Initialize the SET.
156
        $set = [];
157
        foreach ($values as $k => $v) {
158
            $set[] = $k . " = " . $v;
159
        }
160
161
        // Initialize the query.
162
        $query = [];
163
164
        $query[] = "UPDATE ";
165
        $query[] = $table;
166
        $query[] = " SET ";
167
        $query[] = implode(", ", $set);
168
169
        // Return the query.
170
        return implode("", $query);
171
    }
172
173
    /**
174
     * Set the database.
175
     *
176
     * @param string $database The database.
177
     * @return AbstractDatabase Returns the database.
178
     */
179
    final public function setDatabase($database) {
180
        $this->database = $database;
181
        return $this;
182
    }
183
184
    /**
185
     * Set the password.
186
     *
187
     * @param string $password The password.
188
     * @return AbstractDatabase Returns the database.
189
     */
190
    final public function setPassword($password) {
191
        $this->password = $password;
192
        return $this;
193
    }
194
195
    /**
196
     * Set the username.
197
     *
198
     * @param string $username The username.
199
     * @return AbstractDatabase Returns the database.
200
     */
201
    final public function setUsername($username) {
202
        $this->username = $username;
203
        return $this;
204
    }
205
206
}
207