src/EventSubscriber/Shop/UserLoginCartSwitchSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Shop;
  3. use App\Shop\ShopManager;
  4. use App\Shop\VirtualCart\VirtualCart;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Http\SecurityEvents;
  7. /**
  8.  * @author Przemysław Chrupek <przemyslaw.chrupek@avt.pl>
  9.  */
  10. class UserLoginCartSwitchSubscriber implements EventSubscriberInterface
  11. {
  12.     protected ShopManager $shopManager;
  13.     public function __construct(ShopManager $shopManager)
  14.     {
  15.         $this->shopManager $shopManager;
  16.     }
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             SecurityEvents::INTERACTIVE_LOGIN => [
  21.                 ['switchCartStorageOnLogin'1000],
  22.             ]
  23.         ];
  24.     }
  25.     public function switchCartStorageOnLogin(): void
  26.     {
  27.         if (!$this->shopManager->shopEnabled())
  28.             return;
  29.         $cart $this->shopManager->getCart();
  30.         $cart->migrateStorageSessionToDb();
  31.         $cart->getStorage(VirtualCart::STORAGE_SESSION)->cleanProducts();
  32.     }
  33. }