app/Plugin/PrivateURL42/EventSubscriber/PrivateSubscriber.php line 94

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of PrivateURL42
  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\PrivateURL42\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. class PrivateSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var EntityManagerInterface
  26.      */
  27.     protected EntityManagerInterface $entityManager;
  28.     /**
  29.      * @var Context
  30.      */
  31.     protected Context $requestContent;
  32.     /**
  33.      * @var RequestStack
  34.      */
  35.     protected RequestStack $requestStack;
  36.     public function __construct(
  37.         EntityManagerInterface $entityManager,
  38.         Context $requestContent,
  39.         RequestStack $requestStack
  40.     ) {
  41.         $this->entityManager $entityManager;
  42.         $this->requestContent $requestContent;
  43.         $this->requestStack $requestStack;
  44.     }
  45.     /**
  46.      * 注文完了したら秘密キーをセッションから削除
  47.      *
  48.      * @param EventArgs $event
  49.      * @return void
  50.      */
  51.     public function onFrontShoppingCompleteInitialize(EventArgs $event): void
  52.     {
  53. //                $this->requestStack->getSession()->remove(Constant::SESSION_PLG_PRIVATE_URL_PRIVATE_KEY);
  54.     }
  55.     /**
  56.      * 商品を複製したときに秘密キーを生成
  57.      *
  58.      * @param EventArgs $event
  59.      * @return void
  60.      */
  61.     public function onAdminProductCopyComplete(EventArgs $event): void
  62.     {
  63.         $Product $event->getArgument('CopyProduct');
  64.         if ($Product instanceof Product) {
  65.             // 有効化された場合プライベートキー発行
  66.             if ($Product->isPlgPuPrivate()) {
  67.                 do {
  68.                     $key StringUtil::random(32);
  69.                     $private_key $this->entityManager->getRepository(Product::class)->findOneBy(['plg_pu_private_key' => $key]);
  70.                 } while ($private_key);
  71.                 $Product->setPlgPuPrivateKey($key);
  72.                 $this->entityManager->persist($Product);
  73.                 $this->entityManager->flush();
  74.             }
  75.         }
  76.     }
  77.     /**
  78.      * 非公開キーがURLパラメーターにある場合、noindex付与
  79.      *
  80.      * @param TemplateEvent $event
  81.      * @return void
  82.      */
  83.     public function onTemplateProductDetail(TemplateEvent $event): void
  84.     {
  85.         $request $this->requestStack->getCurrentRequest();
  86.         if ($request->get('private_key')) {
  87.             $event->addAsset('@PrivateURL42/default/noindex.twig');
  88.         }
  89.     }
  90.     /**
  91.      * @return string[]
  92.      */
  93.     public static function getSubscribedEvents(): array
  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. }