NotificationLogRepository.php
3 months ago
NotificationRepository.php
3 months ago
NotificationSMSHistoryRepository.php
3 months ago
NotificationsToEntitiesRepository.php
3 months ago
NotificationRepository.php
221 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 | $statement->execute($params); |
| 64 | |
| 65 | return $this->connection->lastInsertId(); |
| 66 | } catch (\Exception $e) { |
| 67 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param int $id |
| 73 | * @param Notification $entity |
| 74 | * |
| 75 | * @return mixed |
| 76 | * @throws QueryExecutionException |
| 77 | */ |
| 78 | public function update($id, $entity) |
| 79 | { |
| 80 | $data = $entity->toArray(); |
| 81 | |
| 82 | $params = [ |
| 83 | ':name' => $data['name'], |
| 84 | ':customName' => $data['customName'], |
| 85 | ':status' => $data['status'], |
| 86 | ':time' => $data['time'], |
| 87 | ':timeBefore' => $data['timeBefore'], |
| 88 | ':timeAfter' => $data['timeAfter'], |
| 89 | ':subject' => $data['subject'], |
| 90 | ':content' => $data['content'], |
| 91 | ':translations' => $data['translations'], |
| 92 | ':sendOnlyMe' => $data['sendOnlyMe'] ? 1 : 0, |
| 93 | ':whatsAppTemplate' => $data['whatsAppTemplate'], |
| 94 | ':minimumTimeBeforeBooking' => $data['minimumTimeBeforeBooking'], |
| 95 | ':id' => $id, |
| 96 | ]; |
| 97 | |
| 98 | try { |
| 99 | $statement = $this->connection->prepare( |
| 100 | "UPDATE {$this->table} SET |
| 101 | `name` = :name, |
| 102 | `customName` = :customName, |
| 103 | `status` = :status, |
| 104 | `time` = :time, |
| 105 | `timeBefore` = :timeBefore, |
| 106 | `timeAfter` = :timeAfter, |
| 107 | `subject` = :subject, |
| 108 | `content` = :content, |
| 109 | `translations` = :translations, |
| 110 | `sendOnlyMe` = :sendOnlyMe, |
| 111 | `whatsAppTemplate` = :whatsAppTemplate, |
| 112 | `minimumTimeBeforeBooking` = :minimumTimeBeforeBooking |
| 113 | WHERE id = :id" |
| 114 | ); |
| 115 | |
| 116 | $statement->execute($params); |
| 117 | |
| 118 | return true; |
| 119 | } catch (\Exception $e) { |
| 120 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param bool $includeCustom Whether to include custom notifications |
| 126 | * @return Collection |
| 127 | * @throws QueryExecutionException |
| 128 | * @throws InvalidArgumentException |
| 129 | */ |
| 130 | public function getAll($includeCustom = true) |
| 131 | { |
| 132 | // Only include custom notifications if both self::CUSTOM is true AND $includeCustom parameter is true |
| 133 | $custom = (!self::CUSTOM || !$includeCustom) ? ' WHERE customName IS NULL' : ''; |
| 134 | |
| 135 | try { |
| 136 | $statement = $this->connection->query($this->selectQuery() . $custom); |
| 137 | |
| 138 | $rows = $statement->fetchAll(); |
| 139 | } catch (\Exception $e) { |
| 140 | throw new QueryExecutionException('Unable to get data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 141 | } |
| 142 | |
| 143 | $items = []; |
| 144 | foreach ($rows as $row) { |
| 145 | $items[] = call_user_func([static::FACTORY, 'create'], $row); |
| 146 | } |
| 147 | |
| 148 | return new Collection($items); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @param $name |
| 153 | * @param $type |
| 154 | * @param bool $includeCustom Whether to include custom notifications (default: true for backward compatibility) |
| 155 | * |
| 156 | * @return Collection |
| 157 | * @throws QueryExecutionException |
| 158 | * @throws InvalidArgumentException |
| 159 | */ |
| 160 | public function getByNameAndType($name, $type, $includeCustom = true) |
| 161 | { |
| 162 | // Only include custom notifications if both self::CUSTOM is true AND $includeCustom parameter is true |
| 163 | $custom = (!self::CUSTOM || !$includeCustom) ? 'customName IS NULL AND ' : ''; |
| 164 | |
| 165 | try { |
| 166 | $statement = $this->connection->prepare( |
| 167 | $this->selectQuery() . " WHERE {$custom}{$this->table}.name LIKE :name AND {$this->table}.type = :type" |
| 168 | ); |
| 169 | |
| 170 | $params = [ |
| 171 | ':name' => $name, |
| 172 | ':type' => $type |
| 173 | ]; |
| 174 | |
| 175 | $statement->execute($params); |
| 176 | |
| 177 | $rows = $statement->fetchAll(); |
| 178 | } catch (\Exception $e) { |
| 179 | throw new QueryExecutionException('Unable to find by name and type in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 180 | } |
| 181 | |
| 182 | $items = new Collection(); |
| 183 | foreach ($rows as $row) { |
| 184 | $items->addItem(call_user_func([static::FACTORY, 'create'], $row), $row['id']); |
| 185 | } |
| 186 | |
| 187 | return $items; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | /** |
| 192 | * @param int $notificationId |
| 193 | * |
| 194 | * @return bool |
| 195 | * @throws QueryExecutionException |
| 196 | * @throws InvalidArgumentException |
| 197 | */ |
| 198 | public function delete($notificationId) |
| 199 | { |
| 200 | $notificationsToEntities = NotificationsToEntitiesTable::getTableName(); |
| 201 | $params = [ |
| 202 | ':id' => $notificationId, |
| 203 | ]; |
| 204 | |
| 205 | try { |
| 206 | $statement = $this->connection->prepare( |
| 207 | "DELETE FROM {$this->table} WHERE id = :id" |
| 208 | ); |
| 209 | $statement->execute($params); |
| 210 | $statement = $this->connection->prepare( |
| 211 | "DELETE FROM {$notificationsToEntities} WHERE notificationId = :id" |
| 212 | ); |
| 213 | $statement->execute($params); |
| 214 | |
| 215 | return true; |
| 216 | } catch (\Exception $e) { |
| 217 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 |