]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Storage/Database.php
Merge pull request #10860 from nupplaphil/feat/depository_profilefield
[friendica.git] / src / Model / Storage / Database.php
index 84dd116b526e834c4f2dd2bf6a193ff59bd2233f..7b90d878909221d8f2ac59d48e990c8cd0f51f08 100644 (file)
@@ -1,73 +1,94 @@
 <?php
 /**
- * @file src/Model/Storage/Filesystem.php
- * @brief Storage backend system
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
 
 namespace Friendica\Model\Storage;
 
-use Friendica\Core\L10n;
-use Psr\Log\LoggerInterface;
+use Exception;
+use Friendica\Database\Database as DBA;
 
 /**
- * @brief Database based storage system
+ * Database based storage system
  *
  * This class manage data stored in database table.
  */
-class Database implements IStorage
+class Database implements IWritableStorage
 {
        const NAME = 'Database';
 
-       /** @var \Friendica\Database\Database */
+       /** @var DBA */
        private $dba;
-       /** @var LoggerInterface */
-       private $logger;
-       /** @var L10n\L10n */
-       private $l10n;
 
        /**
-        * @param \Friendica\Database\Database $dba
-        * @param LoggerInterface              $logger
-        * @param L10n\L10n                    $l10n
+        * @param DBA             $dba
         */
-       public function __construct(\Friendica\Database\Database $dba, LoggerInterface $logger, L10n\L10n $l10n)
+       public function __construct(DBA $dba)
        {
-               $this->dba    = $dba;
-               $this->logger = $logger;
-               $this->l10n   = $l10n;
+               $this->dba = $dba;
        }
 
        /**
         * @inheritDoc
         */
-       public function get(string $reference)
+       public function get(string $reference): string
        {
-               $result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
-               if (!$this->dba->isResult($result)) {
-                       return '';
-               }
+               try {
+                       $result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
+                       if (!$this->dba->isResult($result)) {
+                               throw new ReferenceStorageException(sprintf('Database storage cannot find data for reference %s', $reference));
+                       }
 
-               return $result['data'];
+                       return $result['data'];
+               } catch (Exception $exception) {
+                       if ($exception instanceof ReferenceStorageException) {
+                               throw $exception;
+                       } else {
+                               throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
+                       }
+               }
        }
 
        /**
         * @inheritDoc
         */
-       public function put(string $data, string $reference = '')
+       public function put(string $data, string $reference = ''): string
        {
                if ($reference !== '') {
-                       $result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
+                       try {
+                               $result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
+                       } catch (Exception $exception) {
+                               throw new StorageException(sprintf('Database storage failed to update %s', $reference), $exception->getCode(), $exception);
+                       }
                        if ($result === false) {
-                               $this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' =>  $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
-                               throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
-                       }
+                               throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
+                       }
 
                        return $reference;
                } else {
-                       $result = $this->dba->insert('storage', ['data' => $data]);
+                       try {
+                               $result = $this->dba->insert('storage', ['data' => $data]);
+                       } catch (Exception $exception) {
+                               throw new StorageException(sprintf('Database storage failed to insert %s', $reference), $exception->getCode(), $exception);
+                       }
                        if ($result === false) {
-                               $this->logger->warning('Failed to insert data.', ['errorCode' =>  $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
-                               throw new StorageException($this->l10n->t('Database storage failed to insert data'));
+                               throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
                        }
 
                        return $this->dba->lastInsertId();
@@ -79,30 +100,29 @@ class Database implements IStorage
         */
        public function delete(string $reference)
        {
-               return $this->dba->delete('storage', ['id' => $reference]);
-       }
-
-       /**
-        * @inheritDoc
-        */
-       public function getOptions()
-       {
-               return [];
+               try {
+                       if (!$this->dba->delete('storage', ['id' => $reference]) || $this->dba->affectedRows() === 0) {
+                               throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
+                       }
+               } catch (Exception $exception) {
+                       if ($exception instanceof ReferenceStorageException) {
+                               throw $exception;
+                       } else {
+                               throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
+                       }
+               }
        }
 
        /**
         * @inheritDoc
         */
-       public function saveOptions(array $data)
+       public static function getName(): string
        {
-               return [];
+               return self::NAME;
        }
 
-       /**
-        * @inheritDoc
-        */
        public function __toString()
        {
-               return self::NAME;
+               return self::getName();
        }
 }