<?php declare(strict_types=1);
namespace CoeWishlistSw6\Core\Content\Wishlist\Subscriber;
use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistService;
use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
/**
* Class TransferWishlistSubscriber
* The Database is able to save a wishlist with an sessionId or a customerId.
* If a user is not logged in, the lists are bound to the session ID.
* Once the user logs in, we need to change the storage methode from "sessionId" to "userId".
* This basically means, that after a user logs in, the lists which has been created previously will be bound
* to the customer itself. The main goal is to persist the lists. After the user logs out again, there should be
* no notes associated anymore.
* @package CoeWishlistSw6\Core\Content\Wishlist\Subscriber
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
class TransferWishlistSubscriber implements EventSubscriberInterface
{
/**
* @var WishlistServiceInterface
*/
private $wishlistService;
/**
* @var Session
*/
private $session;
/**
* CustomerSubscriber constructor.
* @param WishlistServiceInterface $wishlistService
* @param Session $session
*/
public function __construct(WishlistServiceInterface $wishlistService, Session $session)
{
$this->wishlistService = $wishlistService;
$this->session = $session;
}
/**
* @return array
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public static function getSubscribedEvents(): array
{
return [
CustomerLoginEvent::class => 'onCustomerLoggedIn',
CustomerBeforeLoginEvent::class => 'onCustomerBeforeLogin',
CustomerRegisterEvent::class => 'onCustomerRegister',
];
}
/**
* When a user logs in, a new session ID will be generated.
* As we need the "old" ID to find the lists associated to it, we need to
* save it temporary.
* @param CustomerBeforeLoginEvent $event
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public function onCustomerBeforeLogin(CustomerBeforeLoginEvent $event)
{
$this->session->set("tmp_SessionId", $this->session->getId());
}
/**
* When a user registers, a new session ID will be generated.
* As we need the "old" ID to find the lists associated to it, we need to
* save it temporary.
* @param CustomerRegisterEvent $event
* @author Jeffry Block <jeffry.block@codeenterprise.de>
*/
public function onCustomerRegister(CustomerRegisterEvent $event)
{
$this->session->set("tmp_SessionId", $this->session->getId());
}
/**
* As mentioned above, we can not use the current Session ID to transfer the lists.
* So we need to get the "old" session ID and transfer the lists by using the wishlistService.
* After the lists has been transfered, the "old" session ID will be deleted.
* @param CustomerLoginEvent|null $event
* @author Jeffry Block <jeffry.block@codeenterprise.de>
* @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
*/
public function onCustomerLoggedIn(CustomerLoginEvent $event)
{
if (is_null($this->session->get("tmp_SessionId"))) {
return;
}
$this->wishlistService->transferList(
$this->session->get("tmp_SessionId"),
$event->getCustomer()->getId(),
$event->getSalesChannelContext()
);
$this->wishlistService->resetDefault(
$event->getCustomer()->getId(),
$event->getSalesChannelContext()->getContext()
);
$this->session->remove("tmp_SessionId");
}
}