<?phpnamespace Customize\EventListener;use Customize\Service\Google\ReCaptchaService;use Eccube\Event\TemplateEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpKernel\Event\ViewEvent;use Symfony\Component\HttpKernel\KernelEvents;use Twig\Environment;class GoogleEventListener implements EventSubscriberInterface{ /** * @var Environment */ private $reCaptchaService; public function __construct(ReCaptchaService $reCaptchaService) { $this->reCaptchaService = $reCaptchaService; } /** * @inheritDoc */ public static function getSubscribedEvents() { return [ KernelEvents::VIEW => 'onKernelView', 'Contact/index.twig' => 'onContactIndex', 'Contact/confirm.twig' => 'onContactConfirm', 'Entry/index.twig' => 'onEntryIndex', 'Entry/confirm.twig' => 'onEntryIndex', 'Forgot/index.twig' => 'onForgotIndex', 'Mail/question.twig' => 'onMailQuestionIndex', 'Mail/question_confirm.twig' => 'onMailQuestionConfirm', ]; } /** * Add path_info to controller result * * @param ViewEvent $event * @return void */ public function onKernelView(ViewEvent $event) { $controllerResult = $event->getControllerResult(); $request = $event->getRequest(); $pathInfo = $request->getPathInfo(); $controllerResult['path_info'] = $pathInfo; $event->setControllerResult($controllerResult); } public function injectReCaptcha(TemplateEvent $event) { $event->addSnippet('Google/reCAPTCHA.twig'); } public function onContactIndex(TemplateEvent $event) { $pathInfo = $event->getParameter('path_info'); if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) { $this->injectReCaptcha($event); $event->addSnippet('Google/Contact/index.twig'); } } public function onContactConfirm(TemplateEvent $event) { $pathInfo = $event->getParameter('path_info'); if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) { $this->injectReCaptcha($event); $event->addSnippet('Google/Contact/confirm.twig'); } } public function onEntryIndex(TemplateEvent $event) { $pathInfo = $event->getParameter('path_info'); if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) { $this->injectReCaptcha($event); $event->addSnippet('Google/Entry/index.twig'); } } public function onForgotIndex(TemplateEvent $event) { $pathInfo = $event->getParameter('path_info'); if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) { $this->injectReCaptcha($event); $event->addSnippet('Google/Forgot/index.twig'); } } public function onMailQuestionIndex(TemplateEvent $event) { $pathInfo = $event->getParameter('path_info'); if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) { $this->injectReCaptcha($event); $event->addSnippet('Google/MailQuestion/index.twig'); } } public function onMailQuestionConfirm(TemplateEvent $event) { $pathInfo = $event->getParameter('path_info'); if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) { $this->injectReCaptcha($event); $event->addSnippet('Google/MailQuestion/confirm.twig'); } }}