Debian Codegraph @

classes/Database/DatabaseController.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2022-2023 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the DatabaseController class
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg\Database;
33
34 use Liuch\DmarcSrg\Exception\RuntimeException;
35 use Liuch\DmarcSrg\Exception\DatabaseFatalException;
36
37 /**
38 * Proxy class for accessing a database of the selected type
39 */
40 class DatabaseController
41 {
42 public const REQUIRED_VERSION = '4.0';
43
44 private $conf_data = null;
45 /** @var DatabaseConnector|null */
46 private $connector = null;
47
48 /**
49 * The constructor
50 *
51 * @param \Liuch\DmarcSrg\Core $core Instance of the Core class
52 * @param DatabaseConnector $connector The connector class of the current database
53 */
54 public function __construct($core, $connector = null)
55 {
56 $this->conf_data = $core->config('database');
57 $this->connector = $connector;
58 }
59
60 /**
61 * Returns the database type
62 *
63 * @return string
64 */
65 public function type(): string
66 {
67 return $this->conf_data['type'] ?? '';
68 }
69
70 /**
71 * Returns the database name
72 *
73 * @return string
74 */
75 public function name(): string
76 {
77 return $this->conf_data['name'] ?? '';
78 }
79
80 /**
81 * Returns the database host
82 *
83 * @return string
84 */
85 public function location(): string
86 {
87 return $this->conf_data['host'] ?? '';
88 }
89
90 /**
91 * Returns information about the database as an array.
92 *
93 * @return array May contain the following fields:
94 * `tables` - an array of tables with their properties;
95 * `needs_upgrade` - true if the database needs upgrading;
96 * `correct` - true if the database is correct;
97 * `type` - the database type;
98 * `name` - the database name;
99 * `location` - the database location;
100 * `version` - the current version of the database structure;
101 * `message` - a state message;
102 * `error_code` - an error code;
103 */
104 public function state(): array
105 {
106 $this->ensureConnector();
107
108 $res = $this->connector->state();
109 $res['type'] = $this->type();
110 $res['name'] = $this->name();
111 $res['location'] = $this->location();
112 if ((($res['correct'] ?? false) || ($res['error_code'] ?? 0) === -4)
113 && ($res['version'] ?? 'null') !== self::REQUIRED_VERSION
114 ) {
115 $res['correct'] = false;
116 $res['message'] = 'The database structure needs upgrading';
117 $res['needs_upgrade'] = true;
118 unset($res['error_code']);
119 }
120 return $res;
121 }
122
123 /**
124 * Initiates the database.
125 *
126 * This method creates needed tables and indexes in the database.
127 * The method will fail if the database already have tables with the table prefix.
128 *
129 * @return array Result array with `error_code` and `message` fields.
130 */
131 public function initDb(): array
132 {
133 $this->ensureConnector();
134 $this->connector->initDb(self::REQUIRED_VERSION);
135 return [ 'message' => 'The database has been initiated' ];
136 }
137
138 /**
139 * Cleans up the database.
140 *
141 * Drops tables with the table prefix in the database or all tables in the database if no table prefix is set.
142 *
143 * @return array Result array with `error_code` and `message` fields.
144 */
145 public function cleanDb(): array
146 {
147 $this->ensureConnector();
148 $this->connector->cleanDb();
149 return [ 'message' => 'The database tables have been dropped' ];
150 }
151
152 /**
153 * Returns a data mapper by its name from the current database connector
154 *
155 * @param string $name Mapper name
156 *
157 * @return object
158 */
159 public function getMapper(string $name): object
160 {
161 $this->ensureConnector();
162 return $this->connector->getMapper($name);
163 }
164
165 /**
166 * Finds the connector of the specified database type and initializes it
167 * if it hasn't already been initialized
168 *
169 * @return void
170 */
171 private function ensureConnector(): void
172 {
173 if (!$this->connector) {
174 switch ($this->conf_data['type']) {
175 case 'mysql':
176 case 'mariadb':
177 $type = 'mariadb';
178 break;
179 default:
180 throw new RuntimeException('Unknown database type: ' . $this->conf_data['type']);
181 }
182 $c_name = __NAMESPACE__ . '\\' . \ucfirst($type) . '\\Connector';
183 $this->connector = new $c_name($this->conf_data);
184 }
185 }
186 }