AddCategoryCommand.php
7 years ago
AddCategoryCommandHandler.php
2 years ago
DeleteCategoryCommand.php
7 years ago
DeleteCategoryCommandHandler.php
2 years ago
GetCategoriesCommand.php
7 years ago
GetCategoriesCommandHandler.php
2 years ago
GetCategoryCommand.php
7 years ago
GetCategoryCommandHandler.php
2 years ago
UpdateCategoriesPositionsCommand.php
7 years ago
UpdateCategoriesPositionsCommandHandler.php
2 years ago
UpdateCategoryCommand.php
7 years ago
UpdateCategoryCommandHandler.php
2 years ago
AddCategoryCommandHandler.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Application\Commands\Bookable\Category; |
| 4 | |
| 5 | use AmeliaBooking\Application\Commands\CommandHandler; |
| 6 | use AmeliaBooking\Application\Commands\CommandResult; |
| 7 | use AmeliaBooking\Domain\Collection\Collection; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 9 | use AmeliaBooking\Domain\Entity\Bookable\Service\Category; |
| 10 | use AmeliaBooking\Domain\Entity\Bookable\Service\Extra; |
| 11 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 12 | use AmeliaBooking\Domain\Entity\Entities; |
| 13 | use AmeliaBooking\Domain\Factory\Bookable\Service\CategoryFactory; |
| 14 | use AmeliaBooking\Application\Common\Exceptions\AccessDeniedException; |
| 15 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 16 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 17 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 18 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ExtraRepository; |
| 19 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 20 | |
| 21 | /** |
| 22 | * Class AddCategoryCommandHandler |
| 23 | * |
| 24 | * @package AmeliaBooking\Application\Commands\Bookable\Category |
| 25 | */ |
| 26 | class AddCategoryCommandHandler extends CommandHandler |
| 27 | { |
| 28 | protected $mandatoryFields = [ |
| 29 | 'name' |
| 30 | ]; |
| 31 | |
| 32 | /** |
| 33 | * @param AddCategoryCommand $command |
| 34 | * |
| 35 | * @return CommandResult |
| 36 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 37 | * @throws AccessDeniedException |
| 38 | * @throws QueryExecutionException |
| 39 | * @throws InvalidArgumentException |
| 40 | * @throws \Interop\Container\Exception\ContainerException |
| 41 | */ |
| 42 | public function handle(AddCategoryCommand $command) |
| 43 | { |
| 44 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) { |
| 45 | throw new AccessDeniedException('You are not allowed to add category.'); |
| 46 | } |
| 47 | |
| 48 | $result = new CommandResult(); |
| 49 | |
| 50 | $this->checkMandatoryFields($command); |
| 51 | |
| 52 | $categoryArray = $command->getFields(); |
| 53 | |
| 54 | $categoryArray = apply_filters('amelia_before_category_added_filter', $categoryArray); |
| 55 | |
| 56 | do_action('amelia_before_category_added', $categoryArray); |
| 57 | |
| 58 | /** @var Category $category */ |
| 59 | $category = CategoryFactory::create($categoryArray); |
| 60 | |
| 61 | if (!($category instanceof Category)) { |
| 62 | $result->setResult(CommandResult::RESULT_ERROR); |
| 63 | $result->setMessage('Could not create category.'); |
| 64 | |
| 65 | return $result; |
| 66 | } |
| 67 | |
| 68 | /** @var CategoryRepository $categoryRepository */ |
| 69 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 70 | /** @var ServiceRepository $serviceRepository */ |
| 71 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 72 | /** @var ExtraRepository $extraRepository */ |
| 73 | $extraRepository = $this->container->get('domain.bookable.extra.repository'); |
| 74 | |
| 75 | $categoryRepository->beginTransaction(); |
| 76 | |
| 77 | $categoryId = $categoryRepository->add($category); |
| 78 | |
| 79 | $category->setId(new Id($categoryId)); |
| 80 | |
| 81 | if ($category->getServiceList() && $category->getServiceList()->length()) { |
| 82 | foreach ($category->getServiceList()->getItems() as $service) { |
| 83 | /** @var Service $service */ |
| 84 | $service->setCategoryId(new Id($categoryId)); |
| 85 | |
| 86 | $serviceId = $serviceRepository->add($service); |
| 87 | |
| 88 | /** @var Extra $extra */ |
| 89 | foreach ($service->getExtras()->getItems() as $extra) { |
| 90 | $extra->setServiceId(new Id($serviceId)); |
| 91 | |
| 92 | $extraId = $extraRepository->add($extra); |
| 93 | |
| 94 | $extra->setId(new Id($extraId)); |
| 95 | } |
| 96 | |
| 97 | $service->setId(new Id($serviceId)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | $categoryRepository->commit(); |
| 102 | |
| 103 | do_action('amelia_after_category_added', $categoryArray); |
| 104 | |
| 105 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 106 | $result->setMessage('Successfully added new category.'); |
| 107 | $result->setData( |
| 108 | [ |
| 109 | Entities::CATEGORY => $category->toArray() |
| 110 | ] |
| 111 | ); |
| 112 | |
| 113 | return $result; |
| 114 | } |
| 115 | } |
| 116 |