app/Plugin/SheebDlc42/Service/PurchaseFlow/Processor/SameItemValidator.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Customize of EC-CUBE by Kenji Nakanishi
  4.  *
  5.  * Copyright(c) 2021 Kenji Nakanishi. All Rights Reserved.
  6.  *
  7.  * https://www.facebook.com/web.kenji.nakanishi
  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 Plugin\SheebDlc42\Service\PurchaseFlow\Processor;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Eccube\Annotation\CartFlow;
  15. use Eccube\Annotation\OrderFlow;
  16. use Eccube\Annotation\ShoppingFlow;
  17. use Eccube\Entity\CartItem;
  18. use Eccube\Entity\Customer;
  19. use Eccube\Entity\ItemInterface;
  20. use Eccube\Entity\Master\SaleType;
  21. use Eccube\Entity\Order;
  22. use Eccube\Repository\OrderRepository;
  23. use Eccube\Service\PurchaseFlow\InvalidItemException;
  24. use Eccube\Service\PurchaseFlow\ItemValidator;
  25. use Eccube\Service\PurchaseFlow\PurchaseContext;
  26. use Plugin\SheebDlc42\PluginManager;
  27. use Symfony\Component\DependencyInjection\ContainerInterface;
  28. /**
  29.  * 購入済みで且つ、ダウンロード可能な商品を購入できないようにする
  30.  *
  31.  * @CartFlow
  32.  * @ShoppingFlow
  33.  * @OrderFlow
  34.  */
  35. class SameItemValidator extends ItemValidator
  36. {
  37.     /**
  38.      * @var EntityManagerInterface
  39.      */
  40.     private $em;
  41.     /**
  42.      * @var SaleType
  43.      */
  44.     private $sheeb_dlc42SaleType;
  45.     /**
  46.      * @var Customer
  47.      */
  48.     private $loginCustomer;
  49.     /**
  50.      * @var int[]
  51.      */
  52.     private $downloadable_product_class_ids = [];
  53.     /**
  54.      * @throws \Exception
  55.      */
  56.     public function __construct(EntityManagerInterface $emContainerInterface $container)
  57.     {
  58.         $this->em $em;
  59.         $this->sheeb_dlc42SaleType PluginManager::getDlcSaleType($this->em);
  60.         // ログイン中のCustomerを取得
  61.         if (!$container->has('security.token_storage')) {
  62.             throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
  63.         }
  64.         if (null === $token $container->get('security.token_storage')->getToken()) {
  65.             $this->loginCustomer null;
  66.         } elseif (!\is_object($token->getUser())) {
  67.             $this->loginCustomer null;
  68.         } else {
  69.             $this->loginCustomer $token->getUser();
  70.         }
  71.         /*
  72.          * ログインユーザーの注文履歴の中で 現在もダウンロード可能な ProductClassIdを保存
  73.          * @var $orderRepository OrderRepository
  74.          * @var $Order Order
  75.          */
  76.         if ($this->loginCustomer instanceof Customer) {
  77.             $orderRepository $this->em->getRepository(Order::class);
  78.             $qb $orderRepository->getQueryBuilderByCustomer($this->loginCustomer);
  79.             $Orders $qb->getQuery()->getResult();
  80.             foreach ($Orders as $Order) {
  81.                 foreach ($Order->getOrderItems() as $OrderItem) {
  82.                     if ($OrderItem->isDownloadable($this->sheeb_dlc42SaleTypePluginManager::getConfig($this->em), true)) {
  83.                         $this->downloadable_product_class_ids[] = $OrderItem->getProductClass()->getId();
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.     /**
  90.      * @throws InvalidItemException
  91.      */
  92.     protected function validate(ItemInterface $itemPurchaseContext $context)
  93.     {
  94.         if (!$item->isProduct()) {
  95.             return;
  96.         }
  97.         // ゲスト購入を無効にしているので、最終的には必ずログインするため、ログインしていなければ無視
  98.         if (empty($this->loginCustomer)) {
  99.             return;
  100.         }
  101.         // SaleType がダウンロードコンテンツじゃなきゃ無視
  102.         $thisSaleType $item->getProductClass()->getSaleType();
  103.         if ($this->sheeb_dlc42SaleType->getId() !== $thisSaleType->getId()) {
  104.             return;
  105.         }
  106.         // この OrderItem が現在もダウンロード可能ならエラー
  107.         if (in_array($item->getProductClass()->getId(), $this->downloadable_product_class_ids)) {
  108.             $this->throwInvalidItemException('sheeb.dlc.purchase.purchased.error');
  109.         }
  110.     }
  111.     protected function handle(ItemInterface $itemPurchaseContext $context)
  112.     {
  113.         $item->setQuantity(0);
  114.     }
  115. }