app/Plugin/RecentlyViewedProducts/Event.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the RecentlyViewedProducts Plugin
  4.  *
  5.  * Copyright (C) 2023 Diezon.
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Plugin\RecentlyViewedProducts;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  13. use Symfony\Component\Security\Http\SecurityEvents;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Plugin\RecentlyViewedProducts\Service\RecentlyViewedProductService;
  18. class Event implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var RecentlyViewedProductService
  22.      */
  23.     protected $recentlyViewedProductService;
  24.     /**
  25.      * Event constructor.
  26.      */
  27.     public function __construct(RecentlyViewedProductService $recentlyViewedProductService)
  28.     {
  29.         $this->recentlyViewedProductService $recentlyViewedProductService;
  30.     }
  31.     /**
  32.      * @param InteractiveLoginEvent $event
  33.      */
  34.     public function onInteractiveLogin(InteractiveLoginEvent $event)
  35.     {
  36.         $Customer $event
  37.             ->getAuthenticationToken()
  38.             ->getUser();
  39.         if ($Customer instanceof Customer) {
  40.             $this->recentlyViewedProductService->mergeFromPersistedProducts($Customer);
  41.         }
  42.     }
  43.     /**
  44.      * @param EventArgs $event
  45.      */
  46.     public function onProductDetailInitialize(EventArgs $event)
  47.     {
  48.         $Product $event->getArgument('Product');
  49.         $this->recentlyViewedProductService->addViewedProduct($Product);
  50.     }
  51.     /**
  52.      * @return array
  53.      */
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
  58.             EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE => 'onProductDetailInitialize',
  59.         ];
  60.     }
  61. }