src/Controller/Action/DisplayProductByBrandControllerAction.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Action;
  4. use App\Entity\Product\Product;
  5. use App\Entity\Taxonomy\Taxon;
  6. use App\Repository\Brand\BrandRepository;
  7. use App\Repository\Product\ProductRepository;
  8. use Sylius\Bundle\TaxonomyBundle\Doctrine\ORM\TaxonRepository;
  9. use Sylius\Component\Channel\Context\ChannelContextInterface;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Twig\Environment;
  16. final class DisplayProductByBrandControllerAction
  17. {
  18.     private Environment $twig;
  19.     private RouterInterface $router;
  20.     private ChannelContextInterface $channelContext;
  21.     public function __construct(
  22.         Environment $twig,
  23.         ChannelContextInterface $channelContext,
  24.         RouterInterface $router
  25.     ) {
  26.         $this->twig $twig;
  27.         $this->router $router;
  28.         $this->channelContext $channelContext;
  29.     }
  30.     public function __invoke(Request $requestBrandRepository $brandRepositoryProductRepository $productRepositoryTaxonRepository $taxonRepository) {
  31.         $code $request->get('code');
  32.         if (!$code) {
  33.             return new RedirectResponse($this->router->generate('app_shop_brand_index'));
  34.         }
  35.         $brand $brandRepository->findOneBy(['code' => $code]);
  36.         if (!$brand) {
  37.             return new RedirectResponse($this->router->generate('app_shop_brand_index'));
  38.         }
  39.         // trier les produits par catégorie
  40.         $taxons = [];
  41.         $products $productRepository->findAllByBrand($brand$this->channelContext->getChannel());
  42.         /** @var Product $product */
  43.         foreach ($products as $product) {
  44.             /** @var Taxon $taxon */
  45.             foreach ($product->getTaxons() as $taxon) {
  46.                 if ($taxon->getParent()) {
  47.                     $taxons[$taxon->getId()]['taxon'] = $taxon;
  48.                     $taxons[$taxon->getId()]['products'][$product->getId()] = $product;
  49.                 }
  50.             }
  51.         }
  52.         return new Response($this->twig->render('@SyliusShop/Brand/Product/index.html.twig', [
  53.             'brand' => $brand,
  54.             'taxons' => $taxons
  55.         ]));
  56.     }
  57. }