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
AddServiceCommandHandler.php
143 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\Application\Services\Entity\EntityApplicationService; |
| 14 | use AmeliaBooking\Application\Services\Extra\AbstractExtraApplicationService; |
| 15 | use AmeliaBooking\Application\Services\Gallery\GalleryApplicationService; |
| 16 | use AmeliaBooking\Domain\Collection\Collection; |
| 17 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 18 | use AmeliaBooking\Domain\Entity\Bookable\Service\Category; |
| 19 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 20 | use AmeliaBooking\Domain\Entity\Entities; |
| 21 | use AmeliaBooking\Domain\Factory\Bookable\Service\ServiceFactory; |
| 22 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 23 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 24 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 25 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 26 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 27 | use AmeliaBooking\Infrastructure\Repository\User\ProviderRepository; |
| 28 | use Interop\Container\Exception\ContainerException; |
| 29 | use Slim\Exception\ContainerValueNotFoundException; |
| 30 | |
| 31 | /** |
| 32 | * Class AddServiceCommandHandler |
| 33 | * |
| 34 | * @package AmeliaBooking\Application\Commands\Bookable\Service |
| 35 | */ |
| 36 | class AddServiceCommandHandler extends CommandHandler |
| 37 | { |
| 38 | /** @var array */ |
| 39 | public $mandatoryFields = [ |
| 40 | 'categoryId', |
| 41 | 'duration', |
| 42 | 'maxCapacity', |
| 43 | 'minCapacity', |
| 44 | 'name', |
| 45 | 'price', |
| 46 | 'providers' |
| 47 | ]; |
| 48 | |
| 49 | /** |
| 50 | * @param AddServiceCommand $command |
| 51 | * |
| 52 | * @return CommandResult |
| 53 | * @throws ContainerValueNotFoundException |
| 54 | * @throws InvalidArgumentException |
| 55 | * @throws QueryExecutionException |
| 56 | * @throws AccessDeniedException |
| 57 | * @throws ContainerException |
| 58 | * @throws NotFoundException |
| 59 | */ |
| 60 | public function handle(AddServiceCommand $command) |
| 61 | { |
| 62 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) { |
| 63 | throw new AccessDeniedException('You are not allowed to add service.'); |
| 64 | } |
| 65 | |
| 66 | $result = new CommandResult(); |
| 67 | |
| 68 | $this->checkMandatoryFields($command); |
| 69 | |
| 70 | $serviceData = $command->getFields(); |
| 71 | |
| 72 | /** @var EntityApplicationService $entityService */ |
| 73 | $entityService = $this->container->get('application.entity.service'); |
| 74 | |
| 75 | $entityService->removeMissingEntitiesForService($serviceData); |
| 76 | |
| 77 | $serviceData = apply_filters('amelia_before_service_added_filter', $serviceData); |
| 78 | |
| 79 | do_action('amelia_before_service_added', $serviceData); |
| 80 | |
| 81 | /** @var Service $service */ |
| 82 | $service = ServiceFactory::create($serviceData); |
| 83 | |
| 84 | if (!($service instanceof Service)) { |
| 85 | $result->setResult(CommandResult::RESULT_ERROR); |
| 86 | $result->setMessage('Could not create service.'); |
| 87 | |
| 88 | return $result; |
| 89 | } |
| 90 | |
| 91 | /** @var ServiceRepository $serviceRepository */ |
| 92 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 93 | /** @var BookableApplicationService $bookableService */ |
| 94 | $bookableService = $this->container->get('application.bookable.service'); |
| 95 | /** @var AbstractExtraApplicationService $extraService */ |
| 96 | $extraService = $this->container->get('application.extra.service'); |
| 97 | /** @var GalleryApplicationService $galleryService */ |
| 98 | $galleryService = $this->container->get('application.gallery.service'); |
| 99 | /** @var ProviderRepository $providerRepository */ |
| 100 | $providerRepository = $this->container->get('domain.users.providers.repository'); |
| 101 | /** @var CategoryRepository $categoryRepository */ |
| 102 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 103 | |
| 104 | $serviceRepository->beginTransaction(); |
| 105 | |
| 106 | /** @var Category $category */ |
| 107 | $category = $categoryRepository->getById($service->getCategoryId()->getValue()); |
| 108 | |
| 109 | if (!$category || !($serviceId = $serviceRepository->add($service))) { |
| 110 | $serviceRepository->rollback(); |
| 111 | |
| 112 | $result->setResult(CommandResult::RESULT_ERROR); |
| 113 | $result->setMessage('Could not create service.'); |
| 114 | |
| 115 | return $result; |
| 116 | } |
| 117 | |
| 118 | $service->setId(new Id($serviceId)); |
| 119 | |
| 120 | /** @var Collection $providers */ |
| 121 | $providers = $command->getField('providers') ? |
| 122 | $providerRepository->getFiltered(['providers' => $command->getField('providers')], 0) : new Collection(); |
| 123 | |
| 124 | $bookableService->manageProvidersForServiceAdd($service, $providers); |
| 125 | $extraService->manageExtrasForServiceAdd($service); |
| 126 | $galleryService->manageGalleryForEntityAdd($service->getGallery(), $serviceId); |
| 127 | |
| 128 | $serviceRepository->commit(); |
| 129 | |
| 130 | do_action('amelia_after_service_added', $service->toArray()); |
| 131 | |
| 132 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 133 | $result->setMessage('Successfully added new service.'); |
| 134 | $result->setData( |
| 135 | [ |
| 136 | Entities::SERVICE => $service->toArray(), |
| 137 | ] |
| 138 | ); |
| 139 | |
| 140 | return $result; |
| 141 | } |
| 142 | } |
| 143 |