src/Eccube/Controller/Mypage/MypageController.php line 93

Open in your IDE?
  1. <?php
  2. namespace Eccube\Controller\Mypage;
  3. use Eccube\Controller\AbstractController;
  4. use Eccube\Entity\BaseInfo;
  5. use Eccube\Entity\Customer;
  6. use Eccube\Entity\Order;
  7. use Eccube\Entity\Product;
  8. use Eccube\Event\EccubeEvents;
  9. use Eccube\Event\EventArgs;
  10. use Eccube\Exception\CartException;
  11. use Eccube\Form\Type\Front\CustomerLoginType;
  12. use Eccube\Repository\BaseInfoRepository;
  13. use Eccube\Repository\CustomerFavoriteProductRepository;
  14. use Eccube\Repository\OrderRepository;
  15. use Eccube\Repository\ProductRepository;
  16. use Eccube\Service\CartService;
  17. use Eccube\Service\PurchaseFlow\PurchaseContext;
  18. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use phpseclib3\Tests\Functional\Net\SFTPLargeFileTest;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  24. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  27. class MypageController extends AbstractController
  28. {
  29.   /**
  30.    * @var ProductRepository
  31.    */
  32.   protected $productRepository;
  33.   /**
  34.    * @var CustomerFavoriteProductRepository
  35.    */
  36.   protected $customerFavoriteProductRepository;
  37.   /**
  38.    * @var BaseInfo
  39.    */
  40.   protected $BaseInfo;
  41.   /**
  42.    * @var CartService
  43.    */
  44.   protected $cartService;
  45.   /**
  46.    * @var OrderRepository
  47.    */
  48.   protected $orderRepository;
  49.   /**
  50.    * @var PurchaseFlow
  51.    */
  52.   protected $purchaseFlow;
  53.   /**
  54.    * MypageController constructor.
  55.    *
  56.    * @param OrderRepository $orderRepository
  57.    * @param CustomerFavoriteProductRepository $customerFavoriteProductRepository
  58.    * @param CartService $cartService
  59.    * @param BaseInfoRepository $baseInfoRepository
  60.    * @param PurchaseFlow $purchaseFlow
  61.    */
  62.   public function __construct(
  63.     OrderRepository                   $orderRepository,
  64.     CustomerFavoriteProductRepository $customerFavoriteProductRepository,
  65.     CartService                       $cartService,
  66.     BaseInfoRepository                $baseInfoRepository,
  67.     PurchaseFlow                      $purchaseFlow
  68.   )
  69.   {
  70.     $this->orderRepository $orderRepository;
  71.     $this->customerFavoriteProductRepository $customerFavoriteProductRepository;
  72.     $this->BaseInfo $baseInfoRepository->get();
  73.     $this->cartService $cartService;
  74.     $this->purchaseFlow $purchaseFlow;
  75.   }
  76.   /**
  77.    * ログイン画面.
  78.    *
  79.    * @Route("/mypage/login", name="mypage_login", methods={"GET", "POST"})
  80.    * @Template("Mypage/login.twig")
  81.    */
  82.   public function login(Request $requestAuthenticationUtils $utils)
  83.   {
  84.     if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  85.       log_info('認証済のためログイン処理をスキップ');
  86.       return $this->redirectToRoute('mypage');
  87.     }
  88.     /* @var $form \Symfony\Component\Form\FormInterface */
  89.     $builder $this->formFactory
  90.       ->createNamedBuilder(''CustomerLoginType::class);
  91.     $builder->get('login_memory')->setData((bool)$request->getSession()->get('_security.login_memory'));
  92.     if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  93.       $Customer $this->getUser();
  94.       if ($Customer instanceof Customer) {
  95.         $builder->get('login_email')
  96.           ->setData($Customer->getEmail());
  97.       }
  98.     }
  99.     $event = new EventArgs(
  100.       [
  101.         'builder' => $builder,
  102.       ],
  103.       $request
  104.     );
  105.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_LOGIN_INITIALIZE);
  106.     $form $builder->getForm();
  107.     return [
  108.       'error' => $utils->getLastAuthenticationError(),
  109.       'form' => $form->createView(),
  110.     ];
  111.   }
  112.   /**
  113.    * マイページ.
  114.    *
  115.    * @Route("/mypage/", name="mypage", methods={"GET"})
  116.    * @Template("Mypage/index.twig")
  117.    */
  118.   public function index(Request $requestPaginatorInterface $paginator)
  119.   {
  120.     $Customer $this->getUser();
  121.     // 購入処理中/決済処理中ステータスの受注を非表示にする.
  122.     $this->entityManager
  123.       ->getFilters()
  124.       ->enable('incomplete_order_status_hidden');
  125.     // paginator
  126.     $qb $this->orderRepository->getQueryBuilderByCustomer($Customer);
  127.     $event = new EventArgs(
  128.       [
  129.         'qb' => $qb,
  130.         'Customer' => $Customer,
  131.       ],
  132.       $request
  133.     );
  134.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_INDEX_SEARCH);
  135.     $pagination $paginator->paginate(
  136.       $qb,
  137.       $request->get('pageno'1),
  138.       $this->eccubeConfig['eccube_search_pmax']
  139.     );
  140.     return [
  141.       'pagination' => $pagination,
  142.     ];
  143.   }
  144.   /**
  145.    * 購入履歴詳細を表示する.
  146.    *
  147.    * @Route("/mypage/history/{order_no}", name="mypage_history", methods={"GET"})
  148.    * @Template("Mypage/history.twig")
  149.    */
  150.   public function history(Request $request$order_no)
  151.   {
  152.     $this->entityManager->getFilters()
  153.       ->enable('incomplete_order_status_hidden');
  154.     $Order $this->orderRepository->findOneBy(
  155.       [
  156.         'order_no' => $order_no,
  157.         'Customer' => $this->getUser(),
  158.       ]
  159.     );
  160.     $event = new EventArgs(
  161.       [
  162.         'Order' => $Order,
  163.       ],
  164.       $request
  165.     );
  166.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_HISTORY_INITIALIZE);
  167.     /** @var Order $Order */
  168.     $Order $event->getArgument('Order');
  169.     if (!$Order) {
  170.       throw new NotFoundHttpException();
  171.     }
  172.     $stockOrder true;
  173.     foreach ($Order->getOrderItems() as $orderItem) {
  174.       if ($orderItem->isProduct() && $orderItem->getQuantity() < 0) {
  175.         $stockOrder false;
  176.         break;
  177.       }
  178.     }
  179.     return [
  180.       'Order' => $Order,
  181.       'stockOrder' => $stockOrder,
  182.     ];
  183.   }
  184.   /**
  185.    * 再購入を行う.
  186.    *
  187.    * @Route("/mypage/order/{order_no}", name="mypage_order", methods={"PUT"})
  188.    */
  189.   public function order(Request $request$order_no)
  190.   {
  191.     $this->isTokenValid();
  192.     log_info('再注文開始', [$order_no]);
  193.     $Customer $this->getUser();
  194.     /* @var $Order \Eccube\Entity\Order */
  195.     $Order $this->orderRepository->findOneBy(
  196.       [
  197.         'order_no' => $order_no,
  198.         'Customer' => $Customer,
  199.       ]
  200.     );
  201.     $event = new EventArgs(
  202.       [
  203.         'Order' => $Order,
  204.         'Customer' => $Customer,
  205.       ],
  206.       $request
  207.     );
  208.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_INITIALIZE);
  209.     if (!$Order) {
  210.       log_info('対象の注文が見つかりません', [$order_no]);
  211.       throw new NotFoundHttpException();
  212.     }
  213.     // エラーメッセージの配列
  214.     $errorMessages = [];
  215.     foreach ($Order->getOrderItems() as $OrderItem) {
  216.       try {
  217.         if ($OrderItem->getProduct() && $OrderItem->getProductClass()) {
  218.           $this->cartService->addProduct($OrderItem->getProductClass(), $OrderItem->getQuantity());
  219.           // 明細の正規化
  220.           $Carts $this->cartService->getCarts();
  221.           foreach ($Carts as $Cart) {
  222.             $result $this->purchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  223.             // 復旧不可のエラーが発生した場合は追加した明細を削除.
  224.             if ($result->hasError()) {
  225.               $this->cartService->removeProduct($OrderItem->getProductClass());
  226.               foreach ($result->getErrors() as $error) {
  227.                 $errorMessages[] = $error->getMessage();
  228.               }
  229.             }
  230.             foreach ($result->getWarning() as $warning) {
  231.               $errorMessages[] = $warning->getMessage();
  232.             }
  233.           }
  234.           $this->cartService->save();
  235.         }
  236.       } catch (CartException $e) {
  237.         log_info($e->getMessage(), [$order_no]);
  238.         $this->addRequestError($e->getMessage());
  239.       }
  240.     }
  241.     foreach ($errorMessages as $errorMessage) {
  242.       $this->addRequestError($errorMessage);
  243.     }
  244.     $event = new EventArgs(
  245.       [
  246.         'Order' => $Order,
  247.         'Customer' => $Customer,
  248.       ],
  249.       $request
  250.     );
  251.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_ORDER_COMPLETE);
  252.     if ($event->getResponse() !== null) {
  253.       return $event->getResponse();
  254.     }
  255.     log_info('再注文完了', [$order_no]);
  256.     return $this->redirect($this->generateUrl('cart'));
  257.   }
  258.   /**
  259.    * お気に入り商品を表示する.
  260.    *
  261.    * @Route("/mypage/favorite", name="mypage_favorite", methods={"GET"})
  262.    * @Template("Mypage/favorite.twig")
  263.    */
  264.   public function favorite(Request $requestPaginatorInterface $paginator)
  265.   {
  266.     if (!$this->BaseInfo->isOptionFavoriteProduct()) {
  267.       throw new NotFoundHttpException();
  268.     }
  269.     $Customer $this->getUser();
  270.     // paginator
  271.     $qb $this->customerFavoriteProductRepository->getQueryBuilderByCustomer($Customer);
  272.     $event = new EventArgs(
  273.       [
  274.         'qb' => $qb,
  275.         'Customer' => $Customer,
  276.       ],
  277.       $request
  278.     );
  279.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_FAVORITE_SEARCH);
  280.     $pagination $paginator->paginate(
  281.       $qb,
  282.       $request->get('pageno'1),
  283.       $this->eccubeConfig['eccube_search_pmax'],
  284.       ['wrap-queries' => true]
  285.     );
  286.     return [
  287.       'pagination' => $pagination,
  288.     ];
  289.   }
  290.   /**
  291.    * お気に入り商品を削除する.
  292.    *
  293.    * @Route("/mypage/favorite/{id}/delete", name="mypage_favorite_delete", methods={"DELETE"}, requirements={"id" = "\d+"})
  294.    */
  295.   public function delete(Request $requestProduct $Product)
  296.   {
  297.     $this->isTokenValid();
  298.     $Customer $this->getUser();
  299.     log_info('お気に入り商品削除開始', [$Customer->getId(), $Product->getId()]);
  300.     $CustomerFavoriteProduct $this->customerFavoriteProductRepository->findOneBy(['Customer' => $Customer'Product' => $Product]);
  301.     if ($CustomerFavoriteProduct) {
  302.       $this->customerFavoriteProductRepository->delete($CustomerFavoriteProduct);
  303.     } else {
  304.       throw new BadRequestHttpException();
  305.     }
  306.     $event = new EventArgs(
  307.       [
  308.         'Customer' => $Customer,
  309.         'CustomerFavoriteProduct' => $CustomerFavoriteProduct,
  310.       ], $request
  311.     );
  312.     $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_MYPAGE_MYPAGE_DELETE_COMPLETE);
  313.     log_info('お気に入り商品削除完了', [$Customer->getId(), $CustomerFavoriteProduct->getId()]);
  314.     return $this->redirect($this->generateUrl('mypage_favorite'));
  315.   }
  316. }