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
DeleteServiceCommandHandler.php
93 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @copyright © TMS-Plugins. All rights reserved. |
| 4 | * @licence See LICENCE.md for license details. |
| 5 | */ |
| 6 | |
| 7 | namespace AmeliaBooking\Application\Commands\Bookable\Service; |
| 8 | |
| 9 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 10 | use AmeliaBooking\Application\Commands\CommandResult; |
| 11 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 12 | use AmeliaBooking\Application\Services\Bookable\BookableApplicationService; |
| 13 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 14 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 15 | use AmeliaBooking\Domain\Entity\Entities; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 18 | use Interop\Container\Exception\ContainerException; |
| 19 | use Slim\Exception\ContainerValueNotFoundException; |
| 20 | |
| 21 | /** |
| 22 | * Class DeleteServiceCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\Bookable\Service |
| 25 | */ |
| 26 | class DeleteServiceCommandHandler extends CommandHandler |
| 27 | { |
| 28 | /** |
| 29 | * @param DeleteServiceCommand $command |
| 30 | * |
| 31 | * @return CommandResult |
| 32 | * @throws ContainerValueNotFoundException |
| 33 | * @throws InvalidArgumentException |
| 34 | * @throws QueryExecutionException |
| 35 | * @throws AccessDeniedException |
| 36 | * @throws ContainerException |
| 37 | */ |
| 38 | public function handle(DeleteServiceCommand $command) |
| 39 | { |
| 40 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::SERVICES)) { |
| 41 | throw new AccessDeniedException('You are not allowed to delete services.'); |
| 42 | } |
| 43 | |
| 44 | $result = new CommandResult(); |
| 45 | |
| 46 | $this->checkMandatoryFields($command); |
| 47 | |
| 48 | /** @var BookableApplicationService $bookableApplicationService */ |
| 49 | $bookableApplicationService = $this->getContainer()->get('application.bookable.service'); |
| 50 | |
| 51 | $appointmentsCount = $bookableApplicationService->getAppointmentsCountForServices([$command->getArg('id')]); |
| 52 | |
| 53 | /** @var ServiceRepository $serviceRepository */ |
| 54 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 55 | |
| 56 | /** @var Service $service */ |
| 57 | $service = $serviceRepository->getByCriteria( |
| 58 | ['services' => [$command->getArg('id')]] |
| 59 | )->getItem($command->getArg('id')); |
| 60 | |
| 61 | if ($appointmentsCount['futureAppointments']) { |
| 62 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 63 | $result->setMessage('Could not delete service.'); |
| 64 | $result->setData([]); |
| 65 | |
| 66 | return $result; |
| 67 | } |
| 68 | |
| 69 | $serviceRepository->beginTransaction(); |
| 70 | |
| 71 | do_action('amelia_before_service_deleted', $service->toArray()); |
| 72 | |
| 73 | if (!$bookableApplicationService->deleteService($service)) { |
| 74 | $serviceRepository->rollback(); |
| 75 | |
| 76 | $result->setResult(CommandResult::RESULT_ERROR); |
| 77 | $result->setMessage('Could not delete service.'); |
| 78 | |
| 79 | return $result; |
| 80 | } |
| 81 | |
| 82 | $serviceRepository->commit(); |
| 83 | |
| 84 | do_action('amelia_after_service_deleted', $service->toArray()); |
| 85 | |
| 86 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 87 | $result->setMessage('Successfully deleted service.'); |
| 88 | $result->setData([]); |
| 89 | |
| 90 | return $result; |
| 91 | } |
| 92 | } |
| 93 |