PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 2.0.1
Booking for Appointments and Events Calendar – Amelia v2.0.1
2.4.5 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 6 months ago NotificationSMSHistoryRepository.php 6 months ago NotificationsToEntitiesRepository.php 1 year 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 public const FACTORY = NotificationFactory::class;
23
24 public const CUSTOM = false;
25
26 /**
27 * @param Notification $entity
28 *
29 * @return int
30 * @throws QueryExecutionException
31 */
32 public function add($entity)
33 {
34 $data = $entity->toArray();
35
36 $params = [
37 ':name' => $data['name'],
38 ':customName' => $data['customName'],
39 ':sendTo' => $data['sendTo'],
40 ':status' => $data['status'],
41 ':type' => $data['type'],
42 ':entity' => $data['entity'],
43 ':time' => $data['time'],
44 ':timeBefore' => $data['timeBefore'],
45 ':timeAfter' => $data['timeAfter'],
46 ':subject' => $data['subject'],
47 ':content' => $data['content'],
48 ':translations' => $data['translations'],
49 ':sendOnlyMe' => $data['sendOnlyMe'] ? 1 : 0,
50 ':whatsAppTemplate' => $data['whatsAppTemplate'],
51 ':minimumTimeBeforeBooking' => $data['minimumTimeBeforeBooking']
52 ];
53
54 try {
55 $statement = $this->connection->prepare(
56 "INSERT INTO {$this->table}
57 (`name`, `customName`, `sendTo`, `status`, `type`, `entity`, `time`, `timeBefore`,
58 `timeAfter`, `subject`, `content`, `translations`, `sendOnlyMe`, `whatsAppTemplate`, `minimumTimeBeforeBooking`)
59 VALUES (:name, :customName, :sendTo, :status, :type, :entity, :time, :timeBefore,
60 :timeAfter, :subject, :content, :translations, :sendOnlyMe, :whatsAppTemplate, :minimumTimeBeforeBooking)"
61 );
62
63 $res = $statement->execute($params);
64 if (!$res) {
65 throw new QueryExecutionException('Unable to add data in ' . __CLASS__);
66 }
67
68 return $this->connection->lastInsertId();
69 } catch (\Exception $e) {
70 throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e);
71 }
72 }
73
74 /**
75 * @param int $id
76 * @param Notification $entity
77 *
78 * @return mixed
79 * @throws QueryExecutionException
80 */
81 public function update($id, $entity)
82 {
83 $data = $entity->toArray();
84
85 $params = [
86 ':name' => $data['name'],
87 ':customName' => $data['customName'],
88 ':status' => $data['status'],
89 ':time' => $data['time'],
90 ':timeBefore' => $data['timeBefore'],
91 ':timeAfter' => $data['timeAfter'],
92 ':subject' => $data['subject'],
93 ':content' => $data['content'],
94 ':translations' => $data['translations'],
95 ':sendOnlyMe' => $data['sendOnlyMe'] ? 1 : 0,
96 ':whatsAppTemplate' => $data['whatsAppTemplate'],
97 ':minimumTimeBeforeBooking' => $data['minimumTimeBeforeBooking'],
98 ':id' => $id,
99 ];
100
101 try {
102 $statement = $this->connection->prepare(
103 "UPDATE {$this->table} SET
104 `name` = :name,
105 `customName` = :customName,
106 `status` = :status,
107 `time` = :time,
108 `timeBefore` = :timeBefore,
109 `timeAfter` = :timeAfter,
110 `subject` = :subject,
111 `content` = :content,
112 `translations` = :translations,
113 `sendOnlyMe` = :sendOnlyMe,
114 `whatsAppTemplate` = :whatsAppTemplate,
115 `minimumTimeBeforeBooking` = :minimumTimeBeforeBooking
116 WHERE id = :id"
117 );
118
119 $res = $statement->execute($params);
120
121 if (!$res) {
122 throw new QueryExecutionException('Unable to save data in ' . __CLASS__);
123 }
124
125 return $res;
126 } catch (\Exception $e) {
127 throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e);
128 }
129 }
130
131 /**
132 * @param bool $includeCustom Whether to include custom notifications
133 * @return Collection
134 * @throws QueryExecutionException
135 * @throws InvalidArgumentException
136 */
137 public function getAll($includeCustom = true)
138 {
139 // Only include custom notifications if both self::CUSTOM is true AND $includeCustom parameter is true
140 $custom = (!self::CUSTOM || !$includeCustom) ? ' WHERE customName IS NULL' : '';
141
142 try {
143 $statement = $this->connection->query($this->selectQuery() . $custom);
144
145 $rows = $statement->fetchAll();
146 } catch (\Exception $e) {
147 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
148 }
149
150 $items = [];
151 foreach ($rows as $row) {
152 $items[] = call_user_func([static::FACTORY, 'create'], $row);
153 }
154
155 return new Collection($items);
156 }
157
158 /**
159 * @param $name
160 * @param $type
161 * @param bool $includeCustom Whether to include custom notifications (default: true for backward compatibility)
162 *
163 * @return Collection
164 * @throws QueryExecutionException
165 * @throws InvalidArgumentException
166 */
167 public function getByNameAndType($name, $type, $includeCustom = true)
168 {
169 // Only include custom notifications if both self::CUSTOM is true AND $includeCustom parameter is true
170 $custom = (!self::CUSTOM || !$includeCustom) ? 'customName IS NULL AND ' : '';
171
172 try {
173 $statement = $this->connection->prepare(
174 $this->selectQuery() . " WHERE {$custom}{$this->table}.name LIKE :name AND {$this->table}.type = :type"
175 );
176
177 $params = [
178 ':name' => $name,
179 ':type' => $type
180 ];
181
182 $statement->execute($params);
183
184 $rows = $statement->fetchAll();
185 } catch (\Exception $e) {
186 throw new QueryExecutionException('Unable to find by name and type in ' . __CLASS__, $e->getCode(), $e);
187 }
188
189 $items = new Collection();
190 foreach ($rows as $row) {
191 $items->addItem(call_user_func([static::FACTORY, 'create'], $row), $row['id']);
192 }
193
194 return $items;
195 }
196
197
198 /**
199 * @param int $notificationId
200 *
201 * @return bool
202 * @throws QueryExecutionException
203 * @throws InvalidArgumentException
204 */
205 public function delete($notificationId)
206 {
207 $notificationsToEntities = NotificationsToEntitiesTable::getTableName();
208 $params = [
209 ':id' => $notificationId,
210 ];
211
212 try {
213 $statement = $this->connection->prepare(
214 "DELETE FROM {$this->table} WHERE id = :id"
215 );
216 $success1 = $statement->execute($params);
217 $statement = $this->connection->prepare(
218 "DELETE FROM {$notificationsToEntities} WHERE notificationId = :id"
219 );
220 $success2 = $statement->execute($params);
221
222 return $success1 && $success2;
223 } catch (\Exception $e) {
224 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
225 }
226 }
227 }
228