app/Plugin/Auth0/EventListener/EntryListener.php line 40

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Auth0 for EC-CUBE
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\Auth0\EventListener;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Entity\Customer;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use KnpU\OAuth2ClientBundle\Security\Helper\FinishRegistrationBehavior;
  18. use Plugin\Auth0\Entity\Connection;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\RequestStack;
  21. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  22. class EntryListener implements EventSubscriberInterface
  23. {
  24.     use FinishRegistrationBehavior;
  25.     /**
  26.      * @var SessionInterface
  27.      */
  28.     private SessionInterface $session;
  29.     /**
  30.      * @var EntityManagerInterface
  31.      */
  32.     private EntityManagerInterface $entityManager;
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         EntityManagerInterface $entityManager
  36.     ) {
  37.         $this->session $requestStack->getSession();
  38.         $this->entityManager $entityManager;
  39.     }
  40.     /**
  41.      * @return string[]
  42.      */
  43.     public static function getSubscribedEvents(): array
  44.     {
  45.         return [
  46.             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => 'onFrontEntryIndexComplete',
  47.         ];
  48.     }
  49.     /**
  50.      * @param EventArgs $args
  51.      *
  52.      * @return void
  53.      */
  54.     public function onFrontEntryIndexComplete(EventArgs $args): void
  55.     {
  56.         $request $args->getRequest();
  57.         if (null === $request) {
  58.             return;
  59.         }
  60.         /** @var Customer $Customer */
  61.         $Customer $args->getArgument('Customer');
  62.         $userInfo $this->getUserInfoFromSession($request);
  63.         if ($userInfo) {
  64.             $Connection = new Connection();
  65.             $Connection->setUserId($userInfo['sub']);
  66.             $Connection->setCustomer($Customer);
  67.             $this->entityManager->persist($Connection);
  68.             $this->entityManager->flush();
  69.             // 会員登録完了時にAuth0のセッション削除
  70.             $this->session->remove('guard.finish_registration.user_information');
  71.         }
  72.     }
  73. }