packages/sylius-google-analytics-plugin/src/EventListener/NavigationEventListener.php line 96

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Jakub Lech <info@smartbyte.pl>
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. declare(strict_types=1);
  9. namespace Spinbits\SyliusGoogleAnalytics4Plugin\EventListener;
  10. use Pagerfanta\Pagerfanta;
  11. use Sonata\BlockBundle\Event\BlockEvent;
  12. use Spinbits\SyliusGoogleAnalytics4Plugin\Factory\Events\NavigationEventFactory;
  13. use Spinbits\SyliusGoogleAnalytics4Plugin\Storage\EventsBag;
  14. use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
  15. use Sylius\Component\Core\Model\ProductInterface;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  19. class NavigationEventListener implements EventSubscriberInterface
  20. {
  21.     private NavigationEventFactory $navEventFactory;
  22.     private EventsBag $eventsStorage;
  23.     private string $searchRouteName;
  24.     public function __construct(
  25.         NavigationEventFactory $navEventFactory,
  26.         EventsBag $eventsStorage,
  27.         string $searchRouteName 'sylius_shop_product_index'
  28.     ) {
  29.         $this->navEventFactory $navEventFactory;
  30.         $this->eventsStorage $eventsStorage;
  31.         $this->searchRouteName $searchRouteName;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             'sylius.product.show' => 'viewItem',
  37.             'sonata.block.event.sylius.shop.product.index.before_list' => 'viewItemList',
  38.             'kernel.controller_arguments' => 'onKernelRequest',//search
  39.         ];
  40.     }
  41.     public function onKernelRequest(ControllerArgumentsEvent $event): void
  42.     {
  43.         /**
  44.          * @psalm-suppress MixedArgument
  45.          * @psalm-suppress MixedAssignment
  46.          */
  47.         $routeName $event->getRequest()->attributes->get('_route');
  48.         if (!$event->isMainRequest()
  49.             || !\is_array($event->getController())
  50.             || $this->searchRouteName !== $routeName
  51.         ) {
  52.             return;
  53.         }
  54.         if ($searchTerm $this->getSearchTerm($event->getRequest())) {
  55.             $this->eventsStorage->setEvent(
  56.                 $this->navEventFactory->search($searchTerm)
  57.             );
  58.         }
  59.     }
  60.     public function viewItem(ResourceControllerEvent $resourceControllerEvent): void
  61.     {
  62.         $product $resourceControllerEvent->getSubject();
  63.         if (!$product instanceof ProductInterface) {
  64.             return;
  65.         }
  66.         $this->eventsStorage->setEvent(
  67.             $this->navEventFactory->viewItem($product)
  68.         );
  69.     }
  70.     public function viewItemList(BlockEvent $event): void
  71.     {
  72.         /** @var array<'products', mixed> $settings */
  73.         $settings $event->getSettings();
  74.         $products = [];
  75.         if (isset($settings['products']) && $settings['products'] instanceof Pagerfanta) {
  76.             /** @var array<ProductInterface> $products */
  77.             $products = (array) $settings['products']->getCurrentPageResults();
  78.         }
  79.         $this->eventsStorage->setEvent(
  80.             $this->navEventFactory->viewItemList($products)
  81.         );
  82.     }
  83.     private function getSearchTerm(Request $request): ?string
  84.     {
  85.         /** @var array<string, array<string, string|null|int>> $criteria */
  86.         $criteria = (array) $request->query->get('criteria');
  87.         return (isset($criteria['search']['value']))
  88.             ? (string) $criteria['search']['value']
  89.             : null;
  90.     }
  91. }