app/Plugin/PrivateURL4/EventSubscriber/PrivateSubscriber.php line 75

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of PrivateURL4
  4.  *
  5.  * Copyright(c) Akira Kurozumi <info@a-zumi.net>
  6.  *
  7.  * https://a-zumi.net
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\PrivateURL4\EventSubscriber;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Entity\Product;
  15. use Eccube\Event\EccubeEvents;
  16. use Eccube\Event\EventArgs;
  17. use Eccube\Event\TemplateEvent;
  18. use Eccube\Request\Context;
  19. use Eccube\Util\StringUtil;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpFoundation\RequestStack;
  22. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  23. class PrivateSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var SessionInterface
  27.      */
  28.     private $session;
  29.     /**
  30.      * @var EntityManagerInterface
  31.      */
  32.     private $entityManager;
  33.     /**
  34.      * @var Context
  35.      */
  36.     private $requestContent;
  37.     /**
  38.      * @var RequestStack
  39.      */
  40.     private $requestStack;
  41.     public function __construct(
  42.         SessionInterface $session,
  43.         EntityManagerInterface $entityManager,
  44.         Context $requestContent,
  45.         RequestStack $requestStack
  46.     ) {
  47.         $this->session $session;
  48.         $this->entityManager $entityManager;
  49.         $this->requestContent $requestContent;
  50.         $this->requestStack $requestStack;
  51.     }
  52.     /**
  53.      * 注文完了したら秘密キーをセッションから削除
  54.      *
  55.      * @param EventArgs $event
  56.      */
  57.     public function onFrontShoppingCompleteInitialize(EventArgs $event)
  58.     {
  59. //        $this->session->remove(Constant::SESSION_PLG_PRIVATE_URL_PRIVATE_KEY);
  60.     }
  61.     /**
  62.      * 商品を複製したときに秘密キーを生成
  63.      *
  64.      * @param EventArgs $event
  65.      */
  66.     public function onAdminProductCopyComplete(EventArgs $event)
  67.     {
  68.         $Product $event->getArgument("CopyProduct");
  69.         if($Product instanceof Product) {
  70.             // 有効化された場合プライベートキー発行
  71.             if ($Product->isPlgPuPrivate()) {
  72.                 do {
  73.                     $key StringUtil::random(32);
  74.                     $private_key $this->entityManager->getRepository(Product::class)->findOneBy(["plg_pu_private_key" => $key]);
  75.                 } while ($private_key);
  76.                 $Product->setPlgPuPrivateKey($key);
  77.                 $this->entityManager->persist($Product);
  78.                 $this->entityManager->flush();
  79.             }
  80.         }
  81.     }
  82.     /**
  83.      * 非公開キーがURLパラメーターにある場合、noindex付与
  84.      * @param TemplateEvent $event
  85.      */
  86.     public function onTemplateProductDetail(TemplateEvent $event)
  87.     {
  88.         $request $this->requestStack->getCurrentRequest();
  89.         if($request->get('private_key')) {
  90.             $event->addAsset('@PrivateURL4/default/noindex.twig');
  91.         }
  92.     }
  93.     public static function getSubscribedEvents()
  94.     {
  95.         return [
  96.             EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE => 'onFrontShoppingCompleteInitialize',
  97.             EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'onAdminProductCopyComplete',
  98.             'Product/detail.twig' => 'onTemplateProductDetail'
  99.         ];
  100.     }
  101. }