]> git.mxchange.org Git - friendica.git/blob - src/Core/PConfig/Repository/PConfig.php
Merge pull request #10918 from nupplaphil/feat/core_new_paradigm
[friendica.git] / src / Core / PConfig / Repository / PConfig.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\PConfig\Repository;
23
24 use Friendica\Core\Config\Util\ValueConversion;
25 use Friendica\Core\PConfig\Exception\PConfigPersistenceException;
26 use Friendica\Database\Database;
27
28 /**
29  * The Config model backend for users, which is using the general DB-model backend for user-configs
30  */
31 class PConfig
32 {
33         protected static $table_name = 'pconfig';
34
35         /** @var Database */
36         protected $db;
37
38         public function __construct(Database $db)
39         {
40                 $this->db = $db;
41         }
42
43         /**
44          * Checks if the model is currently connected
45          *
46          * @return bool
47          */
48         public function isConnected(): bool
49         {
50                 return $this->db->isConnected();
51         }
52
53         /**
54          * Loads all configuration values and returns the loaded category as an array.
55          *
56          * @param int         $uid The id of the user to load
57          * @param string|null $cat The category of the configuration values to load
58          *
59          * @return string[][] The config array
60          *
61          * @throws PConfigPersistenceException In case the persistence layer throws errors
62          */
63         public function load(int $uid, ?string $cat = null): array
64         {
65                 $return = [];
66
67                 try {
68                         if (empty($cat)) {
69                                 $configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['uid' => $uid]);
70                         } else {
71                                 $configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
72                         }
73
74                         while ($config = $this->db->fetch($configs)) {
75                                 $key   = $config['k'];
76                                 $value = ValueConversion::toConfigValue($config['v']);
77
78                                 // just save it in case it is set
79                                 if (isset($value)) {
80                                         $return[$config['cat']][$key] = $value;
81                                 }
82                         }
83                 } catch (\Exception $exception) {
84                         throw new PConfigPersistenceException(sprintf('Cannot load config category %s for user %d', $cat, $uid), $exception);
85                 } finally {
86                         $this->db->close($configs);
87                 }
88
89                 return $return;
90         }
91
92         /**
93          * Get a particular user config variable out of the DB with the
94          * given category name ($cat) and a key ($key).
95          *
96          * Note: Boolean variables are defined as 0/1 in the database
97          *
98          * @param int         $uid The id of the user to load
99          * @param string $cat The category of the configuration value
100          * @param string $key The configuration key to query
101          *
102          * @return array|string|null Stored value or null if it does not exist
103          *
104          * @throws PConfigPersistenceException In case the persistence layer throws errors
105          */
106         public function get(int $uid, string $cat, string $key)
107         {
108                 if (!$this->isConnected()) {
109                         return null;
110                 }
111
112                 try {
113                         $config = $this->db->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
114                         if ($this->db->isResult($config)) {
115                                 $value = ValueConversion::toConfigValue($config['v']);
116
117                                 // just return it in case it is set
118                                 if (isset($value)) {
119                                         return $value;
120                                 }
121                         }
122                 } catch (\Exception $exception) {
123                         throw new PConfigPersistenceException(sprintf('Cannot get config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
124                 }
125
126                 return null;
127         }
128
129         /**
130          * Stores a config value ($value) in the category ($cat) under the key ($key) for a
131          * given user ($uid).
132          *
133          * Note: Please do not store booleans - convert to 0/1 integer values!
134          *
135          * @param int    $uid   The id of the user to load
136          * @param string $cat   The category of the configuration value
137          * @param string $key   The configuration key to set
138          * @param mixed  $value The value to store
139          *
140          * @return bool Operation success
141          *
142          * @throws PConfigPersistenceException In case the persistence layer throws errors
143          */
144         public function set(int $uid, string $cat, string $key, $value): bool
145         {
146                 if (!$this->isConnected()) {
147                         return false;
148                 }
149
150                 // We store our setting values in a string variable.
151                 // So we have to do the conversion here so that the compare below works.
152                 // The exception are array values.
153                 $compare_value = (!is_array($value) ? (string)$value : $value);
154                 $stored_value  = $this->get($uid, $cat, $key);
155
156                 if (isset($stored_value) && ($stored_value === $compare_value)) {
157                         return true;
158                 }
159
160                 try {
161                         $dbValue = ValueConversion::toDbValue($value);
162                         return $this->db->update(static::$table_name, ['v' => $dbValue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
163                 } catch (\Exception $exception) {
164                         throw new PConfigPersistenceException(sprintf('Cannot set config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
165                 }
166         }
167
168         /**
169          * Removes the configured value of the given user.
170          *
171          * @param int    $uid The id of the user to load
172          * @param string $cat The category of the configuration value
173          * @param string $key The configuration key to delete
174          *
175          * @return bool Operation success
176          *
177          * @throws PConfigPersistenceException In case the persistence layer throws errors
178          */
179         public function delete(int $uid, string $cat, string $key): bool
180         {
181                 if (!$this->isConnected()) {
182                         return false;
183                 }
184
185                 try {
186                         return $this->db->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
187                 } catch (\Exception $exception) {
188                         throw new PConfigPersistenceException(sprintf('Cannot delete config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
189                 }
190         }
191 }