ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Service
/
UpdateServicesPositionsCommandHandler.php
AddServiceCommand.php
7 years ago
AddServiceCommandHandler.php
2 years ago
DeleteServiceCommand.php
7 years ago
DeleteServiceCommandHandler.php
2 years ago
GetServiceCommand.php
7 years ago
GetServiceCommandHandler.php
2 years ago
GetServiceDeleteEffectCommand.php
7 years ago
GetServiceDeleteEffectCommandHandler.php
2 years ago
GetServicesCommand.php
7 years ago
GetServicesCommandHandler.php
2 years ago
UpdateServiceCommand.php
7 years ago
UpdateServiceCommandHandler.php
2 years ago
UpdateServiceStatusCommand.php
7 years ago
UpdateServiceStatusCommandHandler.php
2 years ago
UpdateServicesPositionsCommand.php
7 years ago
UpdateServicesPositionsCommandHandler.php
2 years ago
UpdateServicesPositionsCommandHandler.php
103 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Bookable\Service; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Domain\Collection\Collection; |
| 9 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 10 | use AmeliaBooking\Domain\Entity\Entities; |
| 11 | use AmeliaBooking\Domain\Services\Settings\SettingsService; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 13 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 14 | use Interop\Container\Exception\ContainerException; |
| 15 | use Slim\Exception\ContainerValueNotFoundException; |
| 16 | |
| 17 | /** |
| 18 | * Class UpdateServicesPositionsCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Bookable\Category |
| 21 | */ |
| 22 | class UpdateServicesPositionsCommandHandler extends CommandHandler |
| 23 | { |
| 24 | /** |
| 25 | * @param UpdateServicesPositionsCommand $command |
| 26 | * |
| 27 | * @return CommandResult |
| 28 | * @throws ContainerValueNotFoundException |
| 29 | * @throws AccessDeniedException |
| 30 | * @throws QueryExecutionException |
| 31 | * @throws ContainerException |
| 32 | * @throws InvalidArgumentException |
| 33 | */ |
| 34 | public function handle(UpdateServicesPositionsCommand $command) |
| 35 | { |
| 36 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) { |
| 37 | throw new AccessDeniedException('You are not allowed to update bookable services positions.'); |
| 38 | } |
| 39 | |
| 40 | $result = new CommandResult(); |
| 41 | |
| 42 | /** @var ServiceRepository $serviceRepository */ |
| 43 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 44 | |
| 45 | /** @var Collection $services */ |
| 46 | $services = $serviceRepository->getFiltered(['sort' => $command->getFields()['sorting']]); |
| 47 | |
| 48 | $servicesArray = $services->toArray(); |
| 49 | |
| 50 | if ($command->getFields()['sorting'] === 'custom' && |
| 51 | $customSortedServicesArray = $command->getFields()['services'] |
| 52 | ) { |
| 53 | $customSortedServicesIds = array_column($customSortedServicesArray, 'id'); |
| 54 | |
| 55 | $sortedServicesArray = []; |
| 56 | |
| 57 | foreach ($servicesArray as $serviceArray) { |
| 58 | if (in_array($serviceArray['id'], $customSortedServicesIds, false)) { |
| 59 | $sortedServicesArray[] = null; |
| 60 | } else { |
| 61 | $sortedServicesArray[] = $serviceArray; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | foreach ($sortedServicesArray as $index => $serviceArray) { |
| 66 | if ($serviceArray === null) { |
| 67 | $sortedServicesArray[$index] = array_shift($customSortedServicesArray); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | $servicesArray = $sortedServicesArray; |
| 72 | } |
| 73 | |
| 74 | $serviceRepository->beginTransaction(); |
| 75 | |
| 76 | $servicesArray = apply_filters('amelia_before_service_position_updated_filter', $servicesArray); |
| 77 | |
| 78 | do_action('amelia_before_service_position_updated', $servicesArray); |
| 79 | |
| 80 | foreach ($servicesArray as $index => $serviceArray) { |
| 81 | $serviceRepository->updateFieldById($serviceArray['id'], $index + 1, 'position'); |
| 82 | } |
| 83 | |
| 84 | $serviceRepository->commit(); |
| 85 | |
| 86 | do_action('amelia_after_service_position_updated', $servicesArray); |
| 87 | |
| 88 | /** @var SettingsService $settingsService */ |
| 89 | $settingsService = $this->getContainer()->get('domain.settings.service'); |
| 90 | |
| 91 | $settings = $settingsService->getAllSettingsCategorized(); |
| 92 | |
| 93 | $settings['general']['sortingServices'] = $command->getFields()['sorting']; |
| 94 | |
| 95 | $settingsService->setAllSettings($settings); |
| 96 | |
| 97 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 98 | $result->setMessage('Successfully updated bookable services positions.'); |
| 99 | |
| 100 | return $result; |
| 101 | } |
| 102 | } |
| 103 |