]> git.mxchange.org Git - friendica.git/blob - src/Contact/Introduction/Depository/Introduction.php
Move Introduction to new depository paradigm
[friendica.git] / src / Contact / Introduction / Depository / Introduction.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Contact\Introduction\Depository;
23
24 use Friendica\BaseDepository;
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;
34
35 class Introduction extends BaseDepository
36 {
37         /** @var Factory\Introduction */
38         protected $factory;
39
40         protected static $table_name = 'intro';
41
42         public function __construct(Database $database, LoggerInterface $logger, Factory\Introduction $factory)
43         {
44                 parent::__construct($database, $logger, $factory);
45         }
46
47         /**
48          * @param array $condition
49          * @param array $params
50          *
51          * @return Entity\Introduction
52          *
53          * @throws NotFoundException the underlying exception if there's no Introduction with the given conditions
54          */
55         private function selectOne(array $condition, array $params = []): Entity\Introduction
56         {
57                 return parent::_selectOne($condition, $params);
58         }
59
60         /**
61          * Converts a given Introduction into a DB compatible row array
62          *
63          * @param Entity\Introduction $introduction
64          *
65          * @return array
66          */
67         protected function convertToTableRow(Entity\Introduction $introduction): array
68         {
69                 return [
70                         'uid'         => $introduction->uid,
71                         'fid'         => $introduction->fid,
72                         'contact-id'  => $introduction->cid,
73                         'suggest-cid' => $introduction->sid,
74                         'knowyou'     => $introduction->knowyou ? 1 : 0,
75                         'duplex'      => $introduction->duplex ? 1 : 0,
76                         'note'        => $introduction->note,
77                         'hash'        => $introduction->hash,
78                         'blocked'     => $introduction->blocked ? 1 : 0,
79                         'ignore'      => $introduction->ignore ? 1 : 0
80                 ];
81         }
82
83         /**
84          * @param int $id
85          * @param int $uid
86          *
87          * @return Entity\Introduction
88          *
89          * @throws IntroductionNotFoundException in case there is no Introduction with this id
90          */
91         public function selectOneById(int $id, int $uid): Entity\Introduction
92         {
93                 try {
94                         return $this->selectOne(['id' => $id, 'uid' => $uid]);
95                 } catch (NotFoundException $exception) {
96                         throw new IntroductionNotFoundException(sprintf('There is no Introduction with the ID %d for the user %d', $id, $uid), $exception);
97                 }
98         }
99
100         /**
101          * Selects introductions for a given user
102          *
103          * @param int      $uid
104          * @param int|null $min_id
105          * @param int|null $max_id
106          * @param int      $limit
107          *
108          * @return Collection\Introductions
109          */
110         public function selectForUser(int $uid, int $min_id = null, int $max_id = null, int $limit = self::LIMIT): Collection\Introductions
111         {
112                 try {
113                         $BaseCollection = parent::_selectByBoundaries(
114                                 ['`uid = ?` AND NOT `ignore`',$uid],
115                                 ['order' => ['id' => 'DESC']],
116                                 $min_id, $max_id, $limit);
117                 } catch (\Exception $e) {
118                         throw new IntroductionPersistenceException(sprintf('Cannot select Introductions for used %d', $uid), $e);
119                 }
120
121                 return new Collection\Introductions($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
122         }
123
124         /**
125          * @param Entity\Introduction $introduction
126          *
127          * @return bool
128          *
129          * @throws IntroductionPersistenceException in case the underlying storage cannot delete the Introduction
130          */
131         public function delete(Entity\Introduction $introduction): bool
132         {
133                 if (!$introduction->id) {
134                         return false;
135                 }
136
137                 try {
138                         return $this->db->delete(self::$table_name, ['id' => $introduction->id]);
139                 } catch (\Exception $e) {
140                         throw new IntroductionPersistenceException(sprintf('Cannot delete Introduction with id %d', $introduction->id), $e);
141                 }
142         }
143
144         /**
145          * @param Entity\Introduction $introduction
146          *
147          * @return Entity\Introduction
148          *
149          * @throws IntroductionPersistenceException In case the underlying storage cannot save the Introduction
150          */
151         public function save(Entity\Introduction $introduction): Entity\Introduction
152         {
153                 try {
154                         $fields             = $this->convertToTableRow($introduction);
155                         $fields['datetime'] = DateTimeFormat::utcNow();
156
157                         if ($introduction->id) {
158                                 $this->db->update(self::$table_name, $fields, ['id' => $introduction->id]);
159                                 return $this->factory->createFromTableRow($fields);
160                         } else {
161                                 $this->db->insert(self::$table_name, $fields);
162                                 return $this->selectOneById($this->db->lastInsertId(), $introduction->uid);
163                         }
164                 } catch (\Exception $exception) {
165                         throw new IntroductionPersistenceException(sprintf('Cannot insert/update the Introduction %d for user %d', $introduction->id, $introduction->uid), $exception);
166                 }
167         }
168 }