app/Customize/EventSubscriber/Default/MypageHistorySubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace Customize\EventSubscriber\Default;
  3. use Customize\Repository\ContractRepository;
  4. use Eccube\Entity\Order;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Eccube\Event\{
  7.     EccubeEvents,
  8.     EventArgs
  9. };
  10. class MypageHistorySubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var ContractRepository
  14.      */
  15.     private $contractRepository;
  16.     /**
  17.      * Summary of __construct
  18.      * @param \Customize\Repository\ContractRepository $contractRepository
  19.      */
  20.     public function __construct(ContractRepository $contractRepository)
  21.     {
  22.         $this->contractRepository $contractRepository;
  23.     }
  24.     /**
  25.      * マイページの履歴ページの初期化イベントを購読
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             EccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE => 'onMypageHistoryInit',
  31.         ];
  32.     }
  33.     /**
  34.      * マイページの履歴ページの初期化イベントを購読
  35.      * @param \Eccube\Event\EventArgs $event
  36.      * @return void
  37.      */
  38.     public function onMypageHistoryInit(EventArgs $event)
  39.     {
  40.         // イベントから注文情報を取得
  41.         /** @var Order $Order */
  42.         $Order $event->getArgument('Order');
  43.         if ($Order->getContractId() === null) {
  44.             throw new \RuntimeException(trans('error.contract_id_is_null'));
  45.         }
  46.         $Contract $this->contractRepository->get($Order->getContractId());
  47.         $Order->setContract($Contract);
  48.         // データを更新
  49.         $event->setArgument('Order'$Order);
  50.     }
  51. }