<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use Boldr\Shop\ShopBundle\Event\ShopPagePreloadEvent;
use App\Entity\{ ProductAvailable, CategoryOptions, ProductFeatures };
use Boldr\Shop\ShopBundle\ShopContext;
use Boldr\Shop\MultishopBundle\Entity\OrderShop;
use Boldr\Shop\MultishopBundle\DefaultShop\DefaultShopProviderInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class PreloadEventSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $em;
private ShopContext $shopContext;
private DefaultShopProviderInterface $defaultShopProvider;
private RequestStack $requestStack;
public function __construct(
EntityManagerInterface $em,
ShopContext $shopContext,
DefaultShopProviderInterface $defaultShopProvider,
RequestStack $requestStack
)
{
$this->em = $em;
$this->shopContext = $shopContext;
$this->defaultShopProvider = $defaultShopProvider;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
ShopPagePreloadEvent::class => ['onShopPagePreload']
];
}
public function onShopPagePreload($event)
{
foreach ([
CategoryOptions::class,
ProductFeatures::class,
] as $preload) {
if (class_exists($preload))
{
$this->em->getRepository($preload)->findAll();
}
}
$order = $this->shopContext->getCurrentOrder();
$orderShop = $order === null || $order->getId() === null ? null : $this->em->getRepository(OrderShop::class)->find($order);
$shop = $orderShop === null
? $this->defaultShopProvider->getDefaultShop($this->requestStack->getCurrentRequest(), $order->getCustomer())
: $orderShop->getShop();
$this->em->getRepository(ProductAvailable::class)->createQueryBuilder('pa')
->where('pa.shop = :shop')
->setParameter('shop', $shop)
->getQuery()
->getResult();
}
}