app/Plugin/ECCUBE4LineLoginIntegration/LineLoginIntegrationEvent.php line 323

Open in your IDE?
  1. <?php
  2. namespace Plugin\ECCUBE4LineLoginIntegration;
  3. use Eccube\Event\EventArgs;
  4. use Eccube\Event\TemplateEvent;
  5. use Eccube\Event\EccubeEvents;
  6. use Eccube\Entity\Master\CustomerStatus;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Plugin\ECCUBE4LineLoginIntegration\Controller\LineLoginIntegrationController;
  11. use Plugin\ECCUBE4LineLoginIntegration\Controller\Admin\LineLoginIntegrationAdminController;
  12. use Plugin\ECCUBE4LineLoginIntegration\Entity\LineLoginIntegration;
  13. use Plugin\ECCUBE4LineLoginIntegration\Repository\LineLoginIntegrationRepository;
  14. use Plugin\ECCUBE4LineLoginIntegration\Repository\LineLoginIntegrationSettingRepository;
  15. use Twig_Environment;
  16. class LineLoginIntegrationEvent implements EventSubscriberInterface
  17. {
  18.     private $lineLoginIntegrationRepository;
  19.     private $lineLoginIntegrationSettingRepository;
  20.     private $container;
  21.     private $router;
  22.     private $session;
  23.     private $entityManager;
  24.     private $formFactory;
  25.     private $twig;
  26.     public function __construct(
  27.         LineLoginIntegrationRepository $lineLoginIntegrationRepository,
  28.         LineLoginIntegrationSettingRepository $lineLoginIntegrationSettingRepository,
  29.         ContainerInterface $container,
  30.         Twig_Environment $twig
  31.     ) {
  32.         $this->lineLoginIntegrationRepository $lineLoginIntegrationRepository;
  33.         $this->lineLoginIntegrationSettingRepository $lineLoginIntegrationSettingRepository;
  34.         $this->container $container;
  35.         $this->router $this->container->get('router');
  36.         $this->session $this->container->get('session');
  37.         $this->entityManager $this->container->get('doctrine.orm.default_entity_manager');
  38.         $this->formFactory $this->container->get('form.factory');
  39.         $this->twig $twig;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             'Entry/index.twig' => [
  45.                 ['onRenderEntryIndex'10],
  46.                 ['onRenderLineEntryButton', -10]
  47.             ],
  48.             //'Entry/confirm.twig' => 'onRenderEntryConfirm',
  49.             EccubeEvents::FRONT_ENTRY_INDEX_COMPLETE => 'onCompleteEntry',
  50.             'Mypage/login.twig' => 'onRenderLineLoginButton',
  51.             'Mypage/change.twig' => 'onRenderMypageChange',
  52.             'Shopping/login.twig' => 'onRenderShoppingLineLoginButton',
  53.             EccubeEvents::FRONT_MYPAGE_CHANGE_INDEX_COMPLETE => 'onCompleteMypageChange',
  54.             EccubeEvents::FRONT_MYPAGE_WITHDRAW_INDEX_COMPLETE => 'onCompleteMypageWithdraw',
  55.             EccubeEvents::ADMIN_CUSTOMER_EDIT_INDEX_COMPLETE => 'onCompleteCustomerEdit',
  56.         ];
  57.     }
  58.     /**
  59.      * 新規会員登録画面の表示
  60.      * @param TemplateEvent $event
  61.      */
  62.     public function onRenderEntryIndex(TemplateEvent $event)
  63.     {
  64.         if (!$this->isLineSettingCompleted()) {
  65.             return;
  66.         }
  67.     }
  68.     /**
  69.      * 新規会員登録画面にLINEボタンを出力します
  70.      *
  71.      * @param TemplateEvent $event
  72.      */
  73.     public function onRenderLineEntryButton(TemplateEvent $event)
  74.     {
  75.         if (!$this->isLineSettingCompleted()) {
  76.             return;
  77.         }
  78.         $lineUserId $this->session->get(LineLoginIntegrationController::PLUGIN_LINE_LOGIN_INTEGRATION_SSO_USERID);
  79.         $linkUrl $this->router->generate("plugin_line_login", array(), UrlGeneratorInterface::ABSOLUTE_URL);
  80.         $imgUrl $this->router->generate("homepage", array(),
  81.                 UrlGeneratorInterface::ABSOLUTE_URL) . 'html/plugin/line_login_integration/assets/img/btn_register_base.png';
  82.         $snipet '';
  83.         // LINEボタンを表示
  84.         if (empty($lineUserId)) {
  85.             $snipet .= '<div class="btn" style=""><a href="' $linkUrl '" class="line-button"><img src="' $imgUrl '" alt="LINEで登録"></a></div>' PHP_EOL;
  86.             $snipet .= PHP_EOL;
  87.         }
  88.         // LINEにログイン済みなので登録を促す
  89.         else {
  90.             $snipet .= '<div class="col" style="margin-top:-10px; padding:10px;">LINEログイン済みです。この会員登録が完了すると、LINEでログインできるようになります。</div>';
  91.             $snipet .= PHP_EOL;
  92.         }
  93.         $search '<div class="ec-off1Grid__cell">';
  94.         $replace $search $snipet;
  95.         $source str_replace($search$replace$event->getSource());
  96.         $event->setSource($source);
  97.     }
  98.     /**
  99.      * 会員登録処理完了時のLINE連携処理
  100.      * @param EventArgs $event
  101.      */
  102.     public function onCompleteEntry(EventArgs $event)
  103.     {
  104.         if (!$this->isLineSettingCompleted()) {
  105.             return;
  106.         }
  107.         // 顧客とLINEユーザーIDをひも付け(line_login_integrationテーブルのレコードを作成)
  108.         log_info('LINEユーザーとの関連付け開始');
  109.         $lineUserId $this->session->get(LineLoginIntegrationController::PLUGIN_LINE_LOGIN_INTEGRATION_SSO_USERID);
  110.         if (!empty($lineUserId)) {
  111.             log_info('LINEログインしているため、ユーザーとの関連付けを実行');
  112.             $this->lineLoginIntegration $this->lineLoginIntegrationRepository->findOneBy(['line_user_id' => $lineUserId]);
  113.             if (empty($this->lineLoginIntegration)) {
  114.                 $customer $event['Customer'];
  115.                 log_info('LINE IDとユーザーの関連付けを開始', [$customer['id']]);
  116.                 $lineLoginIntegration = new LineLoginIntegration();
  117.                 $lineLoginIntegration->setLineUserId($lineUserId);
  118.                 $lineLoginIntegration->setCustomer($customer);
  119.                 $lineLoginIntegration->setCustomerId($customer['id']);
  120.                 $this->entityManager->persist($lineLoginIntegration);
  121.                 $this->entityManager->flush($lineLoginIntegration);
  122.                 log_info('LINEユーザーとの関連付け終了');
  123.             }
  124.             log_info('LINEユーザーとの関連付け終了');
  125.         } else {
  126.             log_info('LINE未ログインのため関連付け未実施');
  127.         }
  128.     }
  129.     /**
  130.      * ログイン画面にLINEボタンを出力します
  131.      * @param TemplateEvent $event
  132.      */
  133.     public function onRenderLineLoginButton(TemplateEvent $event)
  134.     {
  135.         if (!$this->isLineSettingCompleted()) {
  136.             return;
  137.         }
  138.         $linkUrl $this->router->generate("plugin_line_login", array(), UrlGeneratorInterface::ABSOLUTE_URL);
  139.         $imgUrl $this->router->generate("homepage", array(),
  140.                 UrlGeneratorInterface::ABSOLUTE_URL) . 'html/plugin/line_login_integration/assets/img/btn_login_base.png';
  141.         $snipet '<div class="btn" style=""><a href="' $linkUrl '" class="line-button"><img src="' $imgUrl '" alt="LINEログイン"></a></div><br>' PHP_EOL;
  142.         $search '<div class="ec-off2Grid__cell">';
  143.         $replace $search $snipet;
  144.         $source str_replace($search$replace$event->getSource());
  145.         $event->setSource($source);
  146.     }
  147.     /**
  148.      * カート経由のログイン画面にLINEボタンを出力します
  149.      * @param TemplateEvent $event
  150.      */
  151.     public function onRenderShoppingLineLoginButton(TemplateEvent $event)
  152.     {
  153.         if (!$this->isLineSettingCompleted()) {
  154.             return;
  155.         }
  156.         $linkUrl $this->router->generate("plugin_line_login", array(), UrlGeneratorInterface::ABSOLUTE_URL);
  157.         $imgUrl $this->router->generate("homepage", array(),
  158.                 UrlGeneratorInterface::ABSOLUTE_URL) . 'html/plugin/line_login_integration/assets/img/btn_login_base.png';
  159.         $snipet '<div class="btn" style=""><a href="' $linkUrl '" class="line-button"><img src="' $imgUrl '" alt="LINEログイン"></a></div><br>' PHP_EOL;
  160.         $search '<div class="ec-grid3__cell2">';
  161.         $replace $search $snipet;
  162.         $source str_replace($search$replace$event->getSource());
  163.         $event->setSource($source);
  164.     }
  165.     /**
  166.      * 会員情報変更画面の表示
  167.      * @param TemplateEvent $event
  168.      */
  169.     public function onRenderMypageChange(TemplateEvent $event)
  170.     {
  171.         if (!$this->isLineSettingCompleted()) {
  172.             return;
  173.         }
  174.         $form $event->getParameter('form');
  175.         $customerId $form->vars['value']['id'];
  176.         if (empty($customerId)) {
  177.             error_log("会員IDを取得できませんでした", [$form]);
  178.             return;
  179.         }
  180.         $lineLoginIntegration $this->lineLoginIntegrationRepository
  181.             ->findOneBy(['customer_id' => $customerId]);
  182.         $lineIdBySession $this->session
  183.             ->get(LineLoginIntegrationController::PLUGIN_LINE_LOGIN_INTEGRATION_SSO_USERID);
  184.         // LINEとの紐づけがないとき
  185.         if (empty($lineLoginIntegration)) {
  186.             // LINEのログインボタン表示
  187.             $linkUrl $this->router->generate("plugin_line_login", array(), UrlGeneratorInterface::ABSOLUTE_URL);
  188.             $imgUrl $this->router->generate("homepage", array(),
  189.                     UrlGeneratorInterface::ABSOLUTE_URL) . 'html/plugin/line_login_integration/assets/img/btn_register_base.png';
  190.             $snipet '<div class="btn"><a href="' $linkUrl '" class="line-button"><img src="' $imgUrl '" alt="LINEで登録"></a></div>' PHP_EOL;
  191.             $snipet .= PHP_EOL;
  192.             $snipet .= '<div class="col" style="padding-bottom:10px;">「LINEで登録」ボタンを押してLINEにログインすると、LINEアカウントでログインできるようになります。</div>';
  193.             $snipet .= PHP_EOL;
  194.         }
  195.         // LINEとの紐づけがあっても、現在LINEにログインしていないっぽいとき
  196.         else if (empty($lineIdBySession)) {
  197.             // LINEのログインボタン表示
  198.             $linkUrl $this->router->generate("plugin_line_login", array(), UrlGeneratorInterface::ABSOLUTE_URL);
  199.             $imgUrl $this->router->generate("homepage", array(),
  200.                     UrlGeneratorInterface::ABSOLUTE_URL) . 'html/plugin/line_login_integration/assets/img/btn_login_base.png';
  201.             $snipet '<div class="btn"><a href="' $linkUrl '" class="line-button"><img src="' $imgUrl '" alt="LINEで登録"></a></div>' PHP_EOL;
  202.             $snipet .= PHP_EOL;
  203.             $snipet .= '<div class="col" style="padding-bottom:10px;">LINEアカウントと連携済みですが、現在LINEでログインしていません。</div>';
  204.             $snipet .= PHP_EOL;
  205.         }
  206.         // LINEとの紐づけがあって、かつLINEにログイン中のとき
  207.         else {
  208.             // 連携解除項目を追加
  209.             $this->replaceMypageChangeForm($event);
  210.             $snipet '<div class="col" style="padding-bottom:10px;">LINEアカウント連携済です。解除したいときは「LINE連携 解除」をチェックして「登録する」ボタンを押してください。</div>';
  211.             $snipet .= PHP_EOL;
  212.         }
  213.         $search '<div class="ec-off1Grid__cell">';
  214.         $replace $search $snipet;
  215.         $source str_replace($search$replace$event->getSource());
  216.         $event->setSource($source);
  217.     }
  218.     /**
  219.      * 会員情報編集完了時のイベント処理を行います
  220.      *
  221.      * @param EventArgs $event
  222.      */
  223.     public function onCompleteMypageChange(EventArgs $event)
  224.     {
  225.         if (!$this->isLineSettingCompleted()) {
  226.             return;
  227.         }
  228.         $customerId $event['Customer']->getId();
  229.         $lineLoginIntegration $this->lineLoginIntegrationRepository->findOneBy(['customer_id' => $customerId]);
  230.         // LINEの紐づけがすでにあるとき
  231.         if (!empty($lineLoginIntegration)) {
  232.             $form $event['form'];
  233.             // LINE情報を削除する
  234.             if ($form->has('is_line_delete')) {
  235.                 $is_line_delete $form->get('is_line_delete')->getData();
  236.             }
  237.             if ($is_line_delete == 1) {
  238.                 // 連携解除
  239.                 $this->lineIdUnassociate($customerIdtrue);
  240.             }
  241.         }
  242.         // LINEの紐づけがないとき
  243.         else {
  244.             // 何もしない
  245.             // LINEとの紐づけ処理はログインのコールバック関数(LineLoginIntegrationController.php)内で行われるのでここでは行わない
  246.         }
  247.     }
  248.     /**
  249.      * 会員がマイページから退会手続きを行ったとき
  250.      *
  251.      * 退会した会員のLINE連携を解除する
  252.      *
  253.      * @param EventArgs $event
  254.      */
  255.     public function onCompleteMypageWithdraw(EventArgs $event)
  256.     {
  257.         if (!$this->isLineSettingCompleted()) {
  258.             return;
  259.         }
  260.         log_info('マイページから退会');
  261.         $customerId $event['Customer']['id'];
  262.         $this->lineIdUnassociate($customerIdtrue);
  263.     }
  264.     /**
  265.      * 管理画面から顧客情報を更新したとき
  266.      *
  267.      * 会員を退会にした場合にはLINE連携を解除する
  268.      *
  269.      * @param EventArgs $event
  270.      */
  271.     public function onCompleteCustomerEdit(EventArgs $event)
  272.     {
  273.         if (!$this->isLineSettingCompleted()) {
  274.             return;
  275.         }
  276.         $customerId $event['Customer']->getId();
  277.         $customerStatus $event['Customer']->getStatus();
  278.         // 退会扱いのとき
  279.         if ($customerStatus['id'] == CustomerStatus::WITHDRAWING) {
  280.             log_info('仮画面の会員情報編集ページから退会扱い');
  281.             $this->lineIdUnassociate($customerId);
  282.         }
  283.     }
  284.     /**
  285.      * LINE設定が初期化済みかチェックする
  286.      */
  287.     private function isLineSettingCompleted()
  288.     {
  289.         $lineLoginIntegrationSetting $this->lineLoginIntegrationSettingRepository
  290.             ->find(LineLoginIntegrationAdminController::LINE_LOGIN_INTEGRATION_SETTING_TABLE_ID);
  291.         if (empty($lineLoginIntegrationSetting)) {
  292.             log_error("Line Lineの情報が未設定です");
  293.             return false;
  294.         }
  295.         $lineChannelId $lineLoginIntegrationSetting->getLineChannelId();
  296.         if (empty($lineChannelId)) {
  297.             log_error("Line Channel Idが未設定です");
  298.             return false;
  299.         }
  300.         $lineChannelSecret $lineLoginIntegrationSetting->getLineChannelSecret();
  301.         if (empty($lineChannelSecret)) {
  302.             log_error("Line Channel Secretが未設定です");
  303.             return false;
  304.         }
  305.         return true;
  306.     }
  307.     /**
  308.      * LINEアカウントとの連携を解除する処理
  309.      *
  310.      * 会員IDから連携DBを検索し、該当するレコードを削除する処理。管理画面でなくフロントからのフローでは、
  311.      * セッションを削除するのでフラグをtrueにしておく
  312.      *
  313.      * @param int $customerId       LINEとの連携を解除したい会員ID
  314.      * @param bool $isDeleteSession セッションまで削除する。デフォでfalse
  315.      * @return bool                 会員がLINEと紐づけされていて、紐づけを解除したときにtrueを返す
  316.      */
  317.     private function lineIdUnassociate(int $customerId, ?bool $isDeleteSession null) {
  318.         $lineLoginIntegration $this->lineLoginIntegrationRepository->findOneBy(['customer_id' => $customerId]);
  319.         // LINE情報を削除する
  320.         if (!empty($lineLoginIntegration)) {
  321.             log_info('customer_id:' $customerId 'のLINE連携を解除');
  322.             $this->lineLoginIntegrationRepository->deleteLineAssociation($lineLoginIntegration);
  323.             log_info('LINEの連携を解除しました');
  324.             if ($isDeleteSession) {
  325.                 $this->session->remove(LineLoginIntegrationController::PLUGIN_LINE_LOGIN_INTEGRATION_SSO_STATE);
  326.                 $this->session->remove(LineLoginIntegrationController::PLUGIN_LINE_LOGIN_INTEGRATION_SSO_USERID);
  327.                 $this->session->remove($this->session->is_line_delete);
  328.             }
  329.             return true;
  330.         }
  331.         return false;
  332.     }
  333.     private function replaceMypageChangeForm(TemplateEvent $event)
  334.     {
  335.         log_info('LINE連携削除を追加');
  336.         $snipet $this->twig->getLoader()->getSourceContext('ECCUBE4LineLoginIntegration/Resource/template/mypage_change_add_is_line_delete.twig')->getCode();
  337.         $search '{# エンティティ拡張の自動出力 #}';
  338.         $replace $search $snipet;
  339.         $source str_replace($search$replace$event->getSource());
  340.         $event->setSource($source);
  341.     }
  342. }