vendor/store.shopware.com/coewishlistsw6/src/Core/Content/Wishlist/Subscriber/TransferWishlistSubscriber.php line 67

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CoeWishlistSw6\Core\Content\Wishlist\Subscriber;
  3. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistService;
  4. use CoeWishlistSw6\Core\Content\Wishlist\Service\WishlistServiceInterface;
  5. use Shopware\Core\Checkout\Customer\Event\CustomerBeforeLoginEvent;
  6. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. /**
  11.  * Class TransferWishlistSubscriber
  12.  * The Database is able to save a wishlist with an sessionId or a customerId.
  13.  * If a user is not logged in, the lists are bound to the session ID.
  14.  * Once the user logs in, we need to change the storage methode from "sessionId" to "userId".
  15.  * This basically means, that after a user logs in, the lists which has been created previously will be bound
  16.  * to the customer itself. The main goal is to persist the lists. After the user logs out again, there should be
  17.  * no notes associated anymore.
  18.  * @package CoeWishlistSw6\Core\Content\Wishlist\Subscriber
  19.  * @author Jeffry Block <jeffry.block@codeenterprise.de>
  20.  */
  21. class TransferWishlistSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var WishlistServiceInterface
  25.      */
  26.     private $wishlistService;
  27.     /**
  28.      * @var Session
  29.      */
  30.     private $session;
  31.     /**
  32.      * CustomerSubscriber constructor.
  33.      * @param WishlistServiceInterface $wishlistService
  34.      * @param Session $session
  35.      */
  36.     public function __construct(WishlistServiceInterface $wishlistServiceSession $session)
  37.     {
  38.         $this->wishlistService $wishlistService;
  39.         $this->session $session;
  40.     }
  41.     /**
  42.      * @return array
  43.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  44.      */
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             CustomerLoginEvent::class => 'onCustomerLoggedIn',
  49.             CustomerBeforeLoginEvent::class => 'onCustomerBeforeLogin',
  50.             CustomerRegisterEvent::class => 'onCustomerRegister',
  51.         ];
  52.     }
  53.     /**
  54.      * When a user logs in, a new session ID will be generated.
  55.      * As we need the "old" ID to find the lists associated to it, we need to
  56.      * save it temporary.
  57.      * @param CustomerBeforeLoginEvent $event
  58.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  59.      */
  60.     public function onCustomerBeforeLogin(CustomerBeforeLoginEvent $event)
  61.     {
  62.         $this->session->set("tmp_SessionId"$this->session->getId());
  63.     }
  64.     /**
  65.      * When a user registers, a new session ID will be generated.
  66.      * As we need the "old" ID to find the lists associated to it, we need to
  67.      * save it temporary.
  68.      * @param CustomerRegisterEvent $event
  69.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  70.      */
  71.     public function onCustomerRegister(CustomerRegisterEvent $event)
  72.     {
  73.         $this->session->set("tmp_SessionId"$this->session->getId());
  74.     }
  75.     /**
  76.      * As mentioned above, we can not use the current Session ID to transfer the lists.
  77.      * So we need to get the "old" session ID and transfer the lists by using the wishlistService.
  78.      * After the lists has been transfered, the "old" session ID will be deleted.
  79.      * @param CustomerLoginEvent|null $event
  80.      * @author Jeffry Block <jeffry.block@codeenterprise.de>
  81.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  82.      */
  83.     public function onCustomerLoggedIn(CustomerLoginEvent $event)
  84.     {
  85.         if (is_null($this->session->get("tmp_SessionId"))) {
  86.             return;
  87.         }
  88.         $this->wishlistService->transferList(
  89.             $this->session->get("tmp_SessionId"),
  90.             $event->getCustomer()->getId(),
  91.             $event->getSalesChannelContext()
  92.         );
  93.         $this->wishlistService->resetDefault(
  94.             $event->getCustomer()->getId(),
  95.             $event->getSalesChannelContext()->getContext()
  96.         );
  97.         $this->session->remove("tmp_SessionId");
  98.     }
  99. }