ameliabooking
/
src
/
Application
/
Commands
/
Bookable
/
Category
/
UpdateCategoryCommandHandler.php
AddCategoryCommand.php
1 year ago
AddCategoryCommandHandler.php
6 months ago
DeleteCategoryCommand.php
1 year ago
DeleteCategoryCommandHandler.php
6 months ago
GetCategoriesCommand.php
1 year ago
GetCategoriesCommandHandler.php
6 months ago
GetCategoryCommand.php
1 year ago
GetCategoryCommandHandler.php
6 months ago
GetCategoryDeleteEffectCommand.php
6 months ago
GetCategoryDeleteEffectCommandHandler.php
3 months ago
UpdateCategoriesPositionsCommand.php
1 year ago
UpdateCategoriesPositionsCommandHandler.php
6 months ago
UpdateCategoryCommand.php
1 year ago
UpdateCategoryCommandHandler.php
6 months ago
UpdateCategoryCommandHandler.php
73 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 InvalidArgumentException |
| 31 | */ |
| 32 | public function handle(UpdateCategoryCommand $command) |
| 33 | { |
| 34 | if (!$command->getPermissionService()->currentUserCanWrite(Entities::SERVICES)) { |
| 35 | throw new AccessDeniedException('You are not allowed to update bookable category.'); |
| 36 | } |
| 37 | |
| 38 | $result = new CommandResult(); |
| 39 | |
| 40 | $categoryArray = $command->getFields(); |
| 41 | |
| 42 | $categoryArray = apply_filters('amelia_before_category_updated_filter', $categoryArray); |
| 43 | |
| 44 | do_action('amelia_before_category_updated', $categoryArray); |
| 45 | |
| 46 | $category = CategoryFactory::create($categoryArray); |
| 47 | if (!$category instanceof Category) { |
| 48 | $result->setResult(CommandResult::RESULT_ERROR); |
| 49 | $result->setMessage('Could not update bookable category.'); |
| 50 | |
| 51 | return $result; |
| 52 | } |
| 53 | |
| 54 | /** @var CategoryRepository $categoryRepository */ |
| 55 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 56 | if ($categoryRepository->update($command->getArg('id'), $category)) { |
| 57 | $category->setId(new Id($command->getArg('id'))); |
| 58 | |
| 59 | do_action('amelia_after_category_updated', $category->toArray()); |
| 60 | |
| 61 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 62 | $result->setMessage('Successfully updated bookable category.'); |
| 63 | $result->setData( |
| 64 | [ |
| 65 | Entities::CATEGORY => $category->toArray() |
| 66 | ] |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | return $result; |
| 71 | } |
| 72 | } |
| 73 |