]> 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 c7eb3628cf97e0c87b40cc4bced22fbb72dc40d0..7b90d878909221d8f2ac59d48e990c8cd0f51f08 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -21,8 +21,7 @@
 
 namespace Friendica\Model\Storage;
 
-use Friendica\Core\L10n;
-use Psr\Log\LoggerInterface;
+use Exception;
 use Friendica\Database\Database as DBA;
 
 /**
@@ -30,7 +29,7 @@ use Friendica\Database\Database as DBA;
  *
  * This class manage data stored in database table.
  */
-class Database extends AbstractStorage
+class Database implements IWritableStorage
 {
        const NAME = 'Database';
 
@@ -39,47 +38,57 @@ class Database extends AbstractStorage
 
        /**
         * @param DBA             $dba
-        * @param LoggerInterface $logger
-        * @param L10n            $l10n
         */
-       public function __construct(DBA $dba, LoggerInterface $logger, L10n $l10n)
+       public function __construct(DBA $dba)
        {
-               parent::__construct($l10n, $logger);
-
                $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();
@@ -91,30 +100,29 @@ class Database extends AbstractStorage
         */
        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 static function getName()
+       public function __toString()
        {
-               return self::NAME;
+               return self::getName();
        }
 }