getContainer()->getPermissionsService()->currentUserCanWrite(Entities::SERVICES)) { throw new AccessDeniedException('You are not allowed to add service.'); } $result = new CommandResult(); $this->checkMandatoryFields($command); $service = ServiceFactory::create($command->getFields()); if (!$service instanceof Service) { $result->setResult(CommandResult::RESULT_ERROR); $result->setMessage('Could not create service.'); return $result; } /** @var ServiceRepository $serviceRepository */ $serviceRepository = $this->container->get('domain.bookable.service.repository'); /** @var BookableApplicationService $bookableService */ $bookableService = $this->container->get('application.bookable.service'); /** @var GalleryApplicationService $galleryService */ $galleryService = $this->container->get('application.gallery.service'); /** @var ProviderRepository $providerRepository */ $providerRepository = $this->container->get('domain.users.providers.repository'); $serviceRepository->beginTransaction(); if (!($serviceId = $serviceRepository->add($service))) { $serviceRepository->rollback(); } $service->setId(new Id($serviceId)); $providers = $command->getField('providers') ? $providerRepository->getFiltered(['providers' => $command->getField('providers')], 0) : new Collection(); $bookableService->manageProvidersForServiceAdd($service, $providers); $bookableService->manageExtrasForServiceAdd($service); $galleryService->manageGalleryForEntityAdd($service->getGallery(), $serviceId); $serviceRepository->commit(); $result->setResult(CommandResult::RESULT_SUCCESS); $result->setMessage('Successfully added new service.'); $result->setData( [ Entities::SERVICE => $service->toArray(), ] ); return $result; } }