getContainer()->getPermissionsService()->currentUserCanDelete(Entities::SERVICES)) { throw new AccessDeniedException('You are not allowed to delete bookable category.'); } $result = new CommandResult(); /** @var CategoryRepository $categoryRepository */ $categoryRepository = $this->container->get('domain.bookable.category.repository'); /** @var ServiceRepository $serviceRepository */ $serviceRepository = $this->container->get('domain.bookable.service.repository'); /** @var BookableApplicationService $bookableAS */ $bookableAS = $this->getContainer()->get('application.bookable.service'); /** @var GalleryApplicationService $galleryService */ $galleryService = $this->container->get('application.gallery.service'); $categoryRepository->beginTransaction(); /** @var Category $category */ $category = $categoryRepository->getById($command->getArg('id')); if (!$category instanceof Category) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not delete bookable category.'); return $result; } /** @var Collection $services */ $services = $serviceRepository->getByCriteria(['categories' => [$command->getArg('id')]]); $categoryServiceIds = []; foreach ($services->keys() as $serviceKey) { $categoryServiceIds[] = $services->getItem($serviceKey)->getId()->getValue(); } if ($categoryServiceIds) { $appointmentsCount = $bookableAS->getAppointmentsCountForServices($categoryServiceIds); if ($appointmentsCount['futureAppointments']) { $result->setResult(CommandResult::RESULT_CONFLICT); $result->setMessage('Could not delete category.'); $result->setData([]); return $result; } } if (!$categoryRepository->delete($command->getArg('id'))) { $categoryRepository->rollback(); } /** @var Service $service */ foreach ($services->getItems() as $service) { $galleryService->manageGalleryForEntityDelete($service->getGallery()); } $categoryRepository->commit(); $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully deleted bookable category.'); return $result; } }