src/EventSubscriber/PreloadEventSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Boldr\Shop\ShopBundle\Event\ShopPagePreloadEvent;
  6. use App\Entity\{ ProductAvailableCategoryOptionsProductFeatures };
  7. use Boldr\Shop\ShopBundle\ShopContext;
  8. use Boldr\Shop\MultishopBundle\Entity\OrderShop;
  9. use Boldr\Shop\MultishopBundle\DefaultShop\DefaultShopProviderInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class PreloadEventSubscriber implements EventSubscriberInterface
  12. {
  13.     private EntityManagerInterface $em;
  14.     private ShopContext $shopContext;
  15.     private DefaultShopProviderInterface $defaultShopProvider;
  16.     private RequestStack $requestStack;
  17.     public function __construct(
  18.         EntityManagerInterface $em,
  19.         ShopContext $shopContext,
  20.         DefaultShopProviderInterface $defaultShopProvider,
  21.         RequestStack $requestStack
  22.     )
  23.     {
  24.         $this->em $em;
  25.         $this->shopContext $shopContext;
  26.         $this->defaultShopProvider $defaultShopProvider;
  27.         $this->requestStack $requestStack;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return [
  32.             ShopPagePreloadEvent::class => ['onShopPagePreload']
  33.         ];
  34.     }
  35.     public function onShopPagePreload($event)
  36.     {
  37.         foreach ([
  38.             CategoryOptions::class,
  39.             ProductFeatures::class,
  40.         ] as $preload) {
  41.             if (class_exists($preload))
  42.             {
  43.                 $this->em->getRepository($preload)->findAll();
  44.             }
  45.         }
  46.         $order $this->shopContext->getCurrentOrder();
  47.         $orderShop $order === null || $order->getId() === null null $this->em->getRepository(OrderShop::class)->find($order);
  48.         $shop $orderShop === null
  49.             $this->defaultShopProvider->getDefaultShop($this->requestStack->getCurrentRequest(), $order->getCustomer())
  50.             : $orderShop->getShop();
  51.         $this->em->getRepository(ProductAvailable::class)->createQueryBuilder('pa')
  52.             ->where('pa.shop = :shop')
  53.             ->setParameter('shop'$shop)
  54.             ->getQuery()
  55.             ->getResult();
  56.     }
  57. }