ameliabooking
/
src
/
Infrastructure
/
Repository
/
Bookable
/
Service
/
PackageServiceLocationRepository.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
PackageServiceLocationRepository.php
88 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Bookable\Service; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Entity\Location\Location; |
| 6 | use AmeliaBooking\Domain\Factory\Bookable\Service\PackageServiceFactory; |
| 7 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 8 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 9 | |
| 10 | /** |
| 11 | * Class PackageServiceLocationRepository |
| 12 | * |
| 13 | * @package AmeliaBooking\Infrastructure\Repository\Bookable\Service |
| 14 | */ |
| 15 | class PackageServiceLocationRepository extends AbstractRepository |
| 16 | { |
| 17 | public const FACTORY = PackageServiceFactory::class; |
| 18 | |
| 19 | /** |
| 20 | * @param Location $entity |
| 21 | * @param int $packageServiceId |
| 22 | * |
| 23 | * @return int |
| 24 | * @throws QueryExecutionException |
| 25 | */ |
| 26 | public function add($entity, $packageServiceId) |
| 27 | { |
| 28 | $data = $entity->toArray(); |
| 29 | |
| 30 | $params = [ |
| 31 | ':packageServiceId' => $packageServiceId, |
| 32 | ':locationId' => $data['id'], |
| 33 | ]; |
| 34 | |
| 35 | try { |
| 36 | $statement = $this->connection->prepare( |
| 37 | "INSERT INTO {$this->table} |
| 38 | (`packageServiceId`, `locationId`) |
| 39 | VALUES |
| 40 | (:packageServiceId, :locationId)" |
| 41 | ); |
| 42 | |
| 43 | $statement->execute($params); |
| 44 | } catch (\Exception $e) { |
| 45 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 46 | } |
| 47 | |
| 48 | return $this->connection->lastInsertId(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * |
| 53 | * It will delete all relations for one package service except ones that are sent in locations array |
| 54 | * |
| 55 | * @param array $locationsIds |
| 56 | * @param int $packageServiceId |
| 57 | * |
| 58 | * @return bool |
| 59 | * @throws QueryExecutionException |
| 60 | */ |
| 61 | public function deleteAllNotInLocationsServicesArrayForPackage($locationsIds, $packageServiceId) |
| 62 | { |
| 63 | $locations = ' '; |
| 64 | |
| 65 | if (!empty($locationsIds)) { |
| 66 | foreach ($locationsIds as $index => $value) { |
| 67 | ++$index; |
| 68 | $locations .= ':locationId' . $index . ', '; |
| 69 | $params[':locationId' . $index] = (int)$value; |
| 70 | } |
| 71 | $locations = 'AND `locationId` NOT IN (' . rtrim($locations, ', ') . ')'; |
| 72 | } |
| 73 | |
| 74 | $params[':packageServiceId'] = $packageServiceId; |
| 75 | |
| 76 | try { |
| 77 | $statement = $this->connection->prepare( |
| 78 | "DELETE FROM {$this->table} WHERE 1 = 1 {$locations} AND packageServiceId = :packageServiceId" |
| 79 | ); |
| 80 | |
| 81 | $statement->execute($params); |
| 82 | return true; |
| 83 | } catch (\Exception $e) { |
| 84 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__ . '. ' . $e->getMessage(), $e->getCode(), $e); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 |