PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.20
Booking for Appointments and Events Calendar – Amelia v1.2.20
2.4.4 2.4.3 2.4.2 2.4.1 2.4 trunk 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.28 1.2.29 1.2.3 1.2.30 1.2.31 1.2.32 1.2.33 1.2.34 1.2.35 1.2.36 1.2.37 1.2.38 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.3
ameliabooking / src / Infrastructure / Repository / Notification / NotificationRepository.php
ameliabooking / src / Infrastructure / Repository / Notification Last commit date
NotificationLogRepository.php 1 year ago NotificationRepository.php 2 years ago NotificationSMSHistoryRepository.php 2 years ago NotificationsToEntitiesRepository.php 4 years ago
NotificationRepository.php
228 lines
1 <?php
2
3 namespace AmeliaBooking\Infrastructure\Repository\Notification;
4
5 use AmeliaBooking\Domain\Collection\Collection;
6 use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException;
7 use AmeliaBooking\Domain\Entity\Notification\Notification;
8 use AmeliaBooking\Domain\Factory\Notification\NotificationFactory;
9 use AmeliaBooking\Domain\Repository\Notification\NotificationRepositoryInterface;
10 use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException;
11 use AmeliaBooking\Infrastructure\Repository\AbstractRepository;
12 use AmeliaBooking\Infrastructure\Repository\Booking\Event\EventRepository;
13 use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Notification\NotificationsToEntitiesTable;
14
15 /**
16 * Class NotificationRepository
17 *
18 * @package AmeliaBooking\Infrastructure\Repository\Notification
19 */
20 class NotificationRepository extends AbstractRepository implements NotificationRepositoryInterface
21 {
22
23 const FACTORY = NotificationFactory::class;
24
25 const CUSTOM = false;
26
27 /**
28 * @param Notification $entity
29 *
30 * @return bool
31 * @throws QueryExecutionException
32 */
33 public function add($entity)
34 {
35 $data = $entity->toArray();
36
37 $params = [
38 ':name' => $data['name'],
39 ':customName' => $data['customName'],
40 ':sendTo' => $data['sendTo'],
41 ':status' => $data['status'],
42 ':type' => $data['type'],
43 ':entity' => $data['entity'],
44 ':time' => $data['time'],
45 ':timeBefore' => $data['timeBefore'],
46 ':timeAfter' => $data['timeAfter'],
47 ':subject' => $data['subject'],
48 ':content' => $data['content'],
49 ':translations' => $data['translations'],
50 ':sendOnlyMe' => $data['sendOnlyMe'] ? 1 : 0,
51 ':whatsAppTemplate' => $data['whatsAppTemplate'],
52 ':minimumTimeBeforeBooking' => $data['minimumTimeBeforeBooking']
53 ];
54
55 try {
56 $statement = $this->connection->prepare(
57 "INSERT INTO {$this->table}
58 (`name`, `customName`, `sendTo`, `status`, `type`, `entity`, `time`, `timeBefore`,
59 `timeAfter`, `subject`, `content`, `translations`, `sendOnlyMe`, `whatsAppTemplate`, `minimumTimeBeforeBooking`)
60 VALUES (:name, :customName, :sendTo, :status, :type, :entity, :time, :timeBefore,
61 :timeAfter, :subject, :content, :translations, :sendOnlyMe, :whatsAppTemplate, :minimumTimeBeforeBooking)"
62 );
63
64 $res = $statement->execute($params);
65 if (!$res) {
66 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
67 }
68
69 return $this->connection->lastInsertId();
70 } catch (\Exception $e) {
71 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
72 }
73 }
74
75 /**
76 * @param int $id
77 * @param Notification $entity
78 *
79 * @return mixed
80 * @throws QueryExecutionException
81 */
82 public function update($id, $entity)
83 {
84 $data = $entity->toArray();
85
86 $params = [
87 ':name' => $data['name'],
88 ':customName' => $data['customName'],
89 ':status' => $data['status'],
90 ':time' => $data['time'],
91 ':timeBefore' => $data['timeBefore'],
92 ':timeAfter' => $data['timeAfter'],
93 ':subject' => $data['subject'],
94 ':content' => $data['content'],
95 ':translations' => $data['translations'],
96 ':sendOnlyMe' => $data['sendOnlyMe'] ? 1 : 0,
97 ':whatsAppTemplate' => $data['whatsAppTemplate'],
98 ':minimumTimeBeforeBooking' => $data['minimumTimeBeforeBooking'],
99 ':id' => $id,
100 ];
101
102 try {
103 $statement = $this->connection->prepare(
104 "UPDATE {$this->table} SET
105 `name` = :name,
106 `customName` = :customName,
107 `status` = :status,
108 `time` = :time,
109 `timeBefore` = :timeBefore,
110 `timeAfter` = :timeAfter,
111 `subject` = :subject,
112 `content` = :content,
113 `translations` = :translations,
114 `sendOnlyMe` = :sendOnlyMe,
115 `whatsAppTemplate` = :whatsAppTemplate,
116 `minimumTimeBeforeBooking` = :minimumTimeBeforeBooking
117 WHERE id = :id"
118 );
119
120 $res = $statement->execute($params);
121
122 if (!$res) {
123 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
124 }
125
126 return $res;
127 } catch (\Exception $e) {
128 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
129 }
130 }
131
132 /**
133 * @return Collection
134 * @throws QueryExecutionException
135 * @throws InvalidArgumentException
136 */
137 public function getAll()
138 {
139 $custom = !self::CUSTOM ? ' WHERE customName IS NULL' : '';
140
141 try {
142 $statement = $this->connection->query($this->selectQuery() . $custom);
143
144 $rows = $statement->fetchAll();
145 } catch (\Exception $e) {
146 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
147 }
148
149 $items = [];
150 foreach ($rows as $row) {
151 $items[] = call_user_func([static::FACTORY, 'create'], $row);
152 }
153
154 return new Collection($items);
155 }
156
157 /**
158 * @param $name
159 * @param $type
160 *
161 * @return Collection
162 * @throws QueryExecutionException
163 * @throws InvalidArgumentException
164 */
165 public function getByNameAndType($name, $type)
166 {
167 $custom = !self::CUSTOM ? 'customName IS NULL AND ' : '';
168
169 try {
170 $statement = $this->connection->prepare(
171 $this->selectQuery() . " WHERE {$custom}{$this->table}.name LIKE :name AND {$this->table}.type = :type"
172 );
173
174 $params = [
175 ':name' => $name,
176 ':type' => $type
177 ];
178
179 $statement->execute($params);
180
181 $rows = $statement->fetchAll();
182
183 } catch (\Exception $e) {
184 throw new QueryExecutionException('Unable to find by name and type in ' . __CLASS__, $e->getCode(), $e);
185 }
186
187 $items = new Collection();
188 foreach ($rows as $row) {
189 $items->addItem(call_user_func([static::FACTORY, 'create'], $row), $row['id']);
190 }
191
192 return $items;
193 }
194
195
196 /**
197 * @param int $notificationId
198 *
199 * @return bool
200 * @throws QueryExecutionException
201 * @throws InvalidArgumentException
202 */
203 public function delete($notificationId)
204 {
205 $notificationsToEntities = NotificationsToEntitiesTable::getTableName();
206 $params = [
207 ':id' => $notificationId,
208 ];
209
210 try {
211 $statement = $this->connection->prepare(
212 "DELETE FROM {$this->table} WHERE id = :id"
213 );
214 $success1 = $statement->execute($params);
215 $statement = $this->connection->prepare(
216 "DELETE FROM {$notificationsToEntities} WHERE notificationId = :id"
217 );
218 $success2 = $statement->execute($params);
219
220 return $success1 && $success2;
221 } catch (\Exception $e) {
222 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
223 }
224 }
225
226
227 }
228