src/Component/Core/Shop/Controller/Country/IndexAction.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BitBag\OpenMarketplace\Component\Core\Shop\Controller\Country;
  4. use BitBag\OpenMarketplace\Component\Elasticsearch\Finder\CountryProductsFinderInterface;
  5. use BitBag\OpenMarketplace\Component\Elasticsearch\Form\Resolver\ListProductsFacetsResolverInterface;
  6. use BitBag\OpenMarketplace\Component\Elasticsearch\Form\Type\ListProductsType;
  7. use BitBag\OpenMarketplace\Component\Elasticsearch\Provider\RedirectResponseProviderInterface;
  8. use Sylius\Component\Addressing\Model\CountryInterface;
  9. use Sylius\Component\Resource\Repository\RepositoryInterface;
  10. use Symfony\Component\Form\FormFactoryInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Twig\Environment;
  15. final class IndexAction
  16. {
  17.     public function __construct(
  18.         private RepositoryInterface $countryRepository,
  19.         private CountryProductsFinderInterface $countryProductsFinder,
  20.         private Environment $twig,
  21.         private FormFactoryInterface $formFactory,
  22.         private ListProductsFacetsResolverInterface $facetsResolver,
  23.         private RedirectResponseProviderInterface $redirectResponseProvider,
  24.         ) {
  25.     }
  26.     public function __invoke(Request $request): Response
  27.     {
  28.         $countryCode $request->get('code');
  29.         $redirectResponse $this->redirectResponseProvider->provide($request, [
  30.             'code' => $countryCode,
  31.         ]);
  32.         if (null !== $redirectResponse) {
  33.             return $redirectResponse;
  34.         }
  35.         $country $this->countryRepository->findOneBy(['code' => $countryCode]);
  36.         if (!$country instanceof CountryInterface) {
  37.             throw new NotFoundHttpException(sprintf('Country with code "%s" was not found.'$countryCode));
  38.         }
  39.         $data = [
  40.             'country_code' => $country->getCode(),
  41.             'name' => null,
  42.         ];
  43.         $form $this->formFactory->create(ListProductsType::class, $data, [
  44.             'facets_resolver' => $this->facetsResolver,
  45.         ]);
  46.         $form->handleRequest($request);
  47.         $data array_merge(
  48.             $data,
  49.             $form->getData(),
  50.             $request->query->all(),
  51.         );
  52.         $products $this->countryProductsFinder->find($data);
  53.         return new Response($this->twig->render('Context/Shop/Country/index.html.twig', [
  54.             'form' => $form->createView(),
  55.             'products' => $products,
  56.             'country' => $country,
  57.         ]));
  58.     }
  59. }