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