ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Category
/
UpdateCategoryCommandHandler.php
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
UpdateCategoryCommandHandler.php
72 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\Application\Common\Exceptions\AccessDeniedException; |
| 8 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 9 | use AmeliaBooking\Domain\Entity\Bookable\Service\Category; |
| 10 | use AmeliaBooking\Domain\Entity\Entities; |
| 11 | use AmeliaBooking\Domain\Factory\Bookable\Service\CategoryFactory; |
| 12 | use AmeliaBooking\Domain\ValueObjects\Number\Integer\Id; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 15 | |
| 16 | /** |
| 17 | * Class UpdateCategoryCommandHandler |
| 18 | * |
| 19 | * @package AmeliaBooking\Application\Commands\Bookable\Category |
| 20 | */ |
| 21 | class UpdateCategoryCommandHandler extends CommandHandler |
| 22 | { |
| 23 | /** |
| 24 | * @param UpdateCategoryCommand $command |
| 25 | * |
| 26 | * @return CommandResult |
| 27 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 28 | * @throws AccessDeniedException |
| 29 | * @throws QueryExecutionException |
| 30 | * @throws \Interop\Container\Exception\ContainerException |
| 31 | * @throws InvalidArgumentException |
| 32 | */ |
| 33 | public function handle(UpdateCategoryCommand $command) |
| 34 | { |
| 35 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) { |
| 36 | throw new AccessDeniedException('You are not allowed to update bookable category.'); |
| 37 | } |
| 38 | |
| 39 | $result = new CommandResult(); |
| 40 | |
| 41 | $categoryArray = $command->getFields(); |
| 42 | |
| 43 | $categoryArray = apply_filters('amelia_before_category_updated_filter', $categoryArray); |
| 44 | |
| 45 | do_action('amelia_before_category_updated', $categoryArray); |
| 46 | |
| 47 | $category = CategoryFactory::create($categoryArray); |
| 48 | if (!$category instanceof Category) { |
| 49 | $result->setResult(CommandResult::RESULT_ERROR); |
| 50 | $result->setMessage('Could not update bookable category.'); |
| 51 | |
| 52 | return $result; |
| 53 | } |
| 54 | |
| 55 | /** @var CategoryRepository $categoryRepository */ |
| 56 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 57 | if ($categoryRepository->update($command->getArg('id'), $category)) { |
| 58 | $category->setId(new Id($command->getArg('id'))); |
| 59 | |
| 60 | do_action('amelia_after_category_updated', $category->toArray()); |
| 61 | |
| 62 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 63 | $result->setMessage('Successfully updated bookable category.'); |
| 64 | $result->setData([ |
| 65 | Entities::CATEGORY => $category->toArray() |
| 66 | ]); |
| 67 | } |
| 68 | |
| 69 | return $result; |
| 70 | } |
| 71 | } |
| 72 |