Debian Codegraph @

classes/Database/Common/CommonSettingMapper.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 CommonSettingMapper 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\Common;
33
34 use Liuch\DmarcSrg\Database\SettingMapperInterface;
35 use Liuch\DmarcSrg\Exception\DatabaseFatalException;
36 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
37
38 /**
39 * Universal implementation of SettingMapper class
40 */
41 class CommonSettingMapper implements SettingMapperInterface
42 {
43 /** @var \Liuch\DmarcSrg\Database\DatabaseConnector */
44 private $connector = null;
45
46 /**
47 * The constructor
48 *
49 * @param \Liuch\DmarcSrg\Database\DatabaseConnector $connector DatabaseConnector instance of the current database
50 */
51 public function __construct(object $connector)
52 {
53 $this->connector = $connector;
54 }
55
56 /**
57 * Returns setting value as a string by key
58 *
59 * The method contains a workaround to update the database structure from v3.2 to v4.0.
60 * The method does two attempts to execute the query: with and without the user_id field.
61 * It needs to be fixed in the future.
62 *
63 * @param string $key
64 * @param int $user_id
65 *
66 * @return string
67 */
68 public function value(string $key, int $user_id): string
69 {
70 $db = $this->connector->dbh();
71 $sn = $this->connector->tablePrefix('system');
72 $tr = [
73 [
74 "SELECT value FROM \"{$sn}\" WHERE user_id = ? AND \"key\" = ?",
75 [ [ 1, $user_id, \PDO::PARAM_INT ], [ 2, $key, \PDO::PARAM_STR ] ],
76 ],
77 [
78 "SELECT value FROM \"{$sn}\" WHERE \"key\" = ?",
79 [ [ 1, $key, \PDO::PARAM_STR ] ],
80 ]
81 ];
82 try {
83 for ($i = 0; $i < 2; ++$i) {
84 $it = $tr[$i];
85 $st = $db->prepare($it[0]);
86 foreach ($it[1] as &$b) {
87 $st->bindValue($b[0], $b[1], $b[2]);
88 }
89 unset($b);
90 try {
91 $st->execute();
92 if (!($res = $st->fetch(\PDO::FETCH_NUM))) {
93 throw new DatabaseNotFoundException('Setting not found: ' . $key);
94 }
95 $st->closeCursor();
96 return $res[0];
97 } catch (\PDOException $e) {
98 if ($i !== 0 || $user_id !== 0 || !str_contains($e->getMessage(), 'user_id')) {
99 throw $e;
100 }
101 }
102 }
103 } catch (\PDOException $e) {
104 throw new DatabaseFatalException('Failed to get a setting', -1, $e);
105 }
106 }
107
108 /**
109 * Returns a key-value array of the setting list like this:
110 * [ 'name1' => 'value1', 'name2' => 'value2' ]
111 *
112 * @param int $user_id User Id to get settings for
113 *
114 * @return array
115 */
116 public function list(int $user_id): array
117 {
118 $res = [];
119 try {
120 $st = $this->connector->dbh()->prepare(
121 'SELECT "key", value FROM "' . $this->connector->tablePrefix('system')
122 . '" WHERE user_id = ? ORDER BY "key"'
123 );
124 $st->bindValue(1, $user_id, \PDO::PARAM_INT);
125 $st->execute();
126 while ($row = $st->fetch(\PDO::FETCH_NUM)) {
127 $res[$row[0]] = $row[1];
128 }
129 $st->closeCursor();
130 } catch (\PDOException $e) {
131 throw new DatabaseFatalException('Failed to get a list of the settings', -1, $e);
132 }
133 return $res;
134 }
135
136 /**
137 * Saves the setting to the database
138 *
139 * Updates the value of the setting in the database if the setting exists there or insert a new record otherwise.
140 *
141 * @param string $name Setting name
142 * @param string $value Setting value
143 * @param int $user_id User Id to save the setting for
144 *
145 * @return void
146 */
147 public function save(string $name, string $value, int $user_id): void
148 {
149 $db = $this->connector->dbh();
150 try {
151 $st = $db->prepare(
152 'INSERT INTO "' . $this->connector->tablePrefix('system') .
153 '" ("key", user_id, value) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE value = ?'
154 );
155 $st->bindValue(1, $name, \PDO::PARAM_STR);
156 $st->bindValue(2, $user_id, \PDO::PARAM_INT);
157 $st->bindValue(3, $value, \PDO::PARAM_STR);
158 $st->bindValue(4, $value, \PDO::PARAM_STR);
159 $st->execute();
160 $st->closeCursor();
161 } catch (\PDOException $e) {
162 throw new DatabaseFatalException('Failed to update a setting', -1, $e);
163 }
164 }
165 }