AddServiceCommand.php
6 months ago
AddServiceCommandHandler.php
6 months ago
DeleteServiceCommand.php
6 months ago
DeleteServiceCommandHandler.php
3 months ago
GetServiceCommand.php
6 months ago
GetServiceCommandHandler.php
6 months ago
GetServiceDeleteEffectCommand.php
1 year ago
GetServiceDeleteEffectCommandHandler.php
3 months ago
GetServicesCommand.php
6 months ago
GetServicesCommandHandler.php
2 weeks 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
DeleteServiceCommandHandler.php
108 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\Application\Services\Bookable\BookableApplicationService; |
| 14 | use AmeliaBooking\Domain\Collection\Collection; |
| 15 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 16 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 17 | use AmeliaBooking\Domain\Entity\Entities; |
| 18 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 19 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\PackageRepository; |
| 20 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 21 | use Slim\Exception\ContainerValueNotFoundException; |
| 22 | |
| 23 | /** |
| 24 | * Class DeleteServiceCommandHandler |
| 25 | * |
| 26 | * @package AmeliaBooking\Application\Commands\Bookable\Service |
| 27 | */ |
| 28 | class DeleteServiceCommandHandler extends CommandHandler |
| 29 | { |
| 30 | /** |
| 31 | * @param DeleteServiceCommand $command |
| 32 | * |
| 33 | * @return CommandResult |
| 34 | * @throws ContainerValueNotFoundException |
| 35 | * @throws InvalidArgumentException |
| 36 | * @throws QueryExecutionException |
| 37 | * @throws AccessDeniedException |
| 38 | */ |
| 39 | public function handle(DeleteServiceCommand $command) |
| 40 | { |
| 41 | if (!$command->getPermissionService()->currentUserCanDelete(Entities::SERVICES)) { |
| 42 | throw new AccessDeniedException('You are not allowed to delete services.'); |
| 43 | } |
| 44 | |
| 45 | $result = new CommandResult(); |
| 46 | |
| 47 | $this->checkMandatoryFields($command); |
| 48 | |
| 49 | /** @var BookableApplicationService $bookableApplicationService */ |
| 50 | $bookableApplicationService = $this->getContainer()->get('application.bookable.service'); |
| 51 | |
| 52 | $appointmentsCount = $bookableApplicationService->getAppointmentsCountForServices([$command->getArg('id')]); |
| 53 | |
| 54 | if ($appointmentsCount['futureAppointments']) { |
| 55 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 56 | $result->setMessage('Could not delete service.'); |
| 57 | $result->setData([]); |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | |
| 62 | /** @var PackageRepository $packageRepository */ |
| 63 | $packageRepository = $this->container->get('domain.bookable.package.repository'); |
| 64 | |
| 65 | /** @var Collection $packages */ |
| 66 | $packages = $packageRepository->getByCriteria(['services' => [$command->getArg('id')]]); |
| 67 | |
| 68 | if ($packages->length()) { |
| 69 | $result->setResult(CommandResult::RESULT_CONFLICT); |
| 70 | $result->setMessage('Could not delete service.'); |
| 71 | $result->setData([]); |
| 72 | |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | /** @var ServiceRepository $serviceRepository */ |
| 77 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 78 | |
| 79 | /** @var Service $service */ |
| 80 | $service = $serviceRepository->getByCriteria( |
| 81 | ['services' => [$command->getArg('id')]] |
| 82 | )->getItem($command->getArg('id')); |
| 83 | |
| 84 | $serviceRepository->beginTransaction(); |
| 85 | |
| 86 | do_action('amelia_before_service_deleted', $service->toArray()); |
| 87 | |
| 88 | if (!$bookableApplicationService->deleteService($service)) { |
| 89 | $serviceRepository->rollback(); |
| 90 | |
| 91 | $result->setResult(CommandResult::RESULT_ERROR); |
| 92 | $result->setMessage('Could not delete service.'); |
| 93 | |
| 94 | return $result; |
| 95 | } |
| 96 | |
| 97 | $serviceRepository->commit(); |
| 98 | |
| 99 | do_action('amelia_after_service_deleted', $service->toArray()); |
| 100 | |
| 101 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 102 | $result->setMessage('Successfully deleted service.'); |
| 103 | $result->setData([]); |
| 104 | |
| 105 | return $result; |
| 106 | } |
| 107 | } |
| 108 |