PluginProbe ʕ •ᴥ•ʔ
Booking for Appointments and Events Calendar – Amelia / 1.2.32
Booking for Appointments and Events Calendar – Amelia v1.2.32
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 1 year ago NotificationSMSHistoryRepository.php 1 year ago NotificationsToEntitiesRepository.php 1 year ago
NotificationRepository.php
224 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 bool
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 * @return Collection
133 * @throws QueryExecutionException
134 * @throws InvalidArgumentException
135 */
136 public function getAll()
137 {
138 $custom = !self::CUSTOM ? ' WHERE customName IS NULL' : '';
139
140 try {
141 $statement = $this->connection->query($this->selectQuery() . $custom);
142
143 $rows = $statement->fetchAll();
144 } catch (\Exception $e) {
145 throw new QueryExecutionException('Unable to get data from ' . __CLASS__, $e->getCode(), $e);
146 }
147
148 $items = [];
149 foreach ($rows as $row) {
150 $items[] = call_user_func([static::FACTORY, 'create'], $row);
151 }
152
153 return new Collection($items);
154 }
155
156 /**
157 * @param $name
158 * @param $type
159 *
160 * @return Collection
161 * @throws QueryExecutionException
162 * @throws InvalidArgumentException
163 */
164 public function getByNameAndType($name, $type)
165 {
166 $custom = !self::CUSTOM ? 'customName IS NULL AND ' : '';
167
168 try {
169 $statement = $this->connection->prepare(
170 $this->selectQuery() . " WHERE {$custom}{$this->table}.name LIKE :name AND {$this->table}.type = :type"
171 );
172
173 $params = [
174 ':name' => $name,
175 ':type' => $type
176 ];
177
178 $statement->execute($params);
179
180 $rows = $statement->fetchAll();
181 } catch (\Exception $e) {
182 throw new QueryExecutionException('Unable to find by name and type in ' . __CLASS__, $e->getCode(), $e);
183 }
184
185 $items = new Collection();
186 foreach ($rows as $row) {
187 $items->addItem(call_user_func([static::FACTORY, 'create'], $row), $row['id']);
188 }
189
190 return $items;
191 }
192
193
194 /**
195 * @param int $notificationId
196 *
197 * @return bool
198 * @throws QueryExecutionException
199 * @throws InvalidArgumentException
200 */
201 public function delete($notificationId)
202 {
203 $notificationsToEntities = NotificationsToEntitiesTable::getTableName();
204 $params = [
205 ':id' => $notificationId,
206 ];
207
208 try {
209 $statement = $this->connection->prepare(
210 "DELETE FROM {$this->table} WHERE id = :id"
211 );
212 $success1 = $statement->execute($params);
213 $statement = $this->connection->prepare(
214 "DELETE FROM {$notificationsToEntities} WHERE notificationId = :id"
215 );
216 $success2 = $statement->execute($params);
217
218 return $success1 && $success2;
219 } catch (\Exception $e) {
220 throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e);
221 }
222 }
223 }
224