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