ameliabooking
/
src
/
Infrastructure
/
Repository
/
Notification
/
NotificationsToEntitiesRepository.php
NotificationLogRepository.php
3 months ago
NotificationRepository.php
3 months ago
NotificationSMSHistoryRepository.php
3 months ago
NotificationsToEntitiesRepository.php
3 months ago
NotificationsToEntitiesRepository.php
136 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Notification; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 6 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 7 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 8 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Notification\NotificationsTable; |
| 9 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Notification\NotificationsToEntitiesTable; |
| 10 | |
| 11 | class NotificationsToEntitiesRepository extends AbstractRepository |
| 12 | { |
| 13 | /** |
| 14 | * @param $notificationId |
| 15 | * |
| 16 | * @return array |
| 17 | * @throws QueryExecutionException |
| 18 | * @throws InvalidArgumentException |
| 19 | */ |
| 20 | public function getEntities($notificationId) |
| 21 | { |
| 22 | try { |
| 23 | $statement = $this->connection->prepare( |
| 24 | "SELECT entityId FROM {$this->table} WHERE notificationId = :id" |
| 25 | ); |
| 26 | |
| 27 | $params = [ |
| 28 | ':id' => $notificationId |
| 29 | ]; |
| 30 | |
| 31 | $statement->execute($params); |
| 32 | |
| 33 | $entityRows = $statement->fetchAll(); |
| 34 | } catch (\Exception $e) { |
| 35 | throw new QueryExecutionException('Unable to get entities in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 36 | } |
| 37 | |
| 38 | return array_column($entityRows, 'entityId'); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * @param int $notificationId |
| 43 | * @param int $entityId |
| 44 | * @param string $entity |
| 45 | * |
| 46 | * @return bool |
| 47 | * @throws QueryExecutionException |
| 48 | * @throws InvalidArgumentException |
| 49 | */ |
| 50 | public function removeEntity($notificationId, $entityId, $entity) |
| 51 | { |
| 52 | $params = [ |
| 53 | ':notificationId' => $notificationId, |
| 54 | ':entity' => $entity, |
| 55 | ':entityId' => $entityId |
| 56 | ]; |
| 57 | |
| 58 | try { |
| 59 | $statement = $this->connection->prepare( |
| 60 | "DELETE FROM {$this->table} WHERE notificationId = :notificationId AND entity = :entity AND entityId = :entityId" |
| 61 | ); |
| 62 | |
| 63 | $statement->execute($params); |
| 64 | return true; |
| 65 | } catch (\Exception $e) { |
| 66 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * @param $entityId |
| 73 | * |
| 74 | * @return bool |
| 75 | * @throws QueryExecutionException |
| 76 | * @throws InvalidArgumentException |
| 77 | */ |
| 78 | public function removeIfOnly($entityId) |
| 79 | { |
| 80 | $notificationsTable = NotificationsTable::getTableName(); |
| 81 | try { |
| 82 | $statement = $this->connection->prepare( |
| 83 | "DELETE n FROM {$notificationsTable} n |
| 84 | INNER JOIN {$this->table} ne ON n.id = ne.notificationId |
| 85 | WHERE ne.entityId = :id |
| 86 | AND NOT EXISTS (SELECT * FROM {$this->table} ne2 WHERE ne2.entityId <> ne.entityId AND ne2.notificationId = n.id)" |
| 87 | ); |
| 88 | |
| 89 | $params = [ |
| 90 | ':id' => $entityId |
| 91 | ]; |
| 92 | |
| 93 | $statement->execute($params); |
| 94 | |
| 95 | $statement = $this->connection->prepare( |
| 96 | "DELETE FROM {$this->table} WHERE entityId = :id" |
| 97 | ); |
| 98 | |
| 99 | $statement->execute($params); |
| 100 | |
| 101 | return true; |
| 102 | } catch (\Exception $e) { |
| 103 | throw new QueryExecutionException('Unable to get entities in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @param int $notificationId |
| 109 | * @param int $entityId |
| 110 | * @param string $entity |
| 111 | * |
| 112 | * @return bool |
| 113 | * @throws QueryExecutionException |
| 114 | * @throws InvalidArgumentException |
| 115 | */ |
| 116 | public function addEntity($notificationId, $entityId, $entity) |
| 117 | { |
| 118 | $params = [ |
| 119 | ':notificationId' => $notificationId, |
| 120 | ':entity' => $entity, |
| 121 | ':entityId' => $entityId |
| 122 | ]; |
| 123 | |
| 124 | try { |
| 125 | $statement = $this->connection->prepare( |
| 126 | "INSERT INTO {$this->table} (`notificationId`, `entity`, `entityId`) VALUES (:notificationId, :entity, :entityId)" |
| 127 | ); |
| 128 | |
| 129 | $statement->execute($params); |
| 130 | return true; |
| 131 | } catch (\Exception $e) { |
| 132 | throw new QueryExecutionException('Unable to add data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 |