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