app/Customize/EventListener/GoogleEventListener.php line 108

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Customize\Service\Google\ReCaptchaService;
  4. use Eccube\Event\TemplateEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\Event\ViewEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Twig\Environment;
  9. class GoogleEventListener implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Environment
  13.      */
  14.     private $reCaptchaService;
  15.     public function __construct(ReCaptchaService $reCaptchaService)
  16.     {
  17.         $this->reCaptchaService $reCaptchaService;
  18.     }
  19.     /**
  20.      * @inheritDoc
  21.      */
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             KernelEvents::VIEW => 'onKernelView',
  26.             'Contact/index.twig' => 'onContactIndex',
  27.             'Contact/confirm.twig' => 'onContactConfirm',
  28.             'Entry/index.twig' => 'onEntryIndex',
  29.             'Entry/confirm.twig' => 'onEntryIndex',
  30.             'Forgot/index.twig' => 'onForgotIndex',
  31.             'Mail/question.twig' => 'onMailQuestionIndex',
  32.             'Mail/question_confirm.twig' => 'onMailQuestionConfirm',
  33.         ];
  34.     }
  35.     /**
  36.      * Add path_info to controller result
  37.      *
  38.      * @param ViewEvent $event
  39.      * @return void
  40.      */
  41.     public function onKernelView(ViewEvent $event) {
  42.         $controllerResult $event->getControllerResult();
  43.         $request $event->getRequest();
  44.         $pathInfo $request->getPathInfo();
  45.         $controllerResult['path_info'] = $pathInfo;
  46.         $event->setControllerResult($controllerResult);
  47.     }
  48.     public function injectReCaptcha(TemplateEvent $event)
  49.     {
  50.         $event->addSnippet('Google/reCAPTCHA.twig');
  51.     }
  52.     public function onContactIndex(TemplateEvent $event)
  53.     {
  54.         $pathInfo $event->getParameter('path_info');
  55.         if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) {
  56.             $this->injectReCaptcha($event);
  57.             $event->addSnippet('Google/Contact/index.twig');
  58.         }
  59.     }
  60.     public function onContactConfirm(TemplateEvent $event)
  61.     {
  62.         $pathInfo $event->getParameter('path_info');
  63.         if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) {
  64.             $this->injectReCaptcha($event);
  65.             $event->addSnippet('Google/Contact/confirm.twig');
  66.         }
  67.     }
  68.     public function onEntryIndex(TemplateEvent $event)
  69.     {
  70.         $pathInfo $event->getParameter('path_info');
  71.         if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo)) {
  72.             $this->injectReCaptcha($event);
  73.             $event->addSnippet('Google/Entry/index.twig');
  74.         }
  75.     }
  76.     public function onForgotIndex(TemplateEvent $event)
  77.     {
  78.         $pathInfo $event->getParameter('path_info');
  79.         if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo))
  80.         {
  81.             $this->injectReCaptcha($event);
  82.             $event->addSnippet('Google/Forgot/index.twig');
  83.         }
  84.     }
  85.     public function onMailQuestionIndex(TemplateEvent $event)
  86.     {
  87.         $pathInfo $event->getParameter('path_info');
  88.         if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo))
  89.         {
  90.             $this->injectReCaptcha($event);
  91.             $event->addSnippet('Google/MailQuestion/index.twig');
  92.         }
  93.     }
  94.     public function onMailQuestionConfirm(TemplateEvent $event)
  95.     {
  96.         $pathInfo $event->getParameter('path_info');
  97.         if ($this->reCaptchaService->shouldProcessReCaptcha($pathInfo))
  98.         {
  99.             $this->injectReCaptcha($event);
  100.             $event->addSnippet('Google/MailQuestion/confirm.twig');
  101.         }
  102.     }
  103. }