3 namespace Friendica\Contact\FriendSuggest\Repository;
5 use Friendica\BaseRepository;
6 use Friendica\Contact\FriendSuggest\Collection;
7 use Friendica\Contact\FriendSuggest\Entity;
8 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestNotFoundException;
9 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestPersistenceException;
10 use Friendica\Contact\FriendSuggest\Factory;
11 use Friendica\Database\Database;
12 use Friendica\Network\HTTPException\NotFoundException;
13 use Psr\Log\LoggerInterface;
15 class FriendSuggest extends BaseRepository
17 /** @var Factory\FriendSuggest */
20 protected static $table_name = 'fsuggest';
22 public function __construct(Database $database, LoggerInterface $logger, Factory\FriendSuggest $factory)
24 parent::__construct($database, $logger, $factory);
27 private function convertToTableRow(Entity\FriendSuggest $fsuggest): array
30 'uid' => $fsuggest->uid,
31 'cid' => $fsuggest->cid,
32 'name' => $fsuggest->name,
33 'url' => $fsuggest->url,
34 'request' => $fsuggest->request,
35 'photo' => $fsuggest->photo,
36 'note' => $fsuggest->note,
41 * @param array $condition
42 * @param array $params
44 * @return Entity\FriendSuggest
46 * @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions
48 private function selectOne(array $condition, array $params = []): Entity\FriendSuggest
50 return parent::_selectOne($condition, $params);
54 * @param array $condition
55 * @param array $params
57 * @return Collection\FriendSuggests
61 private function select(array $condition, array $params = []): Collection\FriendSuggests
63 return parent::_select($condition, $params);
69 * @return Entity\FriendSuggest
71 * @throws FriendSuggestNotFoundException in case there's no suggestion for this id
73 public function selectOneById(int $id): Entity\FriendSuggest
76 return $this->selectOne(['id' => $id]);
77 } catch (NotFoundException $e) {
78 throw new FriendSuggestNotFoundException(sprintf('No FriendSuggest found for id %d', $id));
85 * @return Collection\FriendSuggests
87 * @throws FriendSuggestPersistenceException In case the underlying storage cannot select the suggestion
89 public function selectForContact(int $cid): Collection\FriendSuggests
92 return $this->select(['cid' => $cid]);
93 } catch (\Exception $e) {
94 throw new FriendSuggestPersistenceException(sprintf('Cannot select FriendSuggestion for contact %d', $cid));
99 * @param Entity\FriendSuggest $fsuggest
101 * @return Entity\FriendSuggest
103 * @throws FriendSuggestNotFoundException in case the underlying storage cannot save the suggestion
105 public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
108 $fields = $this->convertToTableRow($fsuggest);
111 $this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]);
112 return $this->factory->createFromTableRow($fields);
114 $this->db->insert(self::$table_name, $fields);
115 return $this->selectOneById($this->db->lastInsertId());
117 } catch (\Exception $exception) {
118 throw new FriendSuggestNotFoundException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
123 * @param Collection\FriendSuggest $fsuggests
127 * @throws FriendSuggestNotFoundException in case the underlying storage cannot delete the suggestion
129 public function delete(Collection\FriendSuggests $fsuggests): bool
132 $ids = $fsuggests->column('id');
133 return $this->db->delete(self::$table_name, ['id' => $ids]);
134 } catch (\Exception $exception) {
135 throw new FriendSuggestNotFoundException('Cannot delete the FriendSuggestions', $exception);