3 * @copyright Copyright (C) 2010-2022, 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\Introduction\Repository;
24 use Friendica\BaseRepository;
25 use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
26 use Friendica\Contact\Introduction\Exception\IntroductionPersistenceException;
27 use Friendica\Contact\Introduction\Collection;
28 use Friendica\Contact\Introduction\Entity;
29 use Friendica\Contact\Introduction\Factory;
30 use Friendica\Database\Database;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Util\DateTimeFormat;
33 use Psr\Log\LoggerInterface;
35 class Introduction extends BaseRepository
37 /** @var Factory\Introduction */
40 protected static $table_name = 'intro';
42 public function __construct(Database $database, LoggerInterface $logger, Factory\Introduction $factory)
44 parent::__construct($database, $logger, $factory);
48 * @param array $condition
49 * @param array $params
51 * @return Entity\Introduction
53 * @throws NotFoundException the underlying exception if there's no Introduction with the given conditions
55 private function selectOne(array $condition, array $params = []): Entity\Introduction
57 return parent::_selectOne($condition, $params);
61 * Converts a given Introduction into a DB compatible row array
63 * @param Entity\Introduction $introduction
67 protected function convertToTableRow(Entity\Introduction $introduction): array
70 'uid' => $introduction->uid,
71 'contact-id' => $introduction->cid,
72 'suggest-cid' => $introduction->sid,
73 'knowyou' => $introduction->knowyou ? 1 : 0,
74 'note' => $introduction->note,
75 'hash' => $introduction->hash,
76 'ignore' => $introduction->ignore ? 1 : 0,
77 'datetime' => $introduction->datetime->format(DateTimeFormat::MYSQL),
85 * @return Entity\Introduction
87 * @throws IntroductionNotFoundException in case there is no Introduction with this id
89 public function selectOneById(int $id, int $uid): Entity\Introduction
92 return $this->selectOne(['id' => $id, 'uid' => $uid]);
93 } catch (NotFoundException $exception) {
94 throw new IntroductionNotFoundException(sprintf('There is no Introduction with the ID %d for the user %d', $id, $uid), $exception);
99 * Selects introductions for a given user
102 * @param int|null $min_id
103 * @param int|null $max_id
106 * @return Collection\Introductions
108 public function selectForUser(int $uid, int $min_id = null, int $max_id = null, int $limit = self::LIMIT): Collection\Introductions
111 $BaseCollection = parent::_selectByBoundaries(
112 ['`uid` = ? AND NOT `ignore`',$uid],
113 ['order' => ['id' => 'DESC']],
114 $min_id, $max_id, $limit);
115 } catch (\Exception $e) {
116 throw new IntroductionPersistenceException(sprintf('Cannot select Introductions for used %d', $uid), $e);
119 return new Collection\Introductions($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
123 * Selects the introduction for a given contact
127 * @return Entity\Introduction
129 * @throws IntroductionNotFoundException in case there is not Introduction for this contact
131 public function selectForContact(int $cid): Entity\Introduction
134 return $this->selectOne(['contact-id' => $cid]);
135 } catch (NotFoundException $exception) {
136 throw new IntroductionNotFoundException(sprintf('There is no Introduction for the contact %d', $cid), $exception);
140 public function countActiveForUser($uid, array $params = []): int
143 return $this->count(['ignore' => false, 'uid' => $uid], $params);
144 } catch (\Exception $e) {
145 throw new IntroductionPersistenceException(sprintf('Cannot count Introductions for used %d', $uid), $e);
150 * Checks, if the suggested contact already exists for the user
157 public function suggestionExistsForUser(int $sid, int $uid): bool
160 return $this->exists(['uid' => $uid, 'suggest-cid' => $sid]);
161 } catch (\Exception $e) {
162 throw new IntroductionPersistenceException(sprintf('Cannot check suggested Introduction for contact %d and user %d', $sid, $uid), $e);
167 * @param Entity\Introduction $introduction
171 * @throws IntroductionPersistenceException in case the underlying storage cannot delete the Introduction
173 public function delete(Entity\Introduction $introduction): bool
175 if (!$introduction->id) {
180 return $this->db->delete(self::$table_name, ['id' => $introduction->id]);
181 } catch (\Exception $e) {
182 throw new IntroductionPersistenceException(sprintf('Cannot delete Introduction with id %d', $introduction->id), $e);
187 * @param Entity\Introduction $introduction
189 * @return Entity\Introduction
191 * @throws IntroductionPersistenceException In case the underlying storage cannot save the Introduction
193 public function save(Entity\Introduction $introduction): Entity\Introduction
196 $fields = $this->convertToTableRow($introduction);
198 if ($introduction->id) {
199 $this->db->update(self::$table_name, $fields, ['id' => $introduction->id]);
200 return $this->factory->createFromTableRow($fields);
202 $this->db->insert(self::$table_name, $fields);
203 return $this->selectOneById($this->db->lastInsertId(), $introduction->uid);
205 } catch (\Exception $exception) {
206 throw new IntroductionPersistenceException(sprintf('Cannot insert/update the Introduction %d for user %d', $introduction->id, $introduction->uid), $exception);