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