CalendarProviderService.php
78 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Services\Calendar; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 6 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 7 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 8 | use AmeliaBooking\Infrastructure\Common\Container; |
| 9 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 10 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 11 | |
| 12 | class CalendarProviderService |
| 13 | { |
| 14 | /** @var Container */ |
| 15 | private $container; |
| 16 | |
| 17 | public function __construct(Container $container) |
| 18 | { |
| 19 | $this->container = $container; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Returns providers visible in calendar context with the same criteria used by calendar slots. |
| 24 | * |
| 25 | * @param array $queryParams |
| 26 | * @param bool $includeDatesAsRange |
| 27 | * |
| 28 | * @return Provider[] |
| 29 | * @throws InvalidArgumentException |
| 30 | * @throws QueryExecutionException |
| 31 | */ |
| 32 | public function getVisibleProviders(array $queryParams, bool $includeDatesAsRange = false): array |
| 33 | { |
| 34 | $locationRepository = $this->container->get('domain.locations.repository'); |
| 35 | /** @var ProviderRepository $providerRepository */ |
| 36 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 37 | |
| 38 | $queryParams['locations'] = array_map( |
| 39 | static fn($location) => $location['id'], |
| 40 | $locationRepository->getFiltered( |
| 41 | ['status' => !empty($queryParams['providers']) ? null : Status::VISIBLE], |
| 42 | 0 |
| 43 | )->toArray() |
| 44 | ); |
| 45 | |
| 46 | if ($includeDatesAsRange) { |
| 47 | $queryParams['dates'] = [$queryParams['calendarStartDate'], $queryParams['calendarEndDate']]; |
| 48 | } |
| 49 | |
| 50 | $criteria = ['providerStatus' => !empty($queryParams['providers']) ? null : Status::VISIBLE]; |
| 51 | foreach ($queryParams as $key => $value) { |
| 52 | if ($key !== 'providerStatus') { |
| 53 | $criteria[$key] = $value; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return $providerRepository->getWithSchedule($criteria)->getItems(); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param array $queryParams |
| 62 | * |
| 63 | * @return int[] |
| 64 | * @throws InvalidArgumentException |
| 65 | * @throws QueryExecutionException |
| 66 | */ |
| 67 | public function getVisibleProviderIds(array $queryParams): array |
| 68 | { |
| 69 | $providerIds = []; |
| 70 | |
| 71 | foreach ($this->getVisibleProviders($queryParams, true) as $provider) { |
| 72 | $providerIds[] = $provider->getId()->getValue(); |
| 73 | } |
| 74 | |
| 75 | return $providerIds; |
| 76 | } |
| 77 | } |
| 78 |