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