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
GetCategoryCommandHandler.php
92 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\AbstractCollection; |
| 8 | use AmeliaBooking\Domain\Collection\Collection; |
| 9 | use AmeliaBooking\Domain\Entity\Bookable\Service\Category; |
| 10 | use AmeliaBooking\Domain\Entity\Bookable\Service\Service; |
| 11 | use AmeliaBooking\Domain\Entity\Entities; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\NotFoundException; |
| 13 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 14 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\CategoryRepository; |
| 15 | use AmeliaBooking\Infrastructure\Repository\Bookable\Service\ServiceRepository; |
| 16 | |
| 17 | /** |
| 18 | * Class GetCategoryCommandHandler |
| 19 | * |
| 20 | * @package AmeliaBooking\Application\Commands\Bookable\Category |
| 21 | */ |
| 22 | class GetCategoryCommandHandler extends CommandHandler |
| 23 | { |
| 24 | /** |
| 25 | * @param GetCategoryCommand $command |
| 26 | * |
| 27 | * @return CommandResult |
| 28 | * @throws \Slim\Exception\ContainerValueNotFoundException |
| 29 | * @throws NotFoundException |
| 30 | * @throws QueryExecutionException |
| 31 | * @throws \AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException |
| 32 | * @throws \Interop\Container\Exception\ContainerException |
| 33 | */ |
| 34 | public function handle(GetCategoryCommand $command) |
| 35 | { |
| 36 | $result = new CommandResult(); |
| 37 | |
| 38 | /** @var CategoryRepository $categoryRepository */ |
| 39 | $categoryRepository = $this->container->get('domain.bookable.category.repository'); |
| 40 | /** @var ServiceRepository $serviceRepository */ |
| 41 | $serviceRepository = $this->container->get('domain.bookable.service.repository'); |
| 42 | |
| 43 | /** |
| 44 | * Get services for category |
| 45 | */ |
| 46 | $services = $serviceRepository->getByCriteria(['categories' => [$command->getArg('id')]]); |
| 47 | |
| 48 | if (!$services instanceof AbstractCollection) { |
| 49 | $result->setResult(CommandResult::RESULT_ERROR); |
| 50 | $result->setMessage('Could not get services.'); |
| 51 | |
| 52 | return $result; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get category |
| 57 | */ |
| 58 | $category = $categoryRepository->getById($command->getArg('id')); |
| 59 | |
| 60 | if (!$category instanceof Category) { |
| 61 | $result->setResult(CommandResult::RESULT_ERROR); |
| 62 | $result->setMessage('Could not get category.'); |
| 63 | |
| 64 | return $result; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Add services to category |
| 69 | */ |
| 70 | $category->setServiceList(new Collection()); |
| 71 | |
| 72 | /** @var Service $service */ |
| 73 | foreach ($services->getItems() as $service) { |
| 74 | $category->getServiceList()->addItem($service, $service->getId()->getValue()); |
| 75 | } |
| 76 | |
| 77 | $categoryArray = $category->toArray(); |
| 78 | |
| 79 | $categoryArray = apply_filters('amelia_get_category_filter', $categoryArray); |
| 80 | |
| 81 | do_action('amelia_get_category', $categoryArray); |
| 82 | |
| 83 | $result->setResult(CommandResult::RESULT_SUCCESS); |
| 84 | $result->setMessage('Successfully retrieved category.'); |
| 85 | $result->setData([ |
| 86 | Entities::CATEGORY => $categoryArray |
| 87 | ]); |
| 88 | |
| 89 | return $result; |
| 90 | } |
| 91 | } |
| 92 |