<?php
/**
* This file is part of PrivateURL4
*
* Copyright(c) Akira Kurozumi <info@a-zumi.net>
*
* https://a-zumi.net
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\PrivateURL4\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Product;
use Eccube\Event\EccubeEvents;
use Eccube\Event\EventArgs;
use Eccube\Event\TemplateEvent;
use Eccube\Request\Context;
use Eccube\Util\StringUtil;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class PrivateSubscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
private $session;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var Context
*/
private $requestContent;
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
SessionInterface $session,
EntityManagerInterface $entityManager,
Context $requestContent,
RequestStack $requestStack
) {
$this->session = $session;
$this->entityManager = $entityManager;
$this->requestContent = $requestContent;
$this->requestStack = $requestStack;
}
/**
* 注文完了したら秘密キーをセッションから削除
*
* @param EventArgs $event
*/
public function onFrontShoppingCompleteInitialize(EventArgs $event)
{
// $this->session->remove(Constant::SESSION_PLG_PRIVATE_URL_PRIVATE_KEY);
}
/**
* 商品を複製したときに秘密キーを生成
*
* @param EventArgs $event
*/
public function onAdminProductCopyComplete(EventArgs $event)
{
$Product = $event->getArgument("CopyProduct");
if($Product instanceof Product) {
// 有効化された場合プライベートキー発行
if ($Product->isPlgPuPrivate()) {
do {
$key = StringUtil::random(32);
$private_key = $this->entityManager->getRepository(Product::class)->findOneBy(["plg_pu_private_key" => $key]);
} while ($private_key);
$Product->setPlgPuPrivateKey($key);
$this->entityManager->persist($Product);
$this->entityManager->flush();
}
}
}
/**
* 非公開キーがURLパラメーターにある場合、noindex付与
* @param TemplateEvent $event
*/
public function onTemplateProductDetail(TemplateEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
if($request->get('private_key')) {
$event->addAsset('@PrivateURL4/default/noindex.twig');
}
}
public static function getSubscribedEvents()
{
return [
EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE => 'onFrontShoppingCompleteInitialize',
EccubeEvents::ADMIN_PRODUCT_COPY_COMPLETE => 'onAdminProductCopyComplete',
'Product/detail.twig' => 'onTemplateProductDetail'
];
}
}