Completed
Push — master ( 73bcb1...dd589b )
by WEBEWEB
02:03
created

AbstractDatabase::getAuthenticator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
     * Execute a script.
66
     *
67
     * @param string $scriptname The script name.
68
     * @param boolean $output Output ?
69
     * @return array Returns an array with success, failed and total queries.
70
     * @throws FileNotFoundException Throws a file not found exception if the script name does not exists.
71
     */
72
    final public function executeScript($scriptname, $output = false) {
73
74
        // Initialize the pathname.
75
        $pathname = realpath($scriptname);
76
77
        // Check the pathname.
78
        if (false === file_exists($pathname)) {
79
            throw new FileNotFoundException($pathname);
80
        }
81
82
        // Initialize the output.
83
        $success = 0;
84
        $failed  = 0;
85
        $total   = 0;
86
87
        // Open the pathname.
88
        $stream = fopen($pathname);
89
90
        // Handle each query.
91
        while (false === feof($stream)) {
92
93
            // Read the query.
94
            $query = fegts($stream);
95
96
            // Prepare and execute the statement.
97
            $stmt   = $this->connection->prepare($query);
98
            $result = $stmt->execute();
99
100
            // Increase.
101
            ++$total;
102
            true === $result ? ++$success : ++$failed;
103
104
            // Check the output.
105
            if (true === $output) {
106
                echo "[" . (new DateTime())->format("Y-m-d H:i:s.u") . "] " . (true === $result ? "OK" : "KO") . " " . $query . "\n";
107
            }
108
        }
109
110
        // Close the pathname.
111
        fclose($pathname);
112
113
        // Return.
114
        return [$success, $failed, $total];
115
    }
116
117
    /**
118
     * Get the authenticator.
119
     *
120
     * @return Authenticator Returns the authenticator.
121
     */
122
    final public function getAuthenticator() {
123
        return $this->authenticator;
124
    }
125
126
    /**
127
     * Get the connection.
128
     *
129
     * @return PDO Returns the connection.
130
     * @throws Exception Throws an exception if the connection failed.
131
     */
132
    final public function getConnection() {
133
        if (null === $this->connection) {
134
            $this->connection = $this->connect();
135
        }
136
        return $this->connection;
137
    }
138
139
    /**
140
     * Get the database.
141
     *
142
     * @return string Returns the database.
143
     */
144
    final public function getDatabase() {
145
        return $this->database;
146
    }
147
148
    /**
149
     * Prepare a binding.
150
     *
151
     * @param array $fields The fields.
152
     * @return array Returns the binding as key => :key.
153
     */
154
    final public function prepareBinding(array $fields) {
155
        $output = [];
156
        foreach ($fields as $current) {
157
            $output[$current] = ":" . $current;
158
        }
159
        return $output;
160
    }
161
162
    /**
163
     * Prepare an INSERT SQL query.
164
     *
165
     * @param string $table The table.
166
     * @param array $values The values [field => value].
167
     * @return string Returns the INSERT SQL query.
168
     */
169
    final public function prepareInsert($table, array $values) {
170
171
        // Initialize the query.
172
        $query = [];
173
174
        $query[] = "INSERT INTO ";
175
        $query[] = $table;
176
        $query[] = " (`";
177
        $query[] = implode("`, `", array_keys($values));
178
        $query[] = "`) VALUES (";
179
        $query[] = implode(", ", array_values($values));
180
        $query[] = ")";
181
182
        // Return the query.
183
        return implode("", $query);
184
    }
185
186
    /**
187
     * Prepare an UPDATE SQL query.
188
     *
189
     * @param string $table The table.
190
     * @param array $values The values [field => value]
191
     * @return string Returns the UPDATE SQL query.
192
     */
193
    final public function prepareUpdate($table, array $values) {
194
195
        // Initialize the SET.
196
        $set = [];
197
        foreach ($values as $k => $v) {
198
            $set[] = "`" . $k . "` = " . $v;
199
        }
200
201
        // Initialize the query.
202
        $query = [];
203
204
        $query[] = "UPDATE ";
205
        $query[] = $table;
206
        $query[] = " SET ";
207
        $query[] = implode(", ", $set);
208
209
        // Return the query.
210
        return implode("", $query);
211
    }
212
213
    /**
214
     * Set the authenticator.
215
     *
216
     * @param Authenticator $authenticator The authenticator.
217
     * @return AbstractDatabase Returns this abstract database.
218
     */
219
    final public function setAuthenticator(Authenticator $authenticator) {
220
        $this->authenticator = $authenticator;
221
        return $this;
222
    }
223
224
    /**
225
     * Set the database.
226
     *
227
     * @param string $database The database.
228
     * @return AbstractDatabase Returns this abstract database.
229
     */
230
    final public function setDatabase($database) {
231
        $this->database = $database;
232
        return $this;
233
    }
234
235
}
236