<?php
namespace Plugin\ApexVolumeUp;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Entity\Master\OrderItemType;
use Eccube\Entity\Order;
use Eccube\Entity\OrderItem;
use Eccube\Event\TemplateEvent;
use Eccube\Repository\OrderItemRepository;
use Plugin\ApexSetupFee\Form\Type\SetupFeeOrderType;
use Plugin\ApexVolumeUp\Service\VolumeUpService;
use Plugin\ApexVolumeUp\Entity\VolumeUp;
use Plugin\ApexVolumeUp\Form\Type\VolumeUpType;
use Plugin\ApexVolumeUp\Repository\VolumeUpRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormFactoryInterface;
class Event implements EventSubscriberInterface
{
/**
* Event constructor.
*
*/
public function __construct(
EntityManagerInterface $entityManager,
VolumeUpRepository $volumeUpRepository,
VolumeUpService $volumeUpService,
FormFactoryInterface $formFactory
) {
$this->entityManager = $entityManager;
$this->volumeUpRepository = $volumeUpRepository;
$this->volumeUpService = $volumeUpService;
$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'];
// ボリュームアップを取得
$VolumeUp = $this->volumeUpRepository->findOneBy(['pre_order_id' => $Order->getPreOrderId()]);
if(is_null($VolumeUp)){
$this->volumeUpService->saveVolumeUp($Order);
$VolumeUp = $this->volumeUpRepository->findOneBy(['pre_order_id' => $Order->getPreOrderId()]);
}
$parameters['VolumeUp'] = $VolumeUp;
$event->setParameters($parameters);
$VolumeUpForm = $this->formFactory->createBuilder(VolumeUpType::class, $VolumeUp)->getForm();
$parameters['VolumeUpForm'] = $VolumeUpForm->createView();
$event->setParameters($parameters);
if (strpos($event->getView(), 'index.twig') !== false) {
$event->addSnippet('@ApexVolumeUp/default/shopping_index.twig');
} else {
$event->addSnippet('@ApexVolumeUp/default/shopping_confirm.twig');
}
}
/**
* Hook point add coupon information to mypage history.
*
* @param TemplateEvent $event
*/
public function onRenderMypageHistory(TemplateEvent $event)
{
log_info('VolumeUp trigger onRenderMypageHistory start');
$parameters = $event->getParameters();
if (is_null($parameters['Order'])) {
return;
}
$Order = $parameters['Order'];
// ボリュームアップ情報を取得する
$VolumeUp = $this->volumeUpRepository->findOneBy([
'order_id' => $Order->getId(),
]);
if (is_null($VolumeUp)) {
return;
}
// set parameter for twig files
$parameters['VolumeUp'] = $VolumeUp;
$event->setParameters($parameters);
$event->addSnippet('@ApexVolumeUp/default/mypage_history.twig');
log_info('VolumeUp trigger onRenderMypageHistory finish');
}
}