app/Plugin/NZGoogleLogin42/Event/TemplateEventSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace Plugin\NZGoogleLogin42\Event;
  3. use Plugin\NZGoogleLogin42\Repository\ConfigRepository;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Eccube\Event\TemplateEvent;
  6. class TemplateEventSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var ConfigRepository
  10.      */
  11.     private $configRepository;
  12.     public function __construct(ConfigRepository $configRepository)
  13.     {
  14.         $this->configRepository $configRepository;
  15.     }
  16.     public static function getSubscribedEvents()
  17.     {
  18.         return [
  19.             'Mypage/login.twig' => 'onMypageLogin',
  20.             'Entry/index.twig' => 'onEntryIndex',
  21.         ];
  22.     }
  23.     /**
  24.      * Googleログインが有効かチェック
  25.      */
  26.     private function isGoogleLoginEnabled(): bool
  27.     {
  28.         try {
  29.             $Config $this->configRepository->get();
  30.             return $Config && $Config->isEnabled() && $Config->getClientId() && $Config->getClientSecret();
  31.         } catch (\Exception $e) {
  32.             log_error('[NZGoogleLogin] Error checking config', ['message' => $e->getMessage()]);
  33.             return false;
  34.         }
  35.     }
  36.     /**
  37.      * マイページログインフォームにGoogleボタン追加
  38.      */
  39.     public function onMypageLogin(TemplateEvent $event)
  40.     {
  41.         if (!$this->isGoogleLoginEnabled()) {
  42.             return;
  43.         }
  44.         $event->addSnippet('@NZGoogleLogin42/Mypage/login_button.twig');
  45.     }
  46.     /**
  47.      * 会員登録フォームにGoogleボタン追加
  48.      */
  49.     public function onEntryIndex(TemplateEvent $event)
  50.     {
  51.         if (!$this->isGoogleLoginEnabled()) {
  52.             return;
  53.         }
  54.         $event->addSnippet('@NZGoogleLogin42/Entry/google_button.twig');
  55.     }
  56. }