ameliabooking
/
src
/
Infrastructure
/
Repository
/
Bookable
/
Service
/
ProviderServiceRepository.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
ProviderServiceRepository.php
402 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 6 | use AmeliaBooking\Domain\Entity\Entities; |
| 7 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 8 | use AmeliaBooking\Infrastructure\Licence; |
| 9 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 10 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 11 | |
| 12 | /** |
| 13 | * Class ProviderServiceRepository |
| 14 | * |
| 15 | * @package AmeliaBooking\Infrastructure\Repository\Bookable\Service |
| 16 | */ |
| 17 | class ProviderServiceRepository extends AbstractRepository |
| 18 | { |
| 19 | public const FACTORY = ServiceFactory::class; |
| 20 | |
| 21 | /** |
| 22 | * @param Service $entity |
| 23 | * @param int $userId |
| 24 | * |
| 25 | * @return int |
| 26 | * @throws QueryExecutionException |
| 27 | */ |
| 28 | public function add($entity, $userId) |
| 29 | { |
| 30 | $data = $entity->toArray(); |
| 31 | |
| 32 | $params = [ |
| 33 | ':userId' => $userId, |
| 34 | ':serviceId' => $data['id'], |
| 35 | ':minCapacity' => $data['minCapacity'], |
| 36 | ':maxCapacity' => $data['maxCapacity'], |
| 37 | ':price' => $data['price'], |
| 38 | ]; |
| 39 | |
| 40 | $additionalData = Licence\DataModifier::getProviderServiceRepositoryData($data); |
| 41 | |
| 42 | $params = array_merge($params, $additionalData['values']); |
| 43 | |
| 44 | try { |
| 45 | $statement = $this->connection->prepare( |
| 46 | "INSERT INTO {$this->table} |
| 47 | ( |
| 48 | {$additionalData['columns']} |
| 49 | `userId`, |
| 50 | `serviceId`, |
| 51 | `minCapacity`, |
| 52 | `maxCapacity`, |
| 53 | `price` |
| 54 | ) |
| 55 | VALUES |
| 56 | ( |
| 57 | {$additionalData['placeholders']} |
| 58 | :userId, |
| 59 | :serviceId, |
| 60 | :minCapacity, |
| 61 | :maxCapacity, |
| 62 | :price |
| 63 | )" |
| 64 | ); |
| 65 | |
| 66 | $statement->execute($params); |
| 67 | } catch (\Exception $e) { |
| 68 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 69 | } |
| 70 | |
| 71 | return $this->connection->lastInsertId(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @param Service $entity |
| 76 | * @param int $id |
| 77 | * |
| 78 | * @return void |
| 79 | * @throws QueryExecutionException |
| 80 | */ |
| 81 | public function update($entity, $id) |
| 82 | { |
| 83 | $data = $entity->toArray(); |
| 84 | |
| 85 | $params = [ |
| 86 | ':id' => $id, |
| 87 | ':minCapacity' => $data['minCapacity'], |
| 88 | ':maxCapacity' => $data['maxCapacity'], |
| 89 | ':price' => $data['price'], |
| 90 | ]; |
| 91 | |
| 92 | $additionalData = Licence\DataModifier::getProviderServiceRepositoryData($data); |
| 93 | |
| 94 | $params = array_merge($params, $additionalData['values']); |
| 95 | |
| 96 | try { |
| 97 | $statement = $this->connection->prepare( |
| 98 | "UPDATE {$this->table} |
| 99 | SET |
| 100 | {$additionalData['columnsPlaceholders']} |
| 101 | `minCapacity` = :minCapacity, |
| 102 | `maxCapacity` = :maxCapacity, |
| 103 | `price` = :price |
| 104 | WHERE id = :id" |
| 105 | ); |
| 106 | |
| 107 | $statement->execute($params); |
| 108 | } catch (\Exception $e) { |
| 109 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @param int $id |
| 115 | * @param string $type |
| 116 | * |
| 117 | * @return array |
| 118 | * @throws QueryExecutionException |
| 119 | */ |
| 120 | public function getAllForEntity($id, $type) |
| 121 | { |
| 122 | $columnName = ''; |
| 123 | |
| 124 | switch ($type) { |
| 125 | case (Entities::EMPLOYEE): |
| 126 | $columnName = 'userId'; |
| 127 | |
| 128 | break; |
| 129 | |
| 130 | case (Entities::SERVICE): |
| 131 | $columnName = 'serviceId'; |
| 132 | |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | try { |
| 137 | $statement = $this->connection->prepare( |
| 138 | "SELECT |
| 139 | ps.id, |
| 140 | ps.userId, |
| 141 | ps.serviceId, |
| 142 | ps.minCapacity, |
| 143 | ps.maxCapacity, |
| 144 | ps.price, |
| 145 | ps.customPricing |
| 146 | FROM {$this->table} ps |
| 147 | WHERE ps.{$columnName} = :entityId" |
| 148 | ); |
| 149 | |
| 150 | $params = array( |
| 151 | ':entityId' => $id |
| 152 | ); |
| 153 | |
| 154 | $statement->execute($params); |
| 155 | |
| 156 | $rows = $statement->fetchAll(); |
| 157 | |
| 158 | foreach ($rows as &$row) { |
| 159 | $row['id'] = (int)$row['id']; |
| 160 | $row['userId'] = (int)$row['userId']; |
| 161 | $row['serviceId'] = (int)$row['serviceId']; |
| 162 | $row['minCapacity'] = (int)$row['minCapacity']; |
| 163 | $row['maxCapacity'] = (int)$row['maxCapacity']; |
| 164 | } |
| 165 | |
| 166 | return $rows; |
| 167 | } catch (\Exception $e) { |
| 168 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * |
| 174 | * It will delete all relations for one service except ones that are sent in providers array |
| 175 | * |
| 176 | * @param array $providersIds |
| 177 | * @param int $serviceId |
| 178 | * |
| 179 | * @return bool |
| 180 | * @throws QueryExecutionException |
| 181 | */ |
| 182 | public function deleteAllNotInProvidersArrayForService($providersIds, $serviceId) |
| 183 | { |
| 184 | $providers = ' '; |
| 185 | |
| 186 | if (!empty($providersIds)) { |
| 187 | foreach ($providersIds as $index => $value) { |
| 188 | ++$index; |
| 189 | $providers .= ':providerId' . $index . ', '; |
| 190 | $params[':providerId' . $index] = (int)$value; |
| 191 | } |
| 192 | $providers = 'AND `userId` NOT IN (' . rtrim($providers, ', ') . ')'; |
| 193 | } |
| 194 | |
| 195 | $params[':serviceId'] = $serviceId; |
| 196 | |
| 197 | try { |
| 198 | $statement = $this->connection->prepare( |
| 199 | "DELETE FROM {$this->table} WHERE 1 = 1 $providers AND serviceId = :serviceId" |
| 200 | ); |
| 201 | |
| 202 | $statement->execute($params); |
| 203 | return true; |
| 204 | } catch (\Exception $e) { |
| 205 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * |
| 211 | * It will delete all relations for one service except ones that are sent in providers array |
| 212 | * |
| 213 | * @param array $servicesIds |
| 214 | * @param int $providerId |
| 215 | * |
| 216 | * @return bool |
| 217 | * @throws QueryExecutionException |
| 218 | */ |
| 219 | public function deleteAllNotInServicesArrayForProvider($servicesIds, $providerId) |
| 220 | { |
| 221 | $services = ' '; |
| 222 | |
| 223 | if (!empty($servicesIds)) { |
| 224 | foreach ($servicesIds as $index => $value) { |
| 225 | ++$index; |
| 226 | $services .= ':serviceId' . $index . ', '; |
| 227 | $params[':serviceId' . $index] = $value; |
| 228 | } |
| 229 | $services = 'AND `serviceId` NOT IN (' . rtrim($services, ', ') . ')'; |
| 230 | } |
| 231 | |
| 232 | $params[':providerId'] = $providerId; |
| 233 | |
| 234 | try { |
| 235 | $statement = $this->connection->prepare( |
| 236 | "DELETE FROM {$this->table} WHERE 1 = 1 $services AND userId = :providerId" |
| 237 | ); |
| 238 | |
| 239 | $statement->execute($params); |
| 240 | return true; |
| 241 | } catch (\Exception $e) { |
| 242 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @param int $entityId |
| 248 | * @param int $entityType |
| 249 | * |
| 250 | * @return bool |
| 251 | * @throws QueryExecutionException |
| 252 | */ |
| 253 | public function deleteDuplicated($entityId, $entityType) |
| 254 | { |
| 255 | $matchColumnName = ''; |
| 256 | |
| 257 | $entityColumnName = ''; |
| 258 | |
| 259 | switch ($entityType) { |
| 260 | case (Entities::EMPLOYEE): |
| 261 | $matchColumnName = 'serviceId'; |
| 262 | |
| 263 | $entityColumnName = 'userId'; |
| 264 | |
| 265 | break; |
| 266 | |
| 267 | case (Entities::SERVICE): |
| 268 | $matchColumnName = 'userId'; |
| 269 | |
| 270 | $entityColumnName = 'serviceId'; |
| 271 | |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | $params = [ |
| 276 | ':entityId1' => $entityId, |
| 277 | ':entityId2' => $entityId, |
| 278 | ]; |
| 279 | |
| 280 | try { |
| 281 | $statement = $this->connection->prepare( |
| 282 | "DELETE t1 FROM {$this->table} t1, {$this->table} t2 WHERE |
| 283 | t1.{$entityColumnName} = :entityId1 AND |
| 284 | t2.{$entityColumnName} = :entityId2 AND |
| 285 | t1.id < t2.id AND |
| 286 | t1.{$matchColumnName} = t2.{$matchColumnName}" |
| 287 | ); |
| 288 | |
| 289 | $statement->execute($params); |
| 290 | return true; |
| 291 | } catch (\Exception $e) { |
| 292 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * @param Service $entity |
| 298 | * @param int $serviceId |
| 299 | * |
| 300 | * @return boolean |
| 301 | * @throws QueryExecutionException |
| 302 | */ |
| 303 | public function updateServiceForAllProviders($entity, $serviceId) |
| 304 | { |
| 305 | $data = $entity->toArray(); |
| 306 | |
| 307 | $params = [ |
| 308 | ':serviceId' => $serviceId, |
| 309 | ':minCapacity' => $data['minCapacity'], |
| 310 | ':maxCapacity' => $data['maxCapacity'], |
| 311 | ':price' => $data['price'], |
| 312 | ':customPricing' => $data['customPricing'], |
| 313 | ]; |
| 314 | |
| 315 | try { |
| 316 | $statement = $this->connection->prepare( |
| 317 | "UPDATE {$this->table} |
| 318 | SET `minCapacity` = :minCapacity, `maxCapacity` = :maxCapacity, `price` = :price, `customPricing` = :customPricing |
| 319 | WHERE serviceId = :serviceId" |
| 320 | ); |
| 321 | |
| 322 | $statement->execute($params); |
| 323 | } catch (\Exception $e) { |
| 324 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * @param Service $entity |
| 332 | * @param int $serviceId |
| 333 | * @param int $providerId |
| 334 | * |
| 335 | * @return boolean |
| 336 | * @throws QueryExecutionException |
| 337 | */ |
| 338 | public function updateServiceForProvider($entity, $serviceId, $providerId) |
| 339 | { |
| 340 | $data = $entity->toArray(); |
| 341 | |
| 342 | $params = [ |
| 343 | ':serviceId' => $serviceId, |
| 344 | ':providerId' => $providerId, |
| 345 | ':minCapacity' => $data['minCapacity'], |
| 346 | ':maxCapacity' => $data['maxCapacity'], |
| 347 | ':price' => $data['price'], |
| 348 | ':customPricing' => $data['customPricing'], |
| 349 | ]; |
| 350 | |
| 351 | try { |
| 352 | $statement = $this->connection->prepare( |
| 353 | "UPDATE {$this->table} |
| 354 | SET `minCapacity` = :minCapacity, `maxCapacity` = :maxCapacity, `price` = :price, `customPricing` = :customPricing |
| 355 | WHERE serviceId = :serviceId AND userId = :providerId" |
| 356 | ); |
| 357 | |
| 358 | $statement->execute($params); |
| 359 | } catch (\Exception $e) { |
| 360 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 361 | } |
| 362 | |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @param int $providerId |
| 368 | * |
| 369 | * @return array |
| 370 | * @throws QueryExecutionException |
| 371 | */ |
| 372 | public function getMandatoryServicesIdsForProvider($providerId) |
| 373 | { |
| 374 | |
| 375 | try { |
| 376 | $statement = $this->connection->prepare( |
| 377 | "SELECT |
| 378 | ps.serviceId, ps.userId |
| 379 | FROM {$this->table} ps |
| 380 | GROUP BY ps.serviceId |
| 381 | HAVING COUNT(*) = 1" |
| 382 | ); |
| 383 | |
| 384 | $statement->execute(); |
| 385 | |
| 386 | $rows = $statement->fetchAll(); |
| 387 | } catch (\Exception $e) { |
| 388 | throw new QueryExecutionException('Unable to find data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 389 | } |
| 390 | |
| 391 | $items = []; |
| 392 | |
| 393 | foreach ($rows as $row) { |
| 394 | if ($row['userId'] == $providerId) { |
| 395 | $items[] = $row['serviceId']; |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | return $items; |
| 400 | } |
| 401 | } |
| 402 |