Debian Codegraph @

classes/Database/DatabaseConnector.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-2025 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 abstract DatabaseConnector 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\LogicException;
35
36 abstract class DatabaseConnector
37 {
38 protected static $names = [
39 'domain' => 'DomainMapper',
40 'host' => 'HostMapper',
41 'report' => 'ReportMapper',
42 'report-log' => 'ReportLogMapper',
43 'setting' => 'SettingMapper',
44 'statistics' => 'StatisticsMapper',
45 'upgrader' => 'UpgraderMapper',
46 'user' => 'UserMapper'
47 ];
48
49 protected $host = null;
50 protected $name = null;
51 protected $user = null;
52 protected $password = null;
53 protected $prefix = '';
54 protected $mappers = [];
55
56 /**
57 * The constructor
58 *
59 * @param array $conf Configuration data from the conf.php file
60 */
61 public function __construct(array $conf)
62 {
63 $this->host = $conf['host'] ?? '';
64 $this->name = $conf['name'] ?? '';
65 $this->user = $conf['user'] ?? '';
66 $this->password = $conf['password'] ?? '';
67 $this->prefix = $conf['table_prefix'] ?? '';
68 }
69
70 /**
71 * Returns an instance of PDO class
72 *
73 * @return \PDO
74 */
75 abstract public function dbh(): object;
76
77 /**
78 * Returns the database state as an array
79 *
80 * @return array
81 */
82 abstract public function state(): array;
83
84 /**
85 * Returns a data mapper by its name.
86 *
87 * @param string $name Mapper name
88 *
89 * @return object
90 */
91 public function getMapper(string $name): object
92 {
93 if (isset($this->mappers[$name])) {
94 return $this->mappers[$name];
95 }
96
97 if (!isset(self::$names[$name])) {
98 throw new LogicException('Unknown mapper name: ' . $name);
99 }
100
101 $reflection = new \ReflectionClass($this);
102 $mapper_name = $reflection->getNamespaceName() . '\\' . self::$names[$name];
103 if (!class_exists($mapper_name)) {
104 $reflection = $reflection->getParentClass();
105 if ($reflection) {
106 $mapper_name = $reflection->getNamespaceName() . '\\Common\\' . 'Common' . self::$names[$name];
107 }
108 }
109 $mapper = new $mapper_name($this);
110 $this->mappers[$name] = $mapper;
111 return $mapper;
112 }
113
114 /**
115 * Initiates the database.
116 *
117 * @return void
118 */
119 abstract public function initDb(string $version): void;
120
121 /**
122 * Cleans up the database
123 *
124 * @return void
125 */
126 abstract public function cleanDb(): void;
127
128 /**
129 * Returns the prefix for tables of the database
130 *
131 * @param string $postfix String to be concatenated with the prefix.
132 * Usually, this is a table name.
133 *
134 * @return string
135 */
136 public function tablePrefix(string $postfix = ''): string
137 {
138 return $this->prefix . $postfix;
139 }
140 }