Debian Codegraph @

classes/Settings/Setting.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) 2021-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 implementation of the class Setting
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\Settings;
33
34 use Liuch\DmarcSrg\Core;
35 use Liuch\DmarcSrg\Users\User;
36 use Liuch\DmarcSrg\Exception\SoftException;
37 use Liuch\DmarcSrg\Exception\DatabaseNotFoundException;
38
39 /**
40 * It's a class for accessing to settings item data
41 *
42 * This class is designed for storing and manipulating one item of settings data.
43 * All queries to the database are made in lazy mode.
44 */
45 abstract class Setting
46 {
47 public const TYPE_STRING = 1;
48 public const TYPE_INTEGER = 2;
49 public const TYPE_STRING_SELECT = 3;
50
51 protected $core = null;
52 protected $name = null;
53 protected $user = null;
54 protected $value = null;
55 protected $wignore = false;
56
57 /**
58 * Returns the type of the setting
59 *
60 * @return int Type of the setting
61 */
62 abstract public function type(): int;
63
64 /**
65 * Checks if the value is correct
66 *
67 * @return bool True if the value is correct or false otherwise
68 */
69 abstract protected function checkValue(): bool;
70
71 /**
72 * Converts a string to the value
73 *
74 * @param string $s String for conversion
75 *
76 * @return void
77 */
78 abstract protected function stringToValue(string $s): void;
79
80 /**
81 * Returns a string representation of the value
82 *
83 * @return string The string value
84 */
85 abstract protected function valueToString(): string;
86
87 /**
88 * It's a constructor of the class
89 *
90 * Some examples of using:
91 * (new Setting('some.setting'))->value(); - will return the value of the setting 'some.setting'.
92 * (new Setting([ 'name' => 'some.setting', 'value' => 'some string value' ])->save(); - will add
93 * this setting to the database if it does not exist in it or update the value of the setting.
94 *
95 * @param string|array $data Some setting data to identify it
96 * string value is treated as a name
97 * array has these fields: `name`, `value`, `user`
98 * and usually uses for creating a new setting item.
99 * @param boolean $wignore If true the wrong value is reset to the default
100 * or it throws an exception otherwise.
101 * @param Core|null $core Instance of the Core class
102 *
103 * @return void
104 */
105 public function __construct($data, bool $wignore = false, $core = null)
106 {
107 $this->wignore = $wignore;
108 $this->core = $core ?? Core::instance();
109 switch (gettype($data)) {
110 case 'string':
111 $this->name = $data;
112 SettingsList::checkName($this->name);
113 return;
114 case 'array':
115 if (!isset($data['name']) || gettype($data['name']) !== 'string') {
116 break;
117 }
118 $this->name = $data['name'];
119 SettingsList::checkName($this->name);
120 if (isset($data['value'])) {
121 $this->value = $data['value'];
122 if (!$this->checkValue()) {
123 if (!$wignore) {
124 break;
125 }
126 $this->resetToDefault();
127 }
128 }
129 if (isset($data['user'])) {
130 if (!($data['user'] instanceof User)) {
131 break;
132 }
133 $this->user = $data['user'];
134 }
135 return;
136 }
137 throw new SoftException('Wrong setting data');
138 }
139
140 /**
141 * Returns the name of the setting
142 *
143 * @return string The name of the setting
144 */
145 public function name(): string
146 {
147 return $this->name;
148 }
149
150 /**
151 * Returns the value of the setting
152 *
153 * @return mixed The value of the setting
154 */
155 public function value()
156 {
157 if (is_null($this->value)) {
158 $this->fetchData();
159 }
160 return $this->value;
161 }
162
163 /**
164 * Assigns the passed value to the setting
165 *
166 * @param mixed $value Value to assign
167 *
168 * @return void
169 */
170 public function setValue($value): void
171 {
172 $this->value = $value;
173 if (!$this->checkValue()) {
174 if (!$this->wignore) {
175 throw new SoftException('Wrong setting value');
176 }
177 $this->resetToDefault();
178 }
179 }
180
181 /**
182 * Returns an array with setting data
183 *
184 * @return array Setting data
185 */
186 public function toArray(): array
187 {
188 if (is_null($this->value)) {
189 $this->fetchData();
190 }
191 $sdef = SettingsList::$schema[$this->name]['default'];
192 switch ($this->type()) {
193 case self::TYPE_STRING:
194 $type = 'string';
195 break;
196 case self::TYPE_INTEGER:
197 $type = 'integer';
198 $sdef = intval($sdef);
199 break;
200 case self::TYPE_STRING_SELECT:
201 $type = 'select';
202 break;
203 default:
204 $type = '';
205 $sdef = '';
206 break;
207 }
208 return [
209 'type' => $type,
210 'name' => $this->name,
211 'value' => $this->value,
212 'default' => $sdef
213 ];
214 }
215
216 /**
217 * Saves the setting to the database
218 *
219 * Updates the value of the setting in the database if the setting exists there or insert a new record otherwise.
220 *
221 * @return void
222 */
223 public function save(): void
224 {
225 $this->core->database()->getMapper('setting')->save(
226 $this->name,
227 $this->valueToString(),
228 ($this->user ?? $this->core->getCurrentUser())->id()
229 );
230 }
231
232 /**
233 * Fetches the setting data from the database by its name
234 *
235 * @return void
236 */
237 private function fetchData(): void
238 {
239 try {
240 $res = $this->core->database()->getMapper('setting')->value(
241 $this->name,
242 ($this->user ?? $this->core->getCurrentUser())->id()
243 );
244 } catch (DatabaseNotFoundException $e) {
245 $this->resetToDefault();
246 return;
247 }
248 $this->stringToValue($res);
249 if (!$this->checkValue()) {
250 $this->resetToDefault();
251 }
252 }
253
254 /**
255 * Resets the setting value to its default value
256 *
257 * @return void
258 */
259 private function resetToDefault(): void
260 {
261 $this->stringToValue(SettingsList::$schema[$this->name]['default']);
262 }
263 }