<?php
namespace Customize\EventSubscriber\Default;
use Customize\Repository\ContractRepository;
use Eccube\Entity\Order;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Eccube\Event\{
EccubeEvents,
EventArgs
};
class MypageHistorySubscriber implements EventSubscriberInterface
{
/**
* @var ContractRepository
*/
private $contractRepository;
/**
* Summary of __construct
* @param \Customize\Repository\ContractRepository $contractRepository
*/
public function __construct(ContractRepository $contractRepository)
{
$this->contractRepository = $contractRepository;
}
/**
* マイページの履歴ページの初期化イベントを購読
*/
public static function getSubscribedEvents(): array
{
return [
EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE => 'onMypageHistoryInit',
];
}
/**
* マイページの履歴ページの初期化イベントを購読
* @param \Eccube\Event\EventArgs $event
* @return void
*/
public function onMypageHistoryInit(EventArgs $event)
{
// イベントから注文情報を取得
/** @var Order $Order */
$Order = $event->getArgument('Order');
if ($Order->getContractId() === null) {
throw new \RuntimeException(trans('error.contract_id_is_null'));
}
$Contract = $this->contractRepository->get($Order->getContractId());
$Order->setContract($Contract);
// データを更新
$event->setArgument('Order', $Order);
}
}