<?php
namespace Plugin\NZGoogleLogin42\Event;
use Plugin\NZGoogleLogin42\Repository\ConfigRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Eccube\Event\TemplateEvent;
class TemplateEventSubscriber implements EventSubscriberInterface
{
/**
* @var ConfigRepository
*/
private $configRepository;
public function __construct(ConfigRepository $configRepository)
{
$this->configRepository = $configRepository;
}
public static function getSubscribedEvents()
{
return [
'Mypage/login.twig' => 'onMypageLogin',
'Entry/index.twig' => 'onEntryIndex',
];
}
/**
* Googleログインが有効かチェック
*/
private function isGoogleLoginEnabled(): bool
{
try {
$Config = $this->configRepository->get();
return $Config && $Config->isEnabled() && $Config->getClientId() && $Config->getClientSecret();
} catch (\Exception $e) {
log_error('[NZGoogleLogin] Error checking config', ['message' => $e->getMessage()]);
return false;
}
}
/**
* マイページログインフォームにGoogleボタン追加
*/
public function onMypageLogin(TemplateEvent $event)
{
if (!$this->isGoogleLoginEnabled()) {
return;
}
$event->addSnippet('@NZGoogleLogin42/Mypage/login_button.twig');
}
/**
* 会員登録フォームにGoogleボタン追加
*/
public function onEntryIndex(TemplateEvent $event)
{
if (!$this->isGoogleLoginEnabled()) {
return;
}
$event->addSnippet('@NZGoogleLogin42/Entry/google_button.twig');
}
}