app/Customize/EventListener/GoogleTrustedStoreEventListener.php line 38

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Eccube\Entity\Order;
  4. use Eccube\Event\TemplateEvent;
  5. use Lm\Entity\Jancode;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Twig\Environment;
  11. class GoogleTrustedStoreEventListener implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var Environment
  15.      */
  16.     private $twig;
  17.     public function __construct(Environment $twig)
  18.     {
  19.         $this->twig $twig;
  20.     }
  21.     /**
  22.      * @inheritDoc
  23.      */
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             KernelEvents::RESPONSE => 'onKernelResponse',
  28.             'Shopping/complete.twig' => 'onShoppingComplete',
  29.         ];
  30.     }
  31.     public function onKernelResponse(ResponseEvent $event)
  32.     {
  33.         //
  34.         $response $event->getResponse();
  35.         $request $event->getRequest();
  36.         //
  37.         if (!$event->isMasterRequest()) {
  38.             //
  39.             return;
  40.         }
  41.         //
  42.         if ($request->isXmlHttpRequest()) {
  43.             return;
  44.         }
  45.         //
  46.         $this->injectBadge($response);
  47.     }
  48.     public function onShoppingComplete(TemplateEvent $event)
  49.     {
  50.         //
  51.         $this->injectOptIn($event);
  52.     }
  53.     protected function injectBadge(Response $response)
  54.     {
  55.         //
  56.         $content $response->getContent();
  57.         $pos strripos($content'</body>');
  58.         //
  59.         if (false !== $pos) {
  60.             $badge "\n".$this->twig->render('GoogleTrustedStore/badge.twig', [
  61.             ]);
  62.             $content substr($content0$pos).$badge.substr($content$pos);
  63.             $response->setContent($content);
  64.         }
  65.     }
  66.     protected function injectOptIn(TemplateEvent $event)
  67.     {
  68.         //
  69.         $parameter $event->hasParameter('GoogleTrustedStore') ? $event->getParameter('GoogleTrustedStore') : [];
  70.         /**
  71.          * @var Order $order
  72.          */
  73.         if ($event->hasParameter('Order') && ($order $event->getParameter('Order'))) {
  74.             //
  75.             $products = [];
  76.             //
  77.             foreach ($order->getProductOrderItems() as $orderItem) {
  78.                 //
  79.                 if (($jan = new Jancode($orderItem->getProductClass()->getId())) && ($janCode $jan->getJanJancode())) {
  80.                     //
  81.                     $products[] = [
  82.                         'gtin' => $janCode,
  83.                     ];
  84.                 }
  85.             }
  86.             //
  87.             $parameter['optin']['products'] = $products;
  88.         }
  89.         //
  90.         $event->setParameter('GoogleTrustedStore'$parameter);
  91.         $event->addSnippet('GoogleTrustedStore/optin.twig');
  92.     }
  93. }