]> git.mxchange.org Git - friendica.git/commitdiff
Replace all 'fsuggest' usages with the new paradigm
authorPhilipp <admin@philipp.info>
Thu, 21 Oct 2021 21:13:19 +0000 (23:13 +0200)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 24 Oct 2021 01:45:35 +0000 (21:45 -0400)
src/Contact/FriendSuggest/Collection/FriendSuggests.php [new file with mode: 0644]
src/Contact/FriendSuggest/Depository/FriendSuggest.php
src/Contact/FriendSuggest/Exception/FriendSuggestNotFoundException.php [new file with mode: 0644]
src/Contact/FriendSuggest/Factory/FriendSuggest.php
src/Worker/Contact/RemoveContent.php
src/Worker/Delivery.php

diff --git a/src/Contact/FriendSuggest/Collection/FriendSuggests.php b/src/Contact/FriendSuggest/Collection/FriendSuggests.php
new file mode 100644 (file)
index 0000000..ad382b3
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+namespace Friendica\Contact\FriendSuggest\Collection;
+
+use Friendica\BaseCollection;
+
+class FriendSuggests extends BaseCollection
+{
+}
index 9f3b39accab705f93005c02400d5282f8abde651..76f96bdf04132aa287135ef332b08f48f88b36d1 100644 (file)
@@ -2,10 +2,13 @@
 
 namespace Friendica\Contact\FriendSuggest\Depository;
 
+use Friendica\BaseCollection;
 use Friendica\BaseDepository;
+use Friendica\Contact\FriendSuggest\Collection;
+use Friendica\Contact\FriendSuggest\Entity;
+use Friendica\Contact\FriendSuggest\Exception\FriendSuggestNotFoundException;
 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestPersistenceException;
 use Friendica\Contact\FriendSuggest\Factory;
-use Friendica\Contact\FriendSuggest\Entity;
 use Friendica\Database\Database;
 use Friendica\Network\HTTPException\NotFoundException;
 use Psr\Log\LoggerInterface;
@@ -48,11 +51,58 @@ class FriendSuggest extends BaseDepository
                return parent::_selectOne($condition, $params);
        }
 
+       /**
+        * @param array $condition
+        * @param array $params
+        *
+        * @return Collection\FriendSuggests
+        *
+        * @throws \Exception
+        */
+       private function select(array $condition, array $params = []): Collection\FriendSuggests
+       {
+               return parent::_select($condition, $params);
+       }
+
+       /**
+        * @param int $id
+        *
+        * @return Entity\FriendSuggest
+        *
+        * @throws FriendSuggestNotFoundException in case there's no suggestion for this id
+        */
        public function selectOneById(int $id): Entity\FriendSuggest
        {
-               return $this->selectOne(['id' => $id]);
+               try {
+                       return $this->selectOne(['id' => $id]);
+               } catch (NotFoundException $e) {
+                       throw new FriendSuggestNotFoundException(sprintf('No FriendSuggest found for id %d', $id));
+               }
+       }
+
+       /**
+        * @param int $cid
+        *
+        * @return Collection\FriendSuggests
+        *
+        * @throws FriendSuggestPersistenceException In case the underlying storage cannot select the suggestion
+        */
+       public function selectForContact(int $cid): Collection\FriendSuggests
+       {
+               try {
+                       return $this->select(['cid' => $cid]);
+               } catch (\Exception $e) {
+                       throw new FriendSuggestPersistenceException(sprintf('Cannot select FriendSuggestion for contact %d', $cid));
+               }
        }
 
+       /**
+        * @param Entity\FriendSuggest $fsuggest
+        *
+        * @return Entity\FriendSuggest
+        *
+        * @throws FriendSuggestNotFoundException in case the underlying storage cannot save the suggestion
+        */
        public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
        {
                try {
@@ -66,7 +116,24 @@ class FriendSuggest extends BaseDepository
                                return $this->selectOneById($this->db->lastInsertId());
                        }
                } catch (\Exception $exception) {
-                       throw new FriendSuggestPersistenceException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
+                       throw new FriendSuggestNotFoundException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
+               }
+       }
+
+       /**
+        * @param Collection\FriendSuggest $fsuggests
+        *
+        * @return bool
+        *
+        * @throws FriendSuggestNotFoundException in case the underlying storage cannot delete the suggestion
+        */
+       public function delete(Collection\FriendSuggests $fsuggests): bool
+       {
+               try {
+                       $ids = $fsuggests->column('id');
+                       return $this->db->delete(self::$table_name, ['id' => $ids]);
+               } catch (\Exception $exception) {
+                       throw new FriendSuggestNotFoundException('Cannot delete the FriendSuggestions', $exception);
                }
        }
 }
diff --git a/src/Contact/FriendSuggest/Exception/FriendSuggestNotFoundException.php b/src/Contact/FriendSuggest/Exception/FriendSuggestNotFoundException.php
new file mode 100644 (file)
index 0000000..e5ba7e1
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+namespace Friendica\Contact\FriendSuggest\Exception;
+
+class FriendSuggestNotFoundException extends \OutOfBoundsException
+{
+       public function __construct($message = "", \Throwable $previous = null)
+       {
+               parent::__construct($message, 404, $previous);
+       }
+}
index 80dc199903cb394b67b39810d82e99c1690c3ab9..1a3660e856368ae43210e1b6e3383ef23ab25903 100644 (file)
@@ -34,8 +34,7 @@ class FriendSuggest extends BaseFactory implements ICanCreateFromTableRow
                string $request = '',
                string $photo = '',
                string $note = ''
-       ): Entity\FriendSuggest
-       {
+       ): Entity\FriendSuggest {
                return $this->createFromTableRow([
                        'uid'     => $uid,
                        'cid'     => $cid,
@@ -46,4 +45,9 @@ class FriendSuggest extends BaseFactory implements ICanCreateFromTableRow
                        'note'    => $note,
                ]);
        }
+
+       public function createEmpty(int $id): Entity\FriendSuggest
+       {
+               return $this->createFromTableRow(['id' => $id]);
+       }
 }
index f0e6e631d798b4be5f2afaf09e1669bf5e76c365..cdb32141120d8c7624e4cd8876aa21eaacf6d96e 100644 (file)
@@ -80,7 +80,7 @@ class RemoveContent
                DBA::delete('contact-relation', ['relation-cid' => $id]);
                DBA::delete('contact-relation', ['cid' => $id]);
                DBA::delete('event', ['cid' => $id]);
-               DBA::delete('fsuggest', ['cid' => $id]);
+               DI::fsuggest()->delete(DI::fsuggest()->selectForContact($id));
                DBA::delete('post-tag', ['cid' => $id]);
                DBA::delete('user-contact', ['cid' => $id]);
 
index 8961b3f13d71d935f8947b05204117fe28a1993e..44fded2c5395b731e2516d4f4cccd08a93aef3eb 100644 (file)
@@ -21,6 +21,8 @@
 
 namespace Friendica\Worker;
 
+use Friendica\Contact\FriendSuggest\Collection\FriendSuggests;
+use Friendica\Contact\FriendSuggest\Exception\FriendSuggestNotFoundException;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\DBA;
@@ -64,11 +66,13 @@ class Delivery
                        }
                        $uid = $target_item['uid'];
                } elseif ($cmd == self::SUGGESTION) {
-                       $target_item = DBA::selectFirst('fsuggest', [], ['id' => $post_uriid]);
-                       if (!DBA::isResult($target_item)) {
+                       try {
+                               $target_item = DI::fsuggest()->selectOneById($post_uriid);
+                       } catch (FriendSuggestNotFoundException $e) {
+                               DI::logger()->info('Cannot find FriendSuggestion', ['id' => $post_uriid]);
                                return;
                        }
-                       $uid = $target_item['uid'];
+                       $uid = $target_item->uid;
                } elseif ($cmd == self::RELOCATION) {
                        $uid = $post_uriid;
                        $target_item = [];
@@ -284,7 +288,7 @@ class Delivery
                } elseif ($cmd == self::SUGGESTION) {
                        $item = $target_item;
                        $atom = DFRN::fsuggest($item, $owner);
-                       DBA::delete('fsuggest', ['id' => $item['id']]);
+                       DI::fsuggest()->delete(new FriendSuggests([DI::fsuggest()->selectOneById($item['id'])]));
                } elseif ($cmd == self::RELOCATION) {
                        $atom = DFRN::relocate($owner, $owner['uid']);
                } elseif ($followup) {