]> git.mxchange.org Git - friendica.git/blob - src/Core/Storage/Type/Database.php
Merge pull request #11015 from MrPetovan/task/10979-frio-time-tooltip
[friendica.git] / src / Core / Storage / Type / 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\Core\Storage\Type;
23
24 use Exception;
25 use Friendica\Core\Storage\Exception\ReferenceStorageException;
26 use Friendica\Core\Storage\Exception\StorageException;
27 use Friendica\Core\Storage\Capability\ICanWriteToStorage;
28 use Friendica\Database\Database as DBA;
29
30 /**
31  * Database based storage system
32  *
33  * This class manage data stored in database table.
34  */
35 class Database implements ICanWriteToStorage
36 {
37         const NAME = 'Database';
38
39         /** @var DBA */
40         private $dba;
41
42         /**
43          * @param DBA             $dba
44          */
45         public function __construct(DBA $dba)
46         {
47                 $this->dba = $dba;
48         }
49
50         /**
51          * @inheritDoc
52          */
53         public function get(string $reference): string
54         {
55                 try {
56                         $result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
57                         if (!$this->dba->isResult($result)) {
58                                 throw new ReferenceStorageException(sprintf('Database storage cannot find data for reference %s', $reference));
59                         }
60
61                         return $result['data'];
62                 } catch (Exception $exception) {
63                         if ($exception instanceof ReferenceStorageException) {
64                                 throw $exception;
65                         } else {
66                                 throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
67                         }
68                 }
69         }
70
71         /**
72          * @inheritDoc
73          */
74         public function put(string $data, string $reference = ''): string
75         {
76                 if ($reference !== '') {
77                         try {
78                                 $result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
79                         } catch (Exception $exception) {
80                                 throw new StorageException(sprintf('Database storage failed to update %s', $reference), $exception->getCode(), $exception);
81                         }
82                         if ($result === false) {
83                                 throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
84                         }
85
86                         return $reference;
87                 } else {
88                         try {
89                                 $result = $this->dba->insert('storage', ['data' => $data]);
90                         } catch (Exception $exception) {
91                                 throw new StorageException(sprintf('Database storage failed to insert %s', $reference), $exception->getCode(), $exception);
92                         }
93                         if ($result === false) {
94                                 throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
95                         }
96
97                         return $this->dba->lastInsertId();
98                 }
99         }
100
101         /**
102          * @inheritDoc
103          */
104         public function delete(string $reference)
105         {
106                 try {
107                         if (!$this->dba->delete('storage', ['id' => $reference]) || $this->dba->affectedRows() === 0) {
108                                 throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
109                         }
110                 } catch (Exception $exception) {
111                         if ($exception instanceof ReferenceStorageException) {
112                                 throw $exception;
113                         } else {
114                                 throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
115                         }
116                 }
117         }
118
119         /**
120          * @inheritDoc
121          */
122         public static function getName(): string
123         {
124                 return self::NAME;
125         }
126
127         public function __toString(): string
128         {
129                 return self::getName();
130         }
131 }