]> git.mxchange.org Git - friendica.git/blob - src/Model/Storage/Database.php
Merge pull request #10283 from very-ape/fix-message-button
[friendica.git] / src / Model / Storage / Database.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\Model\Storage;
23
24 use Friendica\Core\L10n;
25 use Psr\Log\LoggerInterface;
26 use Friendica\Database\Database as DBA;
27
28 /**
29  * Database based storage system
30  *
31  * This class manage data stored in database table.
32  */
33 class Database extends AbstractStorage
34 {
35         const NAME = 'Database';
36
37         /** @var DBA */
38         private $dba;
39
40         /**
41          * @param DBA             $dba
42          * @param LoggerInterface $logger
43          * @param L10n            $l10n
44          */
45         public function __construct(DBA $dba, LoggerInterface $logger, L10n $l10n)
46         {
47                 parent::__construct($l10n, $logger);
48
49                 $this->dba = $dba;
50         }
51
52         /**
53          * @inheritDoc
54          */
55         public function get(string $reference)
56         {
57                 $result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
58                 if (!$this->dba->isResult($result)) {
59                         return '';
60                 }
61
62                 return $result['data'];
63         }
64
65         /**
66          * @inheritDoc
67          */
68         public function put(string $data, string $reference = '')
69         {
70                 if ($reference !== '') {
71                         $result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
72                         if ($result === false) {
73                                 $this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
74                                 throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
75                         }
76
77                         return $reference;
78                 } else {
79                         $result = $this->dba->insert('storage', ['data' => $data]);
80                         if ($result === false) {
81                                 $this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
82                                 throw new StorageException($this->l10n->t('Database storage failed to insert data'));
83                         }
84
85                         return $this->dba->lastInsertId();
86                 }
87         }
88
89         /**
90          * @inheritDoc
91          */
92         public function delete(string $reference)
93         {
94                 return $this->dba->delete('storage', ['id' => $reference]);
95         }
96
97         /**
98          * @inheritDoc
99          */
100         public function getOptions()
101         {
102                 return [];
103         }
104
105         /**
106          * @inheritDoc
107          */
108         public function saveOptions(array $data)
109         {
110                 return [];
111         }
112
113         /**
114          * @inheritDoc
115          */
116         public static function getName()
117         {
118                 return self::NAME;
119         }
120 }