src/Eccube/Controller/CartController.php line 119

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller;
  13. use Eccube\Entity\BaseInfo;
  14. use Eccube\Entity\Order;
  15. use Eccube\Entity\ProductClass;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Repository\BaseInfoRepository;
  19. use Eccube\Repository\OrderRepository;
  20. use Eccube\Repository\ProductClassRepository;
  21. use Eccube\Service\CartService;
  22. use Eccube\Service\OrderHelper;
  23. use Eccube\Service\PurchaseFlow\PurchaseContext;
  24. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  25. use Eccube\Service\PurchaseFlow\PurchaseFlowResult;
  26. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\Routing\Annotation\Route;
  29. class CartController extends AbstractController
  30. {
  31.     /**
  32.      * @var ProductClassRepository
  33.      */
  34.     protected $productClassRepository;
  35.     /**
  36.      * @var CartService
  37.      */
  38.     protected $cartService;
  39.     /**
  40.      * @var PurchaseFlow
  41.      */
  42.     protected $purchaseFlow;
  43.     /**
  44.      * @var BaseInfo
  45.      */
  46.     protected $baseInfo;
  47.     /**
  48.      * CartController constructor.
  49.      *
  50.      * @param ProductClassRepository $productClassRepository
  51.      * @param CartService $cartService
  52.      * @param PurchaseFlow $cartPurchaseFlow
  53.      * @param BaseInfoRepository $baseInfoRepository
  54.      */
  55.     public function __construct(
  56.         ProductClassRepository $productClassRepository,
  57.         CartService $cartService,
  58.         PurchaseFlow $cartPurchaseFlow,
  59.         BaseInfoRepository $baseInfoRepository
  60.     ) {
  61.         $this->productClassRepository $productClassRepository;
  62.         $this->cartService $cartService;
  63.         $this->purchaseFlow $cartPurchaseFlow;
  64.         $this->baseInfo $baseInfoRepository->get();
  65.     }
  66.     /**
  67.      * カート画面.
  68.      *
  69.      * @Route("/cart", name="cart", methods={"GET"})
  70.      * @Template("Cart/index.twig")
  71.      */
  72.     public function index(Request $request)
  73.     {
  74.         // カートを取得して明細の正規化を実行
  75.         $Carts $this->cartService->getCarts();
  76.         $this->execPurchaseFlow($Carts);
  77.         // TODO itemHolderから取得できるように
  78.         $least = [];
  79.         $quantity = [];
  80.         $isDeliveryFree = [];
  81.         $totalPrice 0;
  82.         $totalQuantity 0;
  83.         foreach ($Carts as $Cart) {
  84.             $quantity[$Cart->getCartKey()] = 0;
  85.             $isDeliveryFree[$Cart->getCartKey()] = false;
  86.             if ($this->baseInfo->getDeliveryFreeQuantity()) {
  87.                 if ($this->baseInfo->getDeliveryFreeQuantity() > $Cart->getQuantity()) {
  88.                     $quantity[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeQuantity() - $Cart->getQuantity();
  89.                 } else {
  90.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  91.                 }
  92.             }
  93.             if ($this->baseInfo->getDeliveryFreeAmount()) {
  94.                 if (!$isDeliveryFree[$Cart->getCartKey()] && $this->baseInfo->getDeliveryFreeAmount() <= $Cart->getTotalPrice()) {
  95.                     $isDeliveryFree[$Cart->getCartKey()] = true;
  96.                 } else {
  97.                     $least[$Cart->getCartKey()] = $this->baseInfo->getDeliveryFreeAmount() - $Cart->getTotalPrice();
  98.                 }
  99.             }
  100.             $totalPrice += $Cart->getTotalPrice();
  101.             $totalQuantity += $Cart->getQuantity();
  102.         }
  103.         // カートが分割された時のセッション情報を削除
  104.         $request->getSession()->remove(OrderHelper::SESSION_CART_DIVIDE_FLAG);
  105.         
  106.         return [
  107.             'totalPrice' => $totalPrice,
  108.             'totalQuantity' => $totalQuantity,
  109.             // 空のカートを削除し取得し直す
  110.             'Carts' => $this->cartService->getCarts(true),
  111.             'least' => $least,
  112.             'quantity' => $quantity,
  113.             'is_delivery_free' => $isDeliveryFree,
  114.         ];
  115.     }
  116.     /**
  117.      * @param $Carts
  118.      *
  119.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|null
  120.      */
  121.     protected function execPurchaseFlow($Carts)
  122.     {
  123.         /** @var PurchaseFlowResult[] $flowResults */
  124.         $flowResults array_map(function ($Cart) {
  125.             $purchaseContext = new PurchaseContext($Cart$this->getUser());
  126.             return $this->purchaseFlow->validate($Cart$purchaseContext);
  127.         }, $Carts);
  128.         // 復旧不可のエラーが発生した場合はカートをクリアして再描画
  129.         $hasError false;
  130.         foreach ($flowResults as $result) {
  131.             if ($result->hasError()) {
  132.                 $hasError true;
  133.                 foreach ($result->getErrors() as $error) {
  134.                     $this->addRequestError($error->getMessage());
  135.                 }
  136.             }
  137.         }
  138.         if ($hasError) {
  139.             $this->cartService->clear();
  140.             return $this->redirectToRoute('cart');
  141.         }
  142.         $this->cartService->save();
  143.         foreach ($flowResults as $index => $result) {
  144.             foreach ($result->getWarning() as $warning) {
  145.                 if ($Carts[$index]->getItems()->count() > 0) {
  146.                     $cart_key $Carts[$index]->getCartKey();
  147.                     $this->addRequestError($warning->getMessage(), "front.cart.${cart_key}");
  148.                 } else {
  149.                     // キーが存在しない場合はグローバルにエラーを表示する
  150.                     $this->addRequestError($warning->getMessage());
  151.                 }
  152.             }
  153.         }
  154.         return null;
  155.     }
  156.     /**
  157.      * カート明細の加算/減算/削除を行う.
  158.      *
  159.      * - 加算
  160.      *      - 明細の個数を1増やす
  161.      * - 減算
  162.      *      - 明細の個数を1減らす
  163.      *      - 個数が0になる場合は、明細を削除する
  164.      * - 削除
  165.      *      - 明細を削除する
  166.      *
  167.      * @Route(
  168.      *     path="/cart/{operation}/{productClassId}",
  169.      *     name="cart_handle_item",
  170.      *     methods={"PUT"},
  171.      *     requirements={
  172.      *          "operation": "up|down|remove",
  173.      *          "productClassId": "\d+"
  174.      *     }
  175.      * )
  176.      */
  177.     public function handleCartItem($operation$productClassId)
  178.     {
  179.         log_info('カート明細操作開始', ['operation' => $operation'product_class_id' => $productClassId]);
  180.         $this->isTokenValid();
  181.         /** @var ProductClass $ProductClass */
  182.         $ProductClass $this->productClassRepository->find($productClassId);
  183.         if (is_null($ProductClass)) {
  184.             log_info('商品が存在しないため、カート画面へredirect', ['operation' => $operation'product_class_id' => $productClassId]);
  185.             return $this->redirectToRoute('cart');
  186.         }
  187.         // 明細の増減・削除
  188.         switch ($operation) {
  189.             case 'up':
  190.                 $this->cartService->addProduct($ProductClass1);
  191.                 break;
  192.             case 'down':
  193.                 $this->cartService->addProduct($ProductClass, -1);
  194.                 break;
  195.             case 'remove':
  196.                 $this->cartService->removeProduct($ProductClass);
  197.                 break;
  198.         }
  199.         // カートを取得して明細の正規化を実行
  200.         $Carts $this->cartService->getCarts();
  201.         $this->execPurchaseFlow($Carts);
  202.         log_info('カート演算処理終了', ['operation' => $operation'product_class_id' => $productClassId]);
  203.         return $this->redirectToRoute('cart');
  204.     }
  205.     /**
  206.      * カートをロック状態に設定し、購入確認画面へ遷移する.
  207.      *
  208.      * @Route("/cart/buystep/{cart_key}", name="cart_buystep", requirements={"cart_key" = "[a-zA-Z0-9]+[_][\x20-\x7E]+"}, methods={"GET"})
  209.      */
  210.     public function buystep(Request $request$cart_key)
  211.     {
  212.         $Carts $this->cartService->getCart();
  213.         if (!is_object($Carts)) {
  214.             return $this->redirectToRoute('cart');
  215.         }
  216.         // FRONT_CART_BUYSTEP_INITIALIZE
  217.         $event = new EventArgs(
  218.             [],
  219.             $request
  220.         );
  221.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CART_BUYSTEP_INITIALIZE);
  222.         $this->cartService->setPrimary($cart_key);
  223.         $this->cartService->save();
  224.         // FRONT_CART_BUYSTEP_COMPLETE
  225.         $event = new EventArgs(
  226.             [],
  227.             $request
  228.         );
  229.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_CART_BUYSTEP_COMPLETE);
  230.         if ($event->hasResponse()) {
  231.             return $event->getResponse();
  232.         }
  233.         return $this->redirectToRoute('shopping');
  234.     }
  235. }