src/Eccube/Service/CartService.php line 174

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\Service;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Doctrine\ORM\UnitOfWork;
  15. use Eccube\Entity\Cart;
  16. use Eccube\Entity\CartItem;
  17. use Eccube\Entity\Customer;
  18. use Eccube\Entity\ItemHolderInterface;
  19. use Eccube\Entity\ProductClass;
  20. use Eccube\Repository\CartRepository;
  21. use Eccube\Repository\OrderRepository;
  22. use Eccube\Repository\ProductClassRepository;
  23. use Eccube\Service\Cart\CartItemAllocator;
  24. use Eccube\Service\Cart\CartItemComparator;
  25. use Eccube\Util\StringUtil;
  26. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  27. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  28. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  29. class CartService
  30. {
  31.     /**
  32.      * @var Cart[]
  33.      */
  34.     protected $carts;
  35.     /**
  36.      * @var SessionInterface
  37.      */
  38.     protected $session;
  39.     /**
  40.      * @var \Doctrine\ORM\EntityManagerInterface
  41.      */
  42.     protected $entityManager;
  43.     /**
  44.      * @var ItemHolderInterface
  45.      *
  46.      * @deprecated
  47.      */
  48.     protected $cart;
  49.     /**
  50.      * @var ProductClassRepository
  51.      */
  52.     protected $productClassRepository;
  53.     /**
  54.      * @var CartRepository
  55.      */
  56.     protected $cartRepository;
  57.     /**
  58.      * @var CartItemComparator
  59.      */
  60.     protected $cartItemComparator;
  61.     /**
  62.      * @var CartItemAllocator
  63.      */
  64.     protected $cartItemAllocator;
  65.     /**
  66.      * @var OrderRepository
  67.      */
  68.     protected $orderRepository;
  69.     /**
  70.      * @var TokenStorageInterface
  71.      */
  72.     protected $tokenStorage;
  73.     /**
  74.      * @var AuthorizationCheckerInterface
  75.      */
  76.     protected $authorizationChecker;
  77.     /**
  78.      * CartService constructor.
  79.      */
  80.     public function __construct(
  81.         SessionInterface $session,
  82.         EntityManagerInterface $entityManager,
  83.         ProductClassRepository $productClassRepository,
  84.         CartRepository $cartRepository,
  85.         CartItemComparator $cartItemComparator,
  86.         CartItemAllocator $cartItemAllocator,
  87.         OrderRepository $orderRepository,
  88.         TokenStorageInterface $tokenStorage,
  89.         AuthorizationCheckerInterface $authorizationChecker
  90.     ) {
  91.         $this->session $session;
  92.         $this->entityManager $entityManager;
  93.         $this->productClassRepository $productClassRepository;
  94.         $this->cartRepository $cartRepository;
  95.         $this->cartItemComparator $cartItemComparator;
  96.         $this->cartItemAllocator $cartItemAllocator;
  97.         $this->orderRepository $orderRepository;
  98.         $this->tokenStorage $tokenStorage;
  99.         $this->authorizationChecker $authorizationChecker;
  100.     }
  101.     /**
  102.      * 現在のカートの配列を取得する.
  103.      *
  104.      * 本サービスのインスタンスのメンバーが空の場合は、DBまたはセッションからカートを取得する
  105.      *
  106.      * @param bool $empty_delete true の場合、商品明細が空のカートが存在した場合は削除する
  107.      *
  108.      * @return Cart[]
  109.      */
  110.     public function getCarts($empty_delete false)
  111.     {
  112.         if (null !== $this->carts) {
  113.             if ($empty_delete) {
  114.                 $cartKeys = [];
  115.                 foreach (array_keys($this->carts) as $index) {
  116.                     $Cart $this->carts[$index];
  117.                     if ($Cart->getItems()->count() > 0) {
  118.                         $cartKeys[] = $Cart->getCartKey();
  119.                     } else {
  120.                         $this->entityManager->remove($this->carts[$index]);
  121.                         $this->entityManager->flush();
  122.                         unset($this->carts[$index]);
  123.                     }
  124.                 }
  125.                 $this->session->set('cart_keys'$cartKeys);
  126.             }
  127.             return $this->carts;
  128.         }
  129.         if ($this->getUser()) {
  130.             $this->carts $this->getPersistedCarts();
  131.         } else {
  132.             $this->carts $this->getSessionCarts();
  133.         }
  134.         return $this->carts;
  135.     }
  136.     /**
  137.      * 永続化されたカートを返す
  138.      *
  139.      * @return Cart[]
  140.      */
  141.     public function getPersistedCarts()
  142.     {
  143.         return $this->cartRepository->findBy(['Customer' => $this->getUser()]);
  144.     }
  145.     /**
  146.      * セッションにあるカートを返す
  147.      *
  148.      * @return Cart[]
  149.      */
  150.     public function getSessionCarts()
  151.     {
  152.         $cartKeys $this->session->get('cart_keys', []);
  153.         if (empty($cartKeys)) {
  154.             return [];
  155.         }
  156.         return $this->cartRepository->findBy(['cart_key' => $cartKeys], ['id' => 'ASC']);
  157.     }
  158.     /**
  159.      * 会員が保持する永続化されたカートと、非会員時のカートをマージする.
  160.      */
  161.     public function mergeFromPersistedCart()
  162.     {
  163.         $persistedCarts $this->getPersistedCarts();
  164.         $sessionCarts $this->getSessionCarts();
  165.         $CartItems = [];
  166.         // 永続化されたカートとセッションのカートが同一の場合はマージしない #4574
  167.         $cartKeys $this->session->get('cart_keys', []);
  168.         if ((count($persistedCarts) > 0) && !in_array($persistedCarts[0]->getCartKey(), $cartKeystrue)) {
  169.             foreach ($persistedCarts as $Cart) {
  170.                 $CartItems $this->mergeCartItems($Cart->getCartItems(), $CartItems);
  171.             }
  172.         }
  173.         // セッションにある非会員カートとDBから取得した会員カートをマージする.
  174.         foreach ($sessionCarts as $Cart) {
  175.             $CartItems $this->mergeCartItems($Cart->getCartItems(), $CartItems);
  176.         }
  177.         $this->restoreCarts($CartItems);
  178.     }
  179.     /**
  180.      * @return Cart|null
  181.      */
  182.     public function getCart()
  183.     {
  184.         $Carts $this->getCarts();
  185.         if (empty($Carts)) {
  186.             return null;
  187.         }
  188.         $cartKeys $this->session->get('cart_keys', []);
  189.         $Cart null;
  190.         if (count($cartKeys) > 0) {
  191.             foreach ($Carts as $cart) {
  192.                 if ($cart->getCartKey() === current($cartKeys)) {
  193.                     $Cart $cart;
  194.                     break;
  195.                 }
  196.             }
  197.         } else {
  198.             $Cart $Carts[0];
  199.         }
  200.         return $Cart;
  201.     }
  202.     /**
  203.      * @param CartItem[] $cartItems
  204.      *
  205.      * @return CartItem[]
  206.      */
  207.     protected function mergeAllCartItems($cartItems = [])
  208.     {
  209.         /** @var CartItem[] $allCartItems */
  210.         $allCartItems = [];
  211.         foreach ($this->getCarts() as $Cart) {
  212.             $allCartItems $this->mergeCartItems($Cart->getCartItems(), $allCartItems);
  213.         }
  214.         return $this->mergeCartItems($cartItems$allCartItems);
  215.     }
  216.     /**
  217.      * @param $cartItems
  218.      * @param $allCartItems
  219.      *
  220.      * @return array
  221.      */
  222.     protected function mergeCartItems($cartItems$allCartItems)
  223.     {
  224.         foreach ($cartItems as $item) {
  225.             $itemExists false;
  226.             foreach ($allCartItems as $itemInArray) {
  227.                 // 同じ明細があればマージする
  228.                 if ($this->cartItemComparator->compare($item$itemInArray)) {
  229.                     $itemInArray->setQuantity($itemInArray->getQuantity() + $item->getQuantity());
  230.                     $itemExists true;
  231.                     break;
  232.                 }
  233.             }
  234.             if (!$itemExists) {
  235.                 $allCartItems[] = $item;
  236.             }
  237.         }
  238.         return $allCartItems;
  239.     }
  240.     protected function restoreCarts($cartItems)
  241.     {
  242.         foreach ($this->getCarts() as $Cart) {
  243.             foreach ($Cart->getCartItems() as $i) {
  244.                 $this->entityManager->remove($i);
  245.                 $this->entityManager->flush();
  246.             }
  247.             $this->entityManager->remove($Cart);
  248.             $this->entityManager->flush();
  249.         }
  250.         $this->carts = [];
  251.         /** @var Cart[] $Carts */
  252.         $Carts = [];
  253.         foreach ($cartItems as $item) {
  254.             $allocatedId $this->cartItemAllocator->allocate($item);
  255.             $cartKey $this->createCartKey($allocatedId$this->getUser());
  256.             if (isset($Carts[$cartKey])) {
  257.                 $Cart $Carts[$cartKey];
  258.                 $Cart->addCartItem($item);
  259.                 $item->setCart($Cart);
  260.             } else {
  261.                 /** @var Cart $Cart */
  262.                 $Cart $this->cartRepository->findOneBy(['cart_key' => $cartKey]);
  263.                 if ($Cart) {
  264.                     foreach ($Cart->getCartItems() as $i) {
  265.                         $this->entityManager->remove($i);
  266.                         $this->entityManager->flush();
  267.                     }
  268.                     $this->entityManager->remove($Cart);
  269.                     $this->entityManager->flush();
  270.                 }
  271.                 $Cart = new Cart();
  272.                 $Cart->setCartKey($cartKey);
  273.                 $Cart->addCartItem($item);
  274.                 $item->setCart($Cart);
  275.                 $Carts[$cartKey] = $Cart;
  276.             }
  277.         }
  278.         $this->carts array_values($Carts);
  279.     }
  280.     /**
  281.      * カートに商品を追加します.
  282.      *
  283.      * @param $ProductClass ProductClass 商品規格
  284.      * @param $quantity int 数量
  285.      *
  286.      * @return bool 商品を追加できた場合はtrue
  287.      */
  288.     public function addProduct($ProductClass$quantity 1)
  289.     {
  290.         if (!$ProductClass instanceof ProductClass) {
  291.             $ProductClassId $ProductClass;
  292.             $ProductClass $this->entityManager
  293.                 ->getRepository(ProductClass::class)
  294.                 ->find($ProductClassId);
  295.             if (is_null($ProductClass)) {
  296.                 return false;
  297.             }
  298.         }
  299.         $ClassCategory1 $ProductClass->getClassCategory1();
  300.         if ($ClassCategory1 && !$ClassCategory1->isVisible()) {
  301.             return false;
  302.         }
  303.         $ClassCategory2 $ProductClass->getClassCategory2();
  304.         if ($ClassCategory2 && !$ClassCategory2->isVisible()) {
  305.             return false;
  306.         }
  307.         // ProductClass から Product を取得
  308.         $Product $ProductClass->getProduct();
  309.          // カート内のすべてのアイテムを取得
  310.          foreach ($this->getCarts() as $Cart) {
  311.             foreach ($Cart->getCartItems() as $CartItem) {
  312.                 if ($CartItem->getProductClass()->getProduct()->getId() === $Product->getId()) {
  313.                     // 同じ商品がすでにカートにある場合
  314.                     $this->session->getFlashBag()->add('danger'
  315.                         $this->translator->trans('cart.add.duplicate'));
  316.                     return false;
  317.                 }
  318.             }
  319.         }
  320.         /*
  321.         foreach ($this->getCartItems() as $CartItem) {
  322.             if ($CartItem->getProductClass()->getProduct()->getId() === $Product->getId()) {
  323.                 // 既にカートに同じ商品がある場合は追加不可
  324.                 return false;
  325.             }
  326.         }
  327.         */
  328.         $newItem = new CartItem();
  329.         $newItem->setQuantity($quantity);
  330.         $newItem->setPrice($ProductClass->getPrice02IncTax());
  331.         $newItem->setProductClass($ProductClass);
  332.         $allCartItems $this->mergeAllCartItems([$newItem]);
  333.         $this->restoreCarts($allCartItems);
  334.         return true;
  335.     }
  336.     public function removeProduct($ProductClass)
  337.     {
  338.         if (!$ProductClass instanceof ProductClass) {
  339.             $ProductClassId $ProductClass;
  340.             $ProductClass $this->entityManager
  341.                 ->getRepository(ProductClass::class)
  342.                 ->find($ProductClassId);
  343.             if (is_null($ProductClass)) {
  344.                 return false;
  345.             }
  346.         }
  347.         $removeItem = new CartItem();
  348.         $removeItem->setPrice($ProductClass->getPrice02IncTax());
  349.         $removeItem->setProductClass($ProductClass);
  350.         $allCartItems $this->mergeAllCartItems();
  351.         $foundIndex = -1;
  352.         foreach ($allCartItems as $index => $itemInCart) {
  353.             if ($this->cartItemComparator->compare($itemInCart$removeItem)) {
  354.                 $foundIndex $index;
  355.                 break;
  356.             }
  357.         }
  358.         array_splice($allCartItems$foundIndex1);
  359.         $this->restoreCarts($allCartItems);
  360.         return true;
  361.     }
  362.     public function save()
  363.     {
  364.         $cartKeys = [];
  365.         foreach ($this->carts as $Cart) {
  366.             $Cart->setCustomer($this->getUser());
  367.             $this->entityManager->persist($Cart);
  368.             foreach ($Cart->getCartItems() as $item) {
  369.                 $this->entityManager->persist($item);
  370.             }
  371.             $this->entityManager->flush();
  372.             $cartKeys[] = $Cart->getCartKey();
  373.         }
  374.         $this->session->set('cart_keys'$cartKeys);
  375.         return;
  376.     }
  377.     /**
  378.      * @param  string $pre_order_id
  379.      *
  380.      * @return \Eccube\Service\CartService
  381.      */
  382.     public function setPreOrderId($pre_order_id)
  383.     {
  384.         $this->getCart()->setPreOrderId($pre_order_id);
  385.         return $this;
  386.     }
  387.     /**
  388.      * @return string|null
  389.      */
  390.     public function getPreOrderId()
  391.     {
  392.         $Cart $this->getCart();
  393.         if (!empty($Cart)) {
  394.             return $Cart->getPreOrderId();
  395.         }
  396.         return null;
  397.     }
  398.     /**
  399.      * @return \Eccube\Service\CartService
  400.      */
  401.     public function clear()
  402.     {
  403.         $Carts $this->getCarts();
  404.         if (!empty($Carts)) {
  405.             $removed $this->getCart();
  406.             if ($removed && UnitOfWork::STATE_MANAGED === $this->entityManager->getUnitOfWork()->getEntityState($removed)) {
  407.                 $this->entityManager->remove($removed);
  408.                 $this->entityManager->flush();
  409.                 $cartKeys = [];
  410.                 foreach ($Carts as $key => $Cart) {
  411.                     // テーブルから削除されたカートを除外する
  412.                     if ($Cart == $removed) {
  413.                         unset($Carts[$key]);
  414.                     }
  415.                     $cartKeys[] = $Cart->getCartKey();
  416.                 }
  417.                 $this->session->set('cart_keys'$cartKeys);
  418.                 // 注文完了のカートキーをセッションから削除する
  419.                 $this->session->remove('cart_key');
  420.                 $this->carts $this->cartRepository->findBy(['cart_key' => $cartKeys], ['id' => 'ASC']);
  421.             }
  422.         }
  423.         return $this;
  424.     }
  425.     /**
  426.      * @param CartItemComparator $cartItemComparator
  427.      */
  428.     public function setCartItemComparator($cartItemComparator)
  429.     {
  430.         $this->cartItemComparator $cartItemComparator;
  431.     }
  432.     /**
  433.      * カートキーで指定したインデックスにあるカートを優先にする
  434.      *
  435.      * @param string $cartKey カートキー
  436.      */
  437.     public function setPrimary($cartKey)
  438.     {
  439.         $Carts $this->getCarts();
  440.         $primary $Carts[0];
  441.         $index 0;
  442.         foreach ($Carts as $key => $Cart) {
  443.             if ($Cart->getCartKey() === $cartKey) {
  444.                 $index $key;
  445.                 $primary $Carts[$index];
  446.                 break;
  447.             }
  448.         }
  449.         $prev $Carts[0];
  450.         array_splice($Carts01, [$primary]);
  451.         array_splice($Carts$index1, [$prev]);
  452.         $this->carts $Carts;
  453.         $this->save();
  454.     }
  455.     protected function getUser()
  456.     {
  457.         if (null === $token $this->tokenStorage->getToken()) {
  458.             return;
  459.         }
  460.         if (!is_object($user $token->getUser())) {
  461.             // e.g. anonymous authentication
  462.             return;
  463.         }
  464.         return $user;
  465.     }
  466.     /**
  467.      * @param string $allocatedId
  468.      */
  469.     protected function createCartKey($allocatedIdCustomer $Customer null)
  470.     {
  471.         if ($Customer instanceof Customer) {
  472.             return $Customer->getId().'_'.$allocatedId;
  473.         }
  474.         if ($this->session->has('cart_key_prefix')) {
  475.             return $this->session->get('cart_key_prefix').'_'.$allocatedId;
  476.         }
  477.         do {
  478.             $random StringUtil::random(32);
  479.             $cartKey $random.'_'.$allocatedId;
  480.             $Cart $this->cartRepository->findOneBy(['cart_key' => $cartKey]);
  481.         } while ($Cart);
  482.         $this->session->set('cart_key_prefix'$random);
  483.         return $cartKey;
  484.     }
  485. }