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