Completed
Push — master ( afca27...0b15f4 )
by WEBEWEB
05:13 queued 02:53
created

AbstractDatabase::setAuthenticator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
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 DateTime;
15
use Exception;
16
use PDO;
17
use WBW\Library\Core\Exception\IO\FileNotFoundException;
18
use WBW\Library\Core\Security\Authenticator;
19
20
/**
21
 * Abstract database.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Library\Core\Database
25
 */
26
abstract class AbstractDatabase {
27
28
    /**
29
     * Authenticator.
30
     *
31
     * @var Authenticator
32
     */
33
    private $authenticator;
34
35
    /**
36
     * Connection.
37
     *
38
     * @var PDO
39
     */
40
    private $connection;
41
42
    /**
43
     * Database.
44
     *
45
     * @var string
46
     */
47
    private $database;
48
49
    /**
50
     * Constructor.
51
     */
52
    protected function __construct(Authenticator $authenticator) {
53
        $this->authenticator = $authenticator;
54
    }
55
56
    /**
57
     * Connect.
58
     *
59
     * @return PDO Returns the connection.
60
     * @throws Exception Throws an exception if the connection failed.
61
     */
62
    abstract protected function connect();
63
64
    /**
65
     * Get the authenticator.
66
     *
67
     * @return Authenticator Returns the authenticator.
68
     */
69
    final public function getAuthenticator() {
70
        return $this->authenticator;
71
    }
72
73
    /**
74
     * Get the connection.
75
     *
76
     * @return PDO Returns the connection.
77
     * @throws Exception Throws an exception if the connection failed.
78
     */
79
    final public function getConnection() {
80
        if (null === $this->connection) {
81
            $this->connection = $this->connect();
82
        }
83
        return $this->connection;
84
    }
85
86
    /**
87
     * Get the database.
88
     *
89
     * @return string Returns the database.
90
     */
91
    final public function getDatabase() {
92
        return $this->database;
93
    }
94
95
    /**
96
     * Prepare a binding.
97
     *
98
     * @param array $fields The fields.
99
     * @return array Returns the binding as key => :key.
100
     */
101
    final public function prepareBinding(array $fields) {
102
        $output = [];
103
        foreach ($fields as $current) {
104
            $output[$current] = ":" . $current;
105
        }
106
        return $output;
107
    }
108
109
    /**
110
     * Prepare an INSERT SQL query.
111
     *
112
     * @param string $table The table.
113
     * @param array $values The values [field => value].
114
     * @return string Returns the INSERT SQL query.
115
     */
116
    final public function prepareInsert($table, array $values) {
117
118
        // Initialize the query.
119
        $query = [];
120
121
        $query[] = "INSERT INTO ";
122
        $query[] = $table;
123
        $query[] = " (`";
124
        $query[] = implode("`, `", array_keys($values));
125
        $query[] = "`) VALUES (";
126
        $query[] = implode(", ", array_values($values));
127
        $query[] = ")";
128
129
        // Return the query.
130
        return implode("", $query);
131
    }
132
133
    /**
134
     * Prepare an UPDATE SQL query.
135
     *
136
     * @param string $table The table.
137
     * @param array $values The values [field => value]
138
     * @return string Returns the UPDATE SQL query.
139
     */
140
    final public function prepareUpdate($table, array $values) {
141
142
        // Initialize the SET.
143
        $set = [];
144
        foreach ($values as $k => $v) {
145
            $set[] = "`" . $k . "` = " . $v;
146
        }
147
148
        // Initialize the query.
149
        $query = [];
150
151
        $query[] = "UPDATE ";
152
        $query[] = $table;
153
        $query[] = " SET ";
154
        $query[] = implode(", ", $set);
155
156
        // Return the query.
157
        return implode("", $query);
158
    }
159
160
    /**
161
     * Set the authenticator.
162
     *
163
     * @param Authenticator $authenticator The authenticator.
164
     * @return AbstractDatabase Returns this abstract database.
165
     */
166
    final public function setAuthenticator(Authenticator $authenticator) {
167
        $this->authenticator = $authenticator;
168
        return $this;
169
    }
170
171
    /**
172
     * Set the database.
173
     *
174
     * @param string $database The database.
175
     * @return AbstractDatabase Returns this abstract database.
176
     */
177
    final public function setDatabase($database) {
178
        $this->database = $database;
179
        return $this;
180
    }
181
182
}
183