app/Customize/Controller/CartController.php line 89

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