getContainer()->getPermissionsService()->currentUserCanDelete(Entities::SERVICES)) { throw new AccessDeniedException('You are not allowed to delete services.'); } $result = new CommandResult(); $this->checkMandatoryFields($command); /** @var BookableApplicationService $bookableAS */ $bookableAS = $this->getContainer()->get('application.bookable.service'); /** @var GalleryApplicationService $galleryService */ $galleryService = $this->container->get('application.gallery.service'); $serviceId = $command->getArg('id'); $appointmentsCount = $bookableAS->getAppointmentsCountForServices([$serviceId]); /** @var ServiceRepository $serviceRepository */ $serviceRepository = $this->container->get('domain.bookable.service.repository'); $service = $serviceRepository->getByCriteria( ['services' => [$serviceId]] )->getItem($serviceId); if ($appointmentsCount['futureAppointments']) { $result->setResult(CommandResult::RESULT_CONFLICT); $result->setMessage('Could not delete service.'); $result->setData([]); return $result; } $serviceRepository->beginTransaction(); if (!$serviceRepository->delete($command->getArg('id'))) { $serviceRepository->rollback(); $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not delete service.'); return $result; } $galleryService->manageGalleryForEntityDelete($service->getGallery()); $serviceRepository->commit(); $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully deleted service.'); $result->setData([]); return $result; } }