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 Friendica\Util\DateTimeFormat;
33 use Psr\Log\LoggerInterface;
35 class FriendSuggest extends BaseRepository
37 /** @var Factory\FriendSuggest */
40 protected static $table_name = 'fsuggest';
42 public function __construct(Database $database, LoggerInterface $logger, Factory\FriendSuggest $factory)
44 parent::__construct($database, $logger, $factory);
47 private function convertToTableRow(Entity\FriendSuggest $fsuggest): array
50 'uid' => $fsuggest->uid,
51 'cid' => $fsuggest->cid,
52 'name' => $fsuggest->name,
53 'url' => $fsuggest->url,
54 'request' => $fsuggest->request,
55 'photo' => $fsuggest->photo,
56 'note' => $fsuggest->note,
57 'created' => $fsuggest->created->format(DateTimeFormat::MYSQL),
62 * @param array $condition
63 * @param array $params
65 * @return Entity\FriendSuggest
67 * @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions
69 private function selectOne(array $condition, array $params = []): Entity\FriendSuggest
71 return parent::_selectOne($condition, $params);
75 * @param array $condition
76 * @param array $params
78 * @return Collection\FriendSuggests
82 private function select(array $condition, array $params = []): Collection\FriendSuggests
84 return new Collection\FriendSuggests(parent::_select($condition, $params)->getArrayCopy());
90 * @return Entity\FriendSuggest
92 * @throws FriendSuggestNotFoundException in case there's no suggestion for this id
94 public function selectOneById(int $id): Entity\FriendSuggest
97 return $this->selectOne(['id' => $id]);
98 } catch (NotFoundException $e) {
99 throw new FriendSuggestNotFoundException(sprintf('No FriendSuggest found for id %d', $id));
106 * @return Collection\FriendSuggests
108 * @throws FriendSuggestPersistenceException In case the underlying storage cannot select the suggestion
110 public function selectForContact(int $cid): Collection\FriendSuggests
113 return $this->select(['cid' => $cid]);
114 } catch (\Exception $e) {
115 throw new FriendSuggestPersistenceException(sprintf('Cannot select FriendSuggestion for contact %d', $cid));
120 * @param Entity\FriendSuggest $fsuggest
122 * @return Entity\FriendSuggest
124 * @throws FriendSuggestNotFoundException in case the underlying storage cannot save the suggestion
126 public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
129 $fields = $this->convertToTableRow($fsuggest);
132 $this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]);
133 return $this->selectOneById($fsuggest->id);
135 $this->db->insert(self::$table_name, $fields);
136 return $this->selectOneById($this->db->lastInsertId());
138 } catch (\Exception $exception) {
139 throw new FriendSuggestNotFoundException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
144 * @param Collection\FriendSuggests $fsuggests
148 * @throws FriendSuggestNotFoundException in case the underlying storage cannot delete the suggestion
150 public function delete(Collection\FriendSuggests $fsuggests): bool
153 $ids = $fsuggests->column('id');
154 return $this->db->delete(self::$table_name, ['id' => $ids]);
155 } catch (\Exception $exception) {
156 throw new FriendSuggestNotFoundException('Cannot delete the FriendSuggestions', $exception);