src/Ui/Http/Rest/Controller/Article/GetTaxonomyByCodeAction.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Ui\Http\Rest\Controller\Article;
  4. use App\Blog\Application\Query\GetTaxonByCode\GetTaxonByCodeQuery;
  5. use App\Shared\Application\Query\QueryBusInterface;
  6. use FOS\RestBundle\View\View;
  7. use FOS\RestBundle\View\ViewHandlerInterface;
  8. use Sylius\Component\Channel\Context\ChannelContextInterface;
  9. use Sylius\Component\Locale\Context\LocaleContextInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. use Symfony\Contracts\Cache\ItemInterface;
  14. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  15. final class GetTaxonomyByCodeAction
  16. {
  17.     public function __construct(
  18.         private QueryBusInterface $queryBus,
  19.         private ViewHandlerInterface $viewHandler,
  20.         private ChannelContextInterface $channelContext,
  21.         private LocaleContextInterface $localeContext,
  22.         private TagAwareCacheInterface $cache
  23.     ) {
  24.     }
  25.     public function __invoke(string $codeRequest $request) : Response
  26.     {
  27.         try {
  28.             $route $request->attributes->get('_route');
  29.             $page $request->query->getInt('page'1);
  30.             $limit $request->query->getInt('limit'8);
  31.             return $this->cache->get(
  32.                 urlencode("$route-$code-$page-$limit"),
  33.                 function (ItemInterface $item) use ($code$request$route$page$limit) {
  34.                     $item->tag([
  35.                         'article-category',
  36.                         "article-category-by-code-$code"
  37.                     ]);
  38.                     return $this->viewHandler->handle(View::create(
  39.                         $this->queryBus->ask(
  40.                             new GetTaxonByCodeQuery(
  41.                                 $code,
  42.                                 $this->channelContext->getChannel()->getCode(),
  43.                                 $this->localeContext->getLocaleCode(),
  44.                                 $page,
  45.                                 $limit,
  46.                                 $route,
  47.                                 \array_merge(['code' => $code], $request->query->all())
  48.                             )
  49.                         ),
  50.                         Response::HTTP_OK
  51.                     ));
  52.                 });
  53.         } catch (\InvalidArgumentException $exception) {
  54.             throw new BadRequestHttpException($exception->getMessage());
  55.         }
  56.     }
  57. }