vendor/store.shopware.com/netinextmodal/src/Subscriber/StorefrontRenderSubscriber.php line 92

Open in your IDE?
  1. <?php
  2. /**
  3.  * @category NetiNextModal
  4.  * @author   bmueller
  5.  */
  6. namespace NetInventors\NetiNextModal\Subscriber;
  7. use NetInventors\NetiNextModal\Services\PluginConfig;
  8. use Shopware\Core\Content\Category\CategoryEntity;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Storefront\Event\StorefrontRenderEvent;
  14. use Shopware\Storefront\Framework\Twig\ControllerInfo;
  15. use Shopware\Storefront\Page\Navigation\NavigationPage;
  16. use Shopware\Storefront\Page\Product\ProductPage;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. /**
  21.  * Class StorefrontRenderSubscriber
  22.  *
  23.  * @package NetInventors\NetiNextModal\Subscriber
  24.  */
  25. class StorefrontRenderSubscriber implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var RequestStack
  29.      */
  30.     protected $requestStack;
  31.     /**
  32.      * @var EntityRepositoryInterface
  33.      */
  34.     protected $categoryRepository;
  35.     /**
  36.      * @var PluginConfig
  37.      */
  38.     private $pluginConfig;
  39.     /**
  40.      * StorefrontRenderSubscriber constructor.
  41.      *
  42.      * @param RequestStack              $requestStack
  43.      * @param EntityRepositoryInterface $categoryRepository
  44.      * @param PluginConfig              $pluginConfig
  45.      */
  46.     public function __construct(
  47.         RequestStack              $requestStack,
  48.         EntityRepositoryInterface $categoryRepository,
  49.         PluginConfig              $pluginConfig
  50.     ) {
  51.         $this->requestStack       $requestStack;
  52.         $this->categoryRepository $categoryRepository;
  53.         $this->pluginConfig       $pluginConfig;
  54.     }
  55.     /**
  56.      * Returns an array of event names this subscriber wants to listen to.
  57.      *
  58.      * The array keys are event names and the value can be:
  59.      *
  60.      *  * The method name to call (priority defaults to 0)
  61.      *  * An array composed of the method name to call and the priority
  62.      *  * An array of arrays composed of the method names to call and respective
  63.      *    priorities, or 0 if unset
  64.      *
  65.      * For instance:
  66.      *
  67.      *  * ['eventName' => 'methodName']
  68.      *  * ['eventName' => ['methodName', $priority]]
  69.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  70.      *
  71.      * @return array The event names to listen to
  72.      */
  73.     public static function getSubscribedEvents(): array
  74.     {
  75.         return [
  76.             StorefrontRenderEvent::class => 'onStorefrontRender',
  77.         ];
  78.     }
  79.     /**
  80.      * @param StorefrontRenderEvent $event
  81.      *
  82.      * @throws InconsistentCriteriaIdsException
  83.      */
  84.     public function onStorefrontRender(StorefrontRenderEvent $event): void
  85.     {
  86.         $request $this->requestStack->getCurrentRequest();
  87.         if (null === $request) {
  88.             return;
  89.         }
  90.         $parameters $event->getParameters();
  91.         $page       $parameters['page'] ?? null;
  92.         if (!($page instanceof ProductPage)) {
  93.             $categoryId $request->get(
  94.                 'navigationId',
  95.                 $page instanceof NavigationPage $page->getNavigationId() : null
  96.             );
  97.             if (null !== $categoryId && $category $this->getCategory($categoryId$event->getContext())) {
  98.                 $event->setParameter(
  99.                     'neti_modal_category',
  100.                     [
  101.                         'id'   => $category->getId(),
  102.                         'tree' => array_keys($category->getPlainBreadcrumb()),
  103.                     ]
  104.                 );
  105.             }
  106.         }
  107.         $maxWidth     $this->pluginConfig->getMaxWidth();
  108.         $maxWidthUnit $this->pluginConfig->getMaxWidthUnit();
  109.         $event->setParameter('neti_modal_max_width'$maxWidth $maxWidthUnit);
  110.         $maxHeight     $this->pluginConfig->getMaxHeight();
  111.         $maxHeightUnit $this->pluginConfig->getMaxHeightUnit();
  112.         $event->setParameter('neti_modal_max_height'$maxHeight $maxHeightUnit);
  113.         $triggerParameter $this->pluginConfig->getTriggerParameter();
  114.         if (null !== $triggerParameter) {
  115.             $event->setParameter(
  116.                 'neti_modal_id',
  117.                 $request->get($triggerParameter)
  118.             );
  119.         }
  120.         $controllerInfo $this->getControllerInfo($request);
  121.         $event->setParameter('neti_modal_controller_name'$controllerInfo->getName());
  122.         $event->setParameter('neti_modal_controller_action'$controllerInfo->getAction());
  123.     }
  124.     /**
  125.      * @param string  $categoryId
  126.      * @param Context $context
  127.      *
  128.      * @return CategoryEntity|null
  129.      * @throws InconsistentCriteriaIdsException
  130.      */
  131.     private function getCategory(string $categoryIdContext $context): ?CategoryEntity
  132.     {
  133.         $criteria   = new Criteria([ $categoryId ]);
  134.         $categories $this->categoryRepository->search($criteria$context);
  135.         return $categories->first();
  136.     }
  137.     private function getControllerInfo(Request $request): ControllerInfo
  138.     {
  139.         $controllerInfo = new ControllerInfo();
  140.         /** @var string|null $controller */
  141.         $controller $request->attributes->get('_controller');
  142.         if (!$controller) {
  143.             return $controllerInfo;
  144.         }
  145.         $matches = [];
  146.         $pattern '/Controller\\\\(\w+)?(Controller|)::?(\w+)/';
  147.         preg_match($pattern$controller$matches);
  148.         if ($matches) {
  149.             $controllerInfo->setName($matches[1]);
  150.             $controllerInfo->setAction($matches[3]);
  151.         }
  152.         return $controllerInfo;
  153.     }
  154. }