app/Plugin/NZGoogleLogin42/Event/EntryEventSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZGoogleLogin42\Event;
  3. use Eccube\Entity\Customer;
  4. use Eccube\Event\EccubeEvents;
  5. use Eccube\Event\EventArgs;
  6. use Plugin\NZGoogleLogin42\Entity\CustomerOAuth;
  7. use Plugin\NZGoogleLogin42\Repository\CustomerOAuthRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. class EntryEventSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var RequestStack
  15.      */
  16.     private $requestStack;
  17.     /**
  18.      * @var EntityManagerInterface
  19.      */
  20.     private $entityManager;
  21.     /**
  22.      * @var CustomerOAuthRepository
  23.      */
  24.     private $customerOAuthRepository;
  25.     // セッションキー定数(GoogleLoginControllerと同じ)
  26.     private const SESSION_GOOGLE_DATA 'nz_google_login_data';
  27.     // Googleデータの有効期限(30分)
  28.     private const GOOGLE_DATA_EXPIRY 1800;
  29.     public function __construct(
  30.         RequestStack $requestStack,
  31.         EntityManagerInterface $entityManager,
  32.         CustomerOAuthRepository $customerOAuthRepository
  33.     ) {
  34.         $this->requestStack $requestStack;
  35.         $this->entityManager $entityManager;
  36.         $this->customerOAuthRepository $customerOAuthRepository;
  37.     }
  38.     public static function getSubscribedEvents()
  39.     {
  40.         return [
  41.             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => 'onEntryComplete',
  42.         ];
  43.     }
  44.     /**
  45.      * 会員登録完了時にGoogle連携を保存
  46.      */
  47.     public function onEntryComplete(EventArgs $event)
  48.     {
  49.         try {
  50.             $request $this->requestStack->getCurrentRequest();
  51.             if (!$request || !$request->hasSession()) {
  52.                 return;
  53.             }
  54.             $session $request->getSession();
  55.             $googleData $session->get(self::SESSION_GOOGLE_DATA);
  56.             if (!$googleData) {
  57.                 return;
  58.             }
  59.             // データの有効性チェック
  60.             if (!isset($googleData['google_id']) || !isset($googleData['email'])) {
  61.                 log_warning('[NZGoogleLogin] Invalid Google data in session', [
  62.                     'keys' => array_keys($googleData),
  63.                 ]);
  64.                 $session->remove(self::SESSION_GOOGLE_DATA);
  65.                 return;
  66.             }
  67.             // タイムスタンプチェック(30分以上経過していたら無効)
  68.             if (isset($googleData['timestamp'])) {
  69.                 $elapsed time() - $googleData['timestamp'];
  70.                 if ($elapsed self::GOOGLE_DATA_EXPIRY) {
  71.                     log_warning('[NZGoogleLogin] Google data expired', [
  72.                         'elapsed_seconds' => $elapsed,
  73.                     ]);
  74.                     $session->remove(self::SESSION_GOOGLE_DATA);
  75.                     return;
  76.                 }
  77.             }
  78.             /** @var Customer $Customer */
  79.             $Customer $event->getArgument('Customer');
  80.             if (!$Customer || !$Customer->getId()) {
  81.                 log_error('[NZGoogleLogin] Customer not found in entry complete event');
  82.                 $session->remove(self::SESSION_GOOGLE_DATA);
  83.                 return;
  84.             }
  85.             // 既に同じGoogle IDで連携がないかチェック
  86.             $existingOAuth $this->customerOAuthRepository->findByGoogleId($googleData['google_id']);
  87.             if ($existingOAuth) {
  88.                 log_warning('[NZGoogleLogin] Google ID already linked to another customer', [
  89.                     'google_id' => substr($googleData['google_id'], 010) . '...',
  90.                     'existing_customer_id' => $existingOAuth->getCustomer()->getId(),
  91.                     'new_customer_id' => $Customer->getId(),
  92.                 ]);
  93.                 $session->remove(self::SESSION_GOOGLE_DATA);
  94.                 return;
  95.             }
  96.             // この顧客に既にOAuth連携がないかチェック
  97.             $customerOAuth $this->customerOAuthRepository->findByCustomerId($Customer->getId());
  98.             if ($customerOAuth) {
  99.                 // 既存の連携を更新
  100.                 $customerOAuth->setGoogleId($googleData['google_id']);
  101.                 $customerOAuth->setEmail($googleData['email']);
  102.                 $customerOAuth->setUpdateDate(new \DateTime());
  103.                 
  104.                 log_info('[NZGoogleLogin] Updated existing OAuth link', [
  105.                     'customer_id' => $Customer->getId(),
  106.                 ]);
  107.             } else {
  108.                 // Google連携情報を新規保存
  109.                 $CustomerOAuth = new CustomerOAuth();
  110.                 $CustomerOAuth->setCustomer($Customer);
  111.                 $CustomerOAuth->setGoogleId($googleData['google_id']);
  112.                 $CustomerOAuth->setEmail($googleData['email']);
  113.                 $CustomerOAuth->setCreateDate(new \DateTime());
  114.                 $CustomerOAuth->setUpdateDate(new \DateTime());
  115.                 $this->entityManager->persist($CustomerOAuth);
  116.                 
  117.                 log_info('[NZGoogleLogin] Created new OAuth link', [
  118.                     'customer_id' => $Customer->getId(),
  119.                     'google_id' => substr($googleData['google_id'], 010) . '...',
  120.                 ]);
  121.             }
  122.             $this->entityManager->flush();
  123.             // セッションからGoogle情報を削除
  124.             $session->remove(self::SESSION_GOOGLE_DATA);
  125.         } catch (\Exception $e) {
  126.             log_error('[NZGoogleLogin] Error in onEntryComplete', [
  127.                 'message' => $e->getMessage(),
  128.                 'trace' => $e->getTraceAsString(),
  129.             ]);
  130.             
  131.             // エラーが発生してもセッションはクリア
  132.             try {
  133.                 $request $this->requestStack->getCurrentRequest();
  134.                 if ($request && $request->hasSession()) {
  135.                     $request->getSession()->remove(self::SESSION_GOOGLE_DATA);
  136.                 }
  137.             } catch (\Exception $e2) {
  138.                 // ignore
  139.             }
  140.         }
  141.     }
  142. }