<?php
/*
* This file is part of the RecentlyViewedProducts Plugin
*
* Copyright (C) 2023 Diezon.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\RecentlyViewedProducts;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Eccube\Entity\Customer;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Plugin\RecentlyViewedProducts\Service\RecentlyViewedProductService;
class Event implements EventSubscriberInterface
{
/**
* @var RecentlyViewedProductService
*/
protected $recentlyViewedProductService;
/**
* Event constructor.
*/
public function __construct(RecentlyViewedProductService $recentlyViewedProductService)
{
$this->recentlyViewedProductService = $recentlyViewedProductService;
}
/**
* @param InteractiveLoginEvent $event
*/
public function onInteractiveLogin(InteractiveLoginEvent $event)
{
$Customer = $event
->getAuthenticationToken()
->getUser();
if ($Customer instanceof Customer) {
$this->recentlyViewedProductService->mergeFromPersistedProducts($Customer);
}
}
/**
* @param EventArgs $event
*/
public function onProductDetailInitialize(EventArgs $event)
{
$Product = $event->getArgument('Product');
$this->recentlyViewedProductService->addViewedProduct($Product);
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
EccubeEvents::FRONT_PRODUCT_DETAIL_INITIALIZE => 'onProductDetailInitialize',
];
}
}