<?php
declare(strict_types=1);
namespace Ui\Http\Rest\Controller\Article;
use App\Blog\Application\Query\GetTaxonByCode\GetTaxonByCodeQuery;
use App\Shared\Application\Query\QueryBusInterface;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
final class GetTaxonomyByCodeAction
{
public function __construct(
private QueryBusInterface $queryBus,
private ViewHandlerInterface $viewHandler,
private ChannelContextInterface $channelContext,
private LocaleContextInterface $localeContext,
private TagAwareCacheInterface $cache
) {
}
public function __invoke(string $code, Request $request) : Response
{
try {
$route = $request->attributes->get('_route');
$page = $request->query->getInt('page', 1);
$limit = $request->query->getInt('limit', 8);
return $this->cache->get(
urlencode("$route-$code-$page-$limit"),
function (ItemInterface $item) use ($code, $request, $route, $page, $limit) {
$item->tag([
'article-category',
"article-category-by-code-$code"
]);
return $this->viewHandler->handle(View::create(
$this->queryBus->ask(
new GetTaxonByCodeQuery(
$code,
$this->channelContext->getChannel()->getCode(),
$this->localeContext->getLocaleCode(),
$page,
$limit,
$route,
\array_merge(['code' => $code], $request->query->all())
)
),
Response::HTTP_OK
));
});
} catch (\InvalidArgumentException $exception) {
throw new BadRequestHttpException($exception->getMessage());
}
}
}