app/Customize/Controller/CartController.php line 80

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