src/Event/UserInteractiveLoginSubscriber.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Event;
  3. use App\Component\Cart\CartFactory;
  4. use App\Component\Cart\Storage\CartSessionStorage;
  5. use App\Component\CartService;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  13. use Symfony\Component\Security\Http\SecurityEvents;
  14. class UserInteractiveLoginSubscriber implements EventSubscriberInterface
  15. {
  16.     private $em;
  17.     private $session;
  18.     private $cartService;
  19.     private $logger;
  20.     public function __construct(EntityManagerInterface $emSessionInterface $sessionCartService $cartServiceLoggerInterface $logger)
  21.     {
  22.         $this->em $em;
  23.         $this->session $session;
  24.         $this->cartService $cartService;
  25.         $this->logger $logger;
  26.     }
  27.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  28.     {
  29.         /** @var User $user */
  30.         $user $event->getAuthenticationToken()->getUser();
  31.         if (get_class($user) == User::class) {
  32.             $this->logger->debug('onSecurityInteractiveLogin');
  33.             // カート追加時はカートに商品を追加
  34.             $beforeLoginParams $this->session->get('auth_adding_item');
  35. //        dd($this->session->all());
  36.             if (is_array($beforeLoginParams)) {
  37.                 $this->logger->debug('before login params ');
  38.                 if (array_key_exists('menuId'$beforeLoginParams)) {
  39.                     $this->logger->debug('menuId exists');
  40.                     // 商品指定があるのでカートに追加
  41.                     $menu $this->em->getRepository('App:Menu')->find($beforeLoginParams['menuId']);
  42.                     if ($menu) {
  43.                         $this->logger->debug('menu exists');
  44.                         $quantity 1;
  45.                         if (array_key_exists('quantity'$beforeLoginParams)) {
  46.                             // 数量指定があれば適用
  47.                             $quantity $beforeLoginParams['quantity'];
  48.                         }
  49.                         // アイテムをカートに追加
  50.                         $this->cartService->addCartItem($user$menu$quantity);
  51. //                    dd($this->cartFactory->getCurrent());
  52.                         $this->logger->debug('added item');
  53.                     }
  54.                 }
  55.             }
  56. //        $user->setLastLoginAt(new \DateTime());
  57. //        $this->em->persist($user);
  58.             $this->em->flush();
  59.         }
  60.     }
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             SecurityEvents::INTERACTIVE_LOGIN => 'onSecurityInteractiveLogin',
  65.         ];
  66.     }
  67. }