]> git.mxchange.org Git - friendica.git/blob - src/Contact/FriendSuggest/Repository/FriendSuggest.php
Add some missing Copyright header
[friendica.git] / src / Contact / FriendSuggest / Repository / FriendSuggest.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\FriendSuggest\Repository;
23
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;
33
34 class FriendSuggest extends BaseRepository
35 {
36         /** @var Factory\FriendSuggest */
37         protected $factory;
38
39         protected static $table_name = 'fsuggest';
40
41         public function __construct(Database $database, LoggerInterface $logger, Factory\FriendSuggest $factory)
42         {
43                 parent::__construct($database, $logger, $factory);
44         }
45
46         private function convertToTableRow(Entity\FriendSuggest $fsuggest): array
47         {
48                 return [
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,
56                 ];
57         }
58
59         /**
60          * @param array $condition
61          * @param array $params
62          *
63          * @return Entity\FriendSuggest
64          *
65          * @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions
66          */
67         private function selectOne(array $condition, array $params = []): Entity\FriendSuggest
68         {
69                 return parent::_selectOne($condition, $params);
70         }
71
72         /**
73          * @param array $condition
74          * @param array $params
75          *
76          * @return Collection\FriendSuggests
77          *
78          * @throws \Exception
79          */
80         private function select(array $condition, array $params = []): Collection\FriendSuggests
81         {
82                 return parent::_select($condition, $params);
83         }
84
85         /**
86          * @param int $id
87          *
88          * @return Entity\FriendSuggest
89          *
90          * @throws FriendSuggestNotFoundException in case there's no suggestion for this id
91          */
92         public function selectOneById(int $id): Entity\FriendSuggest
93         {
94                 try {
95                         return $this->selectOne(['id' => $id]);
96                 } catch (NotFoundException $e) {
97                         throw new FriendSuggestNotFoundException(sprintf('No FriendSuggest found for id %d', $id));
98                 }
99         }
100
101         /**
102          * @param int $cid
103          *
104          * @return Collection\FriendSuggests
105          *
106          * @throws FriendSuggestPersistenceException In case the underlying storage cannot select the suggestion
107          */
108         public function selectForContact(int $cid): Collection\FriendSuggests
109         {
110                 try {
111                         return $this->select(['cid' => $cid]);
112                 } catch (\Exception $e) {
113                         throw new FriendSuggestPersistenceException(sprintf('Cannot select FriendSuggestion for contact %d', $cid));
114                 }
115         }
116
117         /**
118          * @param Entity\FriendSuggest $fsuggest
119          *
120          * @return Entity\FriendSuggest
121          *
122          * @throws FriendSuggestNotFoundException in case the underlying storage cannot save the suggestion
123          */
124         public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
125         {
126                 try {
127                         $fields = $this->convertToTableRow($fsuggest);
128
129                         if ($fsuggest->id) {
130                                 $this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]);
131                                 return $this->factory->createFromTableRow($fields);
132                         } else {
133                                 $this->db->insert(self::$table_name, $fields);
134                                 return $this->selectOneById($this->db->lastInsertId());
135                         }
136                 } catch (\Exception $exception) {
137                         throw new FriendSuggestNotFoundException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
138                 }
139         }
140
141         /**
142          * @param Collection\FriendSuggest $fsuggests
143          *
144          * @return bool
145          *
146          * @throws FriendSuggestNotFoundException in case the underlying storage cannot delete the suggestion
147          */
148         public function delete(Collection\FriendSuggests $fsuggests): bool
149         {
150                 try {
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);
155                 }
156         }
157 }