<?php
namespace Plugin\ApexSetupFee;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Order;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\OrderRepository;
use Plugin\ApexSetupFee\Form\Type\SetupFeeOrderType;
use Plugin\ApexSetupFee\Repository\SetupFeeOrderRepository;
use Plugin\ApexSetupFee\Repository\SetupFeeRepository;
use Plugin\ApexSetupFee\Service\SetupFeeService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
class Event implements EventSubscriberInterface
{
/**
* @var SetupFeeOrderRepository
*/
private $setupFeeOrderRepository;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var SetupFeeRepository
*/
private $setupFeeRepository;
/**
* @var SetupFeeService
*/
private $SetupFeeService;
/**
* @var OrderRepository
*/
private $orderRepository;
/**
* @var \Twig_Environment
*/
private $twig;
/**
* @var FormFactoryInterface
*/
private $formFactory;
/**
* Event constructor.
*
* @param SetupFeeOrderRepository $setupFeeOrderRepository
* @param EntityManagerInterface $entityManager
* @param SetupFeeRepository $setupFeeRepository
* @param OrderRepository $orderRepository
* @param \Twig_Environment $twig
*/
public function __construct(SetupFeeOrderRepository $setupFeeOrderRepository,
EntityManagerInterface $entityManager,
SetupFeeRepository $setupFeeRepository,
SetupFeeService $setupFeeService,
OrderRepository $orderRepository,
\Twig_Environment $twig,
FormFactoryInterface $formFactory
) {
$this->setupFeeOrderRepository = $setupFeeOrderRepository;
$this->entityManager = $entityManager;
$this->setupFeeRepository = $setupFeeRepository;
$this->setupFeeService = $setupFeeService;
$this->orderRepository = $orderRepository;
$this->twig = $twig;
$this->formFactory = $formFactory;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'Shopping/index.twig' => 'index',
'Shopping/confirm.twig' => 'index',
'Mypage/history.twig' => 'onRenderMypageHistory',
];
}
/**
* @param TemplateEvent $event
*/
public function index(TemplateEvent $event)
{
$parameters = $event->getParameters();
/** @var Order $Order */
$Order = $parameters['Order'];
// クーポンが未入力でクーポン情報が存在すればクーポン情報を削除
$SetupFeeOrder = $this->setupFeeOrderRepository->getSetupFeeOrder($Order->getPreOrderId());
if(is_null($SetupFeeOrder)){
$SetupFee = $this->setupFeeRepository->find(1);
$this->setupFeeService->saveSetupFeeOrder($Order, $SetupFee);
$SetupFeeOrder = $this->setupFeeOrderRepository->getSetupFeeOrder($Order->getPreOrderId());
}
$parameters['SetupFeeOrder'] = $SetupFeeOrder;
$event->setParameters($parameters);
$SetupFees_arr = $this->setupFeeRepository->findBy(
['visible' => true],
['id' => 'ASC']
);
$SetupFees = [];
foreach ($SetupFees_arr as $v){
$SetupFees[$v->getId()] = $v;
}
$parameters['SetupFees'] = $SetupFees;
$SetupFeeForm = $this->formFactory->createBuilder(SetupFeeOrderType::class, null, ['setup_fee_id' => $SetupFeeOrder->getSetupFeeId()])->getForm();
$parameters['SetupFeeForm'] = $SetupFeeForm->createView();
$event->setParameters($parameters);
if (strpos($event->getView(), 'index.twig') !== false) {
$event->addSnippet('@ApexSetupFee/default/shopping_index.twig');
} else {
$event->addSnippet('@ApexSetupFee/default/shopping_confirm.twig');
}
}
/**
* Hook point add coupon information to mypage history.
*
* @param TemplateEvent $event
*/
public function onRenderMypageHistory(TemplateEvent $event)
{
log_info('SetupFee trigger onRenderMypageHistory start');
$parameters = $event->getParameters();
if (is_null($parameters['Order'])) {
return;
}
$Order = $parameters['Order'];
// クーポン受注情報を取得する
$SetupFeeOrder = $this->setupFeeOrderRepository->findOneBy([
'order_id' => $Order->getId(),
]);
if (is_null($SetupFeeOrder)) {
return;
}
// set parameter for twig files
$parameters['setup_fee_name'] = $SetupFeeOrder->getSetupFeeName();
$event->setParameters($parameters);
$event->addSnippet('@ApexSetupFee/default/mypage_history.twig');
log_info('Coupon trigger onRenderMypageHistory finish');
}
}