<?php
namespace App\EventSubscriber\Shop;
use App\Shop\ShopManager;
use App\Shop\VirtualCart\VirtualCart;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\SecurityEvents;
/**
* @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
*/
class UserLoginCartSwitchSubscriber implements EventSubscriberInterface
{
protected ShopManager $shopManager;
public function __construct(ShopManager $shopManager)
{
$this->shopManager = $shopManager;
}
public static function getSubscribedEvents()
{
return [
SecurityEvents::INTERACTIVE_LOGIN => [
['switchCartStorageOnLogin', 1000],
]
];
}
public function switchCartStorageOnLogin(): void
{
if (!$this->shopManager->shopEnabled())
return;
$cart = $this->shopManager->getCart();
$cart->migrateStorageSessionToDb();
$cart->getStorage(VirtualCart::STORAGE_SESSION)->cleanProducts();
}
}