. * * ========================= * * This file contains the DatabaseController class * * @category API * @package DmarcSrg * @author Aleksey Andreev (liuch) * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3 */ namespace Liuch\DmarcSrg\Database; use Liuch\DmarcSrg\Exception\RuntimeException; use Liuch\DmarcSrg\Exception\DatabaseFatalException; /** * Proxy class for accessing a database of the selected type */ class DatabaseController { public const REQUIRED_VERSION = '3.0'; private $conf_data = null; private $connector = null; /** * The constructor * * @param Core $core Instace of the Core class * @param class $connector The connector class of the current database */ public function __construct($core, $connector = null) { $this->conf_data = $core->config('database'); $this->connector = $connector; } /** * Returns the database type * * @return string */ public function type(): string { return $this->conf_data['type'] ?? ''; } /** * Returns the database name * * @return string */ public function name(): string { return $this->conf_data['name'] ?? ''; } /** * Returns the database host * * @return string */ public function location(): string { return $this->conf_data['host'] ?? ''; } /** * Returns information about the database as an array. * * @return array May contain the following fields: * `tables` - an array of tables with their properties; * `needs_upgrade` - true if the database needs upgrading; * `correct` - true if the database is correct; * `type` - the database type; * `name` - the database name; * `location` - the database location; * `version` - the current version of the database structure; * `message` - a state message; * `error_code` - an error code; */ public function state(): array { $this->ensureConnector(); $res = $this->connector->state(); $res['type'] = $this->type(); $res['name'] = $this->name(); $res['location'] = $this->location(); if (($res['correct'] ?? false) && ($res['version'] ?? 'null') !== self::REQUIRED_VERSION) { $res['correct'] = false; $res['message'] = 'The database structure needs upgrading'; $res['needs_upgrade'] = true; } return $res; } /** * Inites the database. * * This method creates needed tables and indexes in the database. * The method will fail if the database already have tables with the table prefix. * * @return array Result array with `error_code` and `message` fields. */ public function initDb(): array { $this->ensureConnector(); $this->connector->initDb(self::REQUIRED_VERSION); return [ 'message' => 'The database has been initiated' ]; } /** * Cleans up the database. * * Drops tables with the table prefix in the database or all tables in the database if no table prefix is set. * * @return array Result array with `error_code` and `message` fields. */ public function cleanDb(): array { $this->ensureConnector(); $this->connector->cleanDb(); return [ 'message' => 'The database tables have been dropped' ]; } /** * Returns a data mapper by its name from the current database connector * * @param string $name Mapper name * * @return object */ public function getMapper(string $name): object { $this->ensureConnector(); return $this->connector->getMapper($name); } /** * Finds the connector of the specified database type and initializes it * if it hasn't already been initialized * * @return void */ private function ensureConnector(): void { if (!$this->connector) { switch ($this->conf_data['type']) { case 'mysql': case 'mariadb': $type = 'mariadb'; break; default: throw new RuntimeException('Unknown database type: ' . $this->conf_data['type']); $type = null; break; } $c_name = __NAMESPACE__ . '\\' . \ucfirst($type) . '\\Connector'; $this->connector = new $c_name($this->conf_data); } } }