3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Contact\FriendSuggest\Repository;
24 use Friendica\BaseRepository;
25 use Friendica\Contact\FriendSuggest\Collection;
26 use Friendica\Contact\FriendSuggest\Entity;
27 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestNotFoundException;
28 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestPersistenceException;
29 use Friendica\Contact\FriendSuggest\Factory;
30 use Friendica\Database\Database;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Psr\Log\LoggerInterface;
34 class FriendSuggest extends BaseRepository
36 /** @var Factory\FriendSuggest */
39 protected static $table_name = 'fsuggest';
41 public function __construct(Database $database, LoggerInterface $logger, Factory\FriendSuggest $factory)
43 parent::__construct($database, $logger, $factory);
46 private function convertToTableRow(Entity\FriendSuggest $fsuggest): array
49 'uid' => $fsuggest->uid,
50 'cid' => $fsuggest->cid,
51 'name' => $fsuggest->name,
52 'url' => $fsuggest->url,
53 'request' => $fsuggest->request,
54 'photo' => $fsuggest->photo,
55 'note' => $fsuggest->note,
60 * @param array $condition
61 * @param array $params
63 * @return Entity\FriendSuggest
65 * @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions
67 private function selectOne(array $condition, array $params = []): Entity\FriendSuggest
69 return parent::_selectOne($condition, $params);
73 * @param array $condition
74 * @param array $params
76 * @return Collection\FriendSuggests
80 private function select(array $condition, array $params = []): Collection\FriendSuggests
82 return parent::_select($condition, $params);
88 * @return Entity\FriendSuggest
90 * @throws FriendSuggestNotFoundException in case there's no suggestion for this id
92 public function selectOneById(int $id): Entity\FriendSuggest
95 return $this->selectOne(['id' => $id]);
96 } catch (NotFoundException $e) {
97 throw new FriendSuggestNotFoundException(sprintf('No FriendSuggest found for id %d', $id));
104 * @return Collection\FriendSuggests
106 * @throws FriendSuggestPersistenceException In case the underlying storage cannot select the suggestion
108 public function selectForContact(int $cid): Collection\FriendSuggests
111 return $this->select(['cid' => $cid]);
112 } catch (\Exception $e) {
113 throw new FriendSuggestPersistenceException(sprintf('Cannot select FriendSuggestion for contact %d', $cid));
118 * @param Entity\FriendSuggest $fsuggest
120 * @return Entity\FriendSuggest
122 * @throws FriendSuggestNotFoundException in case the underlying storage cannot save the suggestion
124 public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
127 $fields = $this->convertToTableRow($fsuggest);
130 $this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]);
131 return $this->factory->createFromTableRow($fields);
133 $this->db->insert(self::$table_name, $fields);
134 return $this->selectOneById($this->db->lastInsertId());
136 } catch (\Exception $exception) {
137 throw new FriendSuggestNotFoundException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
142 * @param Collection\FriendSuggest $fsuggests
146 * @throws FriendSuggestNotFoundException in case the underlying storage cannot delete the suggestion
148 public function delete(Collection\FriendSuggests $fsuggests): bool
151 $ids = $fsuggests->column('id');
152 return $this->db->delete(self::$table_name, ['id' => $ids]);
153 } catch (\Exception $exception) {
154 throw new FriendSuggestNotFoundException('Cannot delete the FriendSuggestions', $exception);