ameliabooking
/
src
/
Infrastructure
/
Repository
/
Bookable
/
Service
/
ResourceEntitiesRepository.php
CategoryRepository.php
1 year ago
ExtraRepository.php
1 year ago
PackageCustomerRepository.php
10 months ago
PackageCustomerServiceRepository.php
1 year ago
PackageRepository.php
1 year ago
PackageServiceLocationRepository.php
1 year ago
PackageServiceProviderRepository.php
1 year ago
PackageServiceRepository.php
1 year ago
ProviderServiceRepository.php
1 year ago
ResourceEntitiesRepository.php
1 year ago
ResourceRepository.php
1 year ago
ServiceRepository.php
1 year ago
ResourceEntitiesRepository.php
189 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © TMS-Plugins. 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 bool |
| 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 | $result = $statement->execute($params); |
| 62 | |
| 63 | if (!$result) { |
| 64 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 65 | } |
| 66 | |
| 67 | return $this->connection->lastInsertId(); |
| 68 | } catch (\Exception $e) { |
| 69 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @param int $resourceEntityId |
| 75 | * @param array $entity |
| 76 | * |
| 77 | * @throws QueryExecutionException |
| 78 | */ |
| 79 | public function update($resourceEntityId, $entity) |
| 80 | { |
| 81 | $params = [ |
| 82 | ':resourceId' => $entity['resourceId'], |
| 83 | ':entityId' => $entity['entityId'], |
| 84 | ':entityType' => $entity['entityType'], |
| 85 | ':id' => $resourceEntityId |
| 86 | ]; |
| 87 | |
| 88 | |
| 89 | try { |
| 90 | $statement = $this->connection->prepare( |
| 91 | "UPDATE {$this->table} |
| 92 | SET |
| 93 | `resourceId` = :resourceId, |
| 94 | `entityId` = :entityId, |
| 95 | `entityType` = :entityType, |
| 96 | WHERE |
| 97 | id = :id" |
| 98 | ); |
| 99 | |
| 100 | $result = $statement->execute($params); |
| 101 | |
| 102 | if (!$result) { |
| 103 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 104 | } |
| 105 | } catch (\Exception $e) { |
| 106 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param $id |
| 112 | * |
| 113 | * @return array |
| 114 | * @throws QueryExecutionException |
| 115 | */ |
| 116 | public function getByResourceId($id) |
| 117 | { |
| 118 | try { |
| 119 | $statement = $this->connection->prepare( |
| 120 | "SELECT * FROM {$this->table} WHERE resourceId = :resourceId" |
| 121 | ); |
| 122 | |
| 123 | $params = [ |
| 124 | ':resourceId' => $id |
| 125 | ]; |
| 126 | |
| 127 | $statement->execute($params); |
| 128 | |
| 129 | $entityRows = $statement->fetchAll(); |
| 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 | } catch (\Exception $e) { |
| 185 | throw new QueryExecutionException('Unable to delete entities in ' . __CLASS__, $e->getCode(), $e); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 |