ameliabooking
/
src
/
Infrastructure
/
Repository
/
Bookable
/
Service
/
ResourceEntitiesRepository.php
CategoryRepository.php
3 months ago
ExtraRepository.php
3 months ago
PackageCustomerRepository.php
1 month ago
PackageCustomerServiceRepository.php
2 weeks ago
PackageRepository.php
2 months ago
PackageServiceLocationRepository.php
3 months ago
PackageServiceProviderRepository.php
3 months ago
PackageServiceRepository.php
3 months ago
ProviderServiceRepository.php
3 months ago
ResourceEntitiesRepository.php
3 months ago
ResourceRepository.php
3 months ago
ServiceRepository.php
2 weeks ago
ResourceEntitiesRepository.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service; |
| 9 | |
| 10 | use AmeliaBooking\Infrastructure\Connection; |
| 11 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 13 | |
| 14 | /** |
| 15 | * Class ResourceEntitiesRepository |
| 16 | * |
| 17 | * @package AmeliaBooking\Infrastructure\Repository\Service |
| 18 | */ |
| 19 | class ResourceEntitiesRepository extends AbstractRepository |
| 20 | { |
| 21 | /** |
| 22 | * @param Connection $connection |
| 23 | * @param string $table |
| 24 | */ |
| 25 | public function __construct( |
| 26 | Connection $connection, |
| 27 | $table |
| 28 | ) { |
| 29 | parent::__construct($connection, $table); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @param array $entity |
| 34 | * |
| 35 | * @return int |
| 36 | * @throws QueryExecutionException |
| 37 | */ |
| 38 | public function add($entity) |
| 39 | { |
| 40 | $params = [ |
| 41 | ':resourceId' => $entity['resourceId'], |
| 42 | ':entityId' => $entity['entityId'], |
| 43 | ':entityType' => $entity['entityType'], |
| 44 | ]; |
| 45 | |
| 46 | try { |
| 47 | $statement = $this->connection->prepare( |
| 48 | "INSERT INTO |
| 49 | {$this->table} |
| 50 | ( |
| 51 | `resourceId`, |
| 52 | `entityId`, |
| 53 | `entityType` |
| 54 | ) VALUES ( |
| 55 | :resourceId, |
| 56 | :entityId, |
| 57 | :entityType |
| 58 | )" |
| 59 | ); |
| 60 | |
| 61 | $statement->execute($params); |
| 62 | |
| 63 | return $this->connection->lastInsertId(); |
| 64 | } catch (\Exception $e) { |
| 65 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @param int $resourceEntityId |
| 71 | * @param array $entity |
| 72 | * |
| 73 | * @throws QueryExecutionException |
| 74 | */ |
| 75 | public function update($resourceEntityId, $entity) |
| 76 | { |
| 77 | $params = [ |
| 78 | ':resourceId' => $entity['resourceId'], |
| 79 | ':entityId' => $entity['entityId'], |
| 80 | ':entityType' => $entity['entityType'], |
| 81 | ':id' => $resourceEntityId |
| 82 | ]; |
| 83 | |
| 84 | |
| 85 | try { |
| 86 | $statement = $this->connection->prepare( |
| 87 | "UPDATE {$this->table} |
| 88 | SET |
| 89 | `resourceId` = :resourceId, |
| 90 | `entityId` = :entityId, |
| 91 | `entityType` = :entityType, |
| 92 | WHERE |
| 93 | id = :id" |
| 94 | ); |
| 95 | |
| 96 | $statement->execute($params); |
| 97 | } catch (\Exception $e) { |
| 98 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @param $id |
| 104 | * |
| 105 | * @return array |
| 106 | * @throws QueryExecutionException |
| 107 | */ |
| 108 | public function getByResourceId($id) |
| 109 | { |
| 110 | try { |
| 111 | $statement = $this->connection->prepare( |
| 112 | "SELECT * FROM {$this->table} WHERE resourceId = :resourceId" |
| 113 | ); |
| 114 | |
| 115 | $params = [ |
| 116 | ':resourceId' => $id |
| 117 | ]; |
| 118 | |
| 119 | $statement->execute($params); |
| 120 | |
| 121 | $entityRows = $statement->fetchAll(); |
| 122 | } catch (\Exception $e) { |
| 123 | throw new QueryExecutionException('Unable to get entities in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 124 | } |
| 125 | |
| 126 | return $entityRows; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @param int $entityId |
| 131 | * @param string $entityType |
| 132 | * |
| 133 | * @return bool |
| 134 | * @throws QueryExecutionException |
| 135 | */ |
| 136 | public function deleteByEntityIdAndEntityType($entityId, $entityType) |
| 137 | { |
| 138 | $params = [ |
| 139 | ':entityId' => $entityId, |
| 140 | ':entityType' => $entityType, |
| 141 | ]; |
| 142 | |
| 143 | try { |
| 144 | $statement = $this->connection->prepare( |
| 145 | "DELETE FROM {$this->table} WHERE entityId = :entityId AND entityType = :entityType" |
| 146 | ); |
| 147 | |
| 148 | $statement->execute($params); |
| 149 | return true; |
| 150 | } catch (\Exception $e) { |
| 151 | throw new QueryExecutionException('Unable to delete entities in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * @param int $entityId |
| 157 | * @param string $entityType |
| 158 | * @param int $resourceId |
| 159 | * |
| 160 | * @return bool |
| 161 | * @throws QueryExecutionException |
| 162 | */ |
| 163 | public function deleteByEntityIdAndEntityTypeAndResourceId($entityId, $entityType, $resourceId) |
| 164 | { |
| 165 | $params = [ |
| 166 | ':entityId' => $entityId, |
| 167 | ':entityType' => $entityType, |
| 168 | ':resourceId' => $resourceId, |
| 169 | ]; |
| 170 | |
| 171 | try { |
| 172 | $statement = $this->connection->prepare( |
| 173 | "DELETE FROM {$this->table} WHERE entityId = :entityId AND entityType = :entityType AND resourceId = :resourceId" |
| 174 | ); |
| 175 | |
| 176 | $statement->execute($params); |
| 177 | return true; |
| 178 | } catch (\Exception $e) { |
| 179 | throw new QueryExecutionException('Unable to delete entities in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 |