ameliabooking
/
src
/
Infrastructure
/
Repository
/
Bookable
/
Service
/
PackageServiceLocationRepository.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
PackageServiceLocationRepository.php
90 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 | 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 | $res = $statement->execute($params); |
| 44 | if (!$res) { |
| 45 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 46 | } |
| 47 | } catch (\Exception $e) { |
| 48 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 49 | } |
| 50 | |
| 51 | return $this->connection->lastInsertId(); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * It will delete all relations for one package service except ones that are sent in locations array |
| 57 | * |
| 58 | * @param array $locationsIds |
| 59 | * @param int $packageServiceId |
| 60 | * |
| 61 | * @return bool |
| 62 | * @throws QueryExecutionException |
| 63 | */ |
| 64 | public function deleteAllNotInLocationsServicesArrayForPackage($locationsIds, $packageServiceId) |
| 65 | { |
| 66 | $locations = ' '; |
| 67 | |
| 68 | if (!empty($locationsIds)) { |
| 69 | foreach ($locationsIds as $index => $value) { |
| 70 | ++$index; |
| 71 | $locations .= ':locationId' . $index . ', '; |
| 72 | $params[':locationId' . $index] = (int)$value; |
| 73 | } |
| 74 | $locations = 'AND `locationId` NOT IN (' . rtrim($locations, ', ') . ')'; |
| 75 | } |
| 76 | |
| 77 | $params[':packageServiceId'] = $packageServiceId; |
| 78 | |
| 79 | try { |
| 80 | $statement = $this->connection->prepare( |
| 81 | "DELETE FROM {$this->table} WHERE 1 = 1 {$locations} AND packageServiceId = :packageServiceId" |
| 82 | ); |
| 83 | |
| 84 | return $statement->execute($params); |
| 85 | } catch (\Exception $e) { |
| 86 | throw new QueryExecutionException('Unable to delete data from ' . __CLASS__, $e->getCode(), $e); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 |