AddServiceCommand.php
6 months ago
AddServiceCommandHandler.php
6 months ago
DeleteServiceCommand.php
6 months ago
DeleteServiceCommandHandler.php
6 months ago
GetServiceCommand.php
6 months ago
GetServiceCommandHandler.php
6 months ago
GetServiceDeleteEffectCommand.php
1 year ago
GetServiceDeleteEffectCommandHandler.php
6 months ago
GetServicesCommand.php
6 months ago
GetServicesCommandHandler.php
6 months ago
UpdateServiceCommand.php
6 months ago
UpdateServiceCommandHandler.php
6 months ago
UpdateServiceStatusCommand.php
6 months ago
UpdateServiceStatusCommandHandler.php
6 months ago
UpdateServicesPositionsCommand.php
1 year ago
UpdateServicesPositionsCommandHandler.php
1 year ago
GetServicesCommandHandler.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * @copyright © Melograno Ventures. All rights reserved. |
| 5 | * @licence See LICENCE.md for license details. |
| 6 | */ |
| 7 | |
| 8 | namespace AmeliaBooking\Application\Commands\Bookable\Service; |
| 9 | |
| 10 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 11 | use AmeliaBooking\Application\Commands\CommandResult; |
| 12 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 13 | use AmeliaBooking\Domain\Collection\Collection; |
| 14 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 15 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 16 | use AmeliaBooking\Domain\Entity\Entities; |
| 17 | use AmeliaBooking\Domain\Entity\User\Provider; |
| 18 | use AmeliaBooking\Domain\ValueObjects\String\Status; |
| 19 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 20 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 21 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 22 | use Interop\Container\Exception\ContainerException; |
| 23 | use Slim\Exception\ContainerValueNotFoundException; |
| 24 | |
| 25 | /** |
| 26 | * Class GetServicesCommandHandler |
| 27 | * |
| 28 | * @package AmeliaBooking\Application\Commands\Bookable\Service |
| 29 | */ |
| 30 | class GetServicesCommandHandler extends CommandHandler |
| 31 | { |
| 32 | /** |
| 33 | * @param GetServicesCommand $command |
| 34 | * |
| 35 | * @return CommandResult |
| 36 | * @throws ContainerValueNotFoundException |
| 37 | * @throws QueryExecutionException |
| 38 | * @throws InvalidArgumentException |
| 39 | * @throws AccessDeniedException |
| 40 | * @throws ContainerException |
| 41 | */ |
| 42 | public function handle(GetServicesCommand $command) |
| 43 | { |
| 44 | if (!$command->getPermissionService()->currentUserCanRead(Entities::SERVICES)) { |
| 45 | throw new AccessDeniedException('You are not allowed to read services.'); |
| 46 | } |
| 47 | |
| 48 | $result = new CommandResult(); |
| 49 | |
| 50 | $this->checkMandatoryFields($command); |
| 51 | |
| 52 | /** @var ServiceRepository $serviceRepository */ |
| 53 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 54 | |
| 55 | /** @var ProviderRepository $providerRepository */ |
| 56 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 57 | |
| 58 | $params = $command->getField('params'); |
| 59 | |
| 60 | $itemsPerPage = !empty($params['limit']) ? $params['limit'] : 10; |
| 61 | |
| 62 | /** @var Collection $services */ |
| 63 | $services = $serviceRepository->getFiltered( |
| 64 | array_merge( |
| 65 | $params, |
| 66 | [ |
| 67 | 'sort' => !empty($command->getField('params')['sort']) |
| 68 | ? $command->getField('params')['sort'] |
| 69 | : 'idAsc', |
| 70 | ] |
| 71 | ), |
| 72 | $itemsPerPage |
| 73 | ); |
| 74 | |
| 75 | /** @var Service $service */ |
| 76 | foreach ($services->getItems() as $service) { |
| 77 | if ($service->getSettings() && json_decode($service->getSettings()->getValue(), true) === null) { |
| 78 | $service->setSettings(null); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** @var Collection $allProviders */ |
| 83 | $allProviders = $providerRepository->getByFieldValue('type', Entities::PROVIDER); |
| 84 | |
| 85 | $providersServices = $providerRepository->getProvidersServices($services->keys()); |
| 86 | |
| 87 | $servicesArray = $services->toArray(); |
| 88 | |
| 89 | // Get providers for each service |
| 90 | foreach ($servicesArray as &$serviceData) { |
| 91 | /** @var Collection $providers */ |
| 92 | $providers = new Collection(); |
| 93 | |
| 94 | foreach ($providersServices as $providerId => $providerServices) { |
| 95 | if (!empty($providerServices[$serviceData['id']])) { |
| 96 | /** @var Provider $provider */ |
| 97 | $provider = $allProviders->getItem($providerId); |
| 98 | |
| 99 | if ($provider->getStatus()->getValue() === Status::VISIBLE) { |
| 100 | $providers->addItem($provider, $providerId); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | $serviceData['employees'] = array_map(function ($provider) { |
| 106 | return [ |
| 107 | 'id' => $provider['id'], |
| 108 | 'firstName' => $provider['firstName'], |
| 109 | 'lastName' => $provider['lastName'], |
| 110 | 'picture' => $provider['pictureThumbPath'], |
| 111 | ]; |
| 112 | }, $providers->toArray()); |
| 113 | } |
| 114 | |
| 115 | $servicesArray = apply_filters('amelia_get_services_filter', $servicesArray); |
| 116 | |
| 117 | do_action('amelia_get_services', $servicesArray); |
| 118 | |
| 119 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 120 | $result->setMessage('Successfully retrieved services.'); |
| 121 | $result->setData( |
| 122 | [ |
| 123 | Entities::SERVICES => $servicesArray, |
| 124 | 'countFiltered' => (int)$serviceRepository->getCount($command->getField('params')), |
| 125 | 'countTotalByCategory' => (int)$serviceRepository->getCount([ |
| 126 | 'categoryId' => !empty($command->getField('params')['categoryId']) |
| 127 | ? $command->getField('params')['categoryId'] |
| 128 | : null, |
| 129 | ]), |
| 130 | 'countTotal' => (int)$serviceRepository->getCount([]), |
| 131 | ] |
| 132 | ); |
| 133 | |
| 134 | return $result; |
| 135 | } |
| 136 | } |
| 137 |