]> git.mxchange.org Git - friendica.git/blob - src/Contact/FriendSuggest/Repository/FriendSuggest.php
Rename Depository to Repository
[friendica.git] / src / Contact / FriendSuggest / Repository / FriendSuggest.php
1 <?php
2
3 namespace Friendica\Contact\FriendSuggest\Repository;
4
5 use Friendica\BaseRepository;
6 use Friendica\Contact\FriendSuggest\Collection;
7 use Friendica\Contact\FriendSuggest\Entity;
8 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestNotFoundException;
9 use Friendica\Contact\FriendSuggest\Exception\FriendSuggestPersistenceException;
10 use Friendica\Contact\FriendSuggest\Factory;
11 use Friendica\Database\Database;
12 use Friendica\Network\HTTPException\NotFoundException;
13 use Psr\Log\LoggerInterface;
14
15 class FriendSuggest extends BaseRepository
16 {
17         /** @var Factory\FriendSuggest */
18         protected $factory;
19
20         protected static $table_name = 'fsuggest';
21
22         public function __construct(Database $database, LoggerInterface $logger, Factory\FriendSuggest $factory)
23         {
24                 parent::__construct($database, $logger, $factory);
25         }
26
27         private function convertToTableRow(Entity\FriendSuggest $fsuggest): array
28         {
29                 return [
30                         'uid'     => $fsuggest->uid,
31                         'cid'     => $fsuggest->cid,
32                         'name'    => $fsuggest->name,
33                         'url'     => $fsuggest->url,
34                         'request' => $fsuggest->request,
35                         'photo'   => $fsuggest->photo,
36                         'note'    => $fsuggest->note,
37                 ];
38         }
39
40         /**
41          * @param array $condition
42          * @param array $params
43          *
44          * @return Entity\FriendSuggest
45          *
46          * @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions
47          */
48         private function selectOne(array $condition, array $params = []): Entity\FriendSuggest
49         {
50                 return parent::_selectOne($condition, $params);
51         }
52
53         /**
54          * @param array $condition
55          * @param array $params
56          *
57          * @return Collection\FriendSuggests
58          *
59          * @throws \Exception
60          */
61         private function select(array $condition, array $params = []): Collection\FriendSuggests
62         {
63                 return parent::_select($condition, $params);
64         }
65
66         /**
67          * @param int $id
68          *
69          * @return Entity\FriendSuggest
70          *
71          * @throws FriendSuggestNotFoundException in case there's no suggestion for this id
72          */
73         public function selectOneById(int $id): Entity\FriendSuggest
74         {
75                 try {
76                         return $this->selectOne(['id' => $id]);
77                 } catch (NotFoundException $e) {
78                         throw new FriendSuggestNotFoundException(sprintf('No FriendSuggest found for id %d', $id));
79                 }
80         }
81
82         /**
83          * @param int $cid
84          *
85          * @return Collection\FriendSuggests
86          *
87          * @throws FriendSuggestPersistenceException In case the underlying storage cannot select the suggestion
88          */
89         public function selectForContact(int $cid): Collection\FriendSuggests
90         {
91                 try {
92                         return $this->select(['cid' => $cid]);
93                 } catch (\Exception $e) {
94                         throw new FriendSuggestPersistenceException(sprintf('Cannot select FriendSuggestion for contact %d', $cid));
95                 }
96         }
97
98         /**
99          * @param Entity\FriendSuggest $fsuggest
100          *
101          * @return Entity\FriendSuggest
102          *
103          * @throws FriendSuggestNotFoundException in case the underlying storage cannot save the suggestion
104          */
105         public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest
106         {
107                 try {
108                         $fields = $this->convertToTableRow($fsuggest);
109
110                         if ($fsuggest->id) {
111                                 $this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]);
112                                 return $this->factory->createFromTableRow($fields);
113                         } else {
114                                 $this->db->insert(self::$table_name, $fields);
115                                 return $this->selectOneById($this->db->lastInsertId());
116                         }
117                 } catch (\Exception $exception) {
118                         throw new FriendSuggestNotFoundException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception);
119                 }
120         }
121
122         /**
123          * @param Collection\FriendSuggest $fsuggests
124          *
125          * @return bool
126          *
127          * @throws FriendSuggestNotFoundException in case the underlying storage cannot delete the suggestion
128          */
129         public function delete(Collection\FriendSuggests $fsuggests): bool
130         {
131                 try {
132                         $ids = $fsuggests->column('id');
133                         return $this->db->delete(self::$table_name, ['id' => $ids]);
134                 } catch (\Exception $exception) {
135                         throw new FriendSuggestNotFoundException('Cannot delete the FriendSuggestions', $exception);
136                 }
137         }
138 }