Completed
Pull Request — 4.0 (#4262)
by
unknown
04:39
created

StockReduceProcessor::eachProductOrderItems()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
nc 13
nop 2
dl 0
loc 37
ccs 5
cts 5
cp 1
crap 7
rs 8.3946
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\PurchaseFlow\Processor;
15
16
use Doctrine\DBAL\LockMode;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Eccube\Entity\ItemHolderInterface;
19
use Eccube\Entity\Order;
20
use Eccube\Entity\ProductClass;
21
use Eccube\Entity\ProductStock;
22
use Eccube\Exception\ShoppingException;
23
use Eccube\Repository\ProductStockRepository;
24
use Eccube\Service\PurchaseFlow\PurchaseContext;
25
26
/**
27
 * 在庫制御.
28
 */
29
class StockReduceProcessor extends AbstractPurchaseProcessor
30
{
31
    /**
32
     * @var ProductStockRepository
33
     */
34
    protected $productStockRepository;
35
36
    /**
37
     * @var EntityManagerInterface
38 196
     */
39
    protected $entityManager;
40 196
41
    /**
42
     * StockReduceProcessor constructor.
43
     *
44
     * @param ProductStockRepository $productStockRepository
45
     * @param EntityManagerInterface $entityManager
46
     */
47
    public function __construct(ProductStockRepository $productStockRepository, EntityManagerInterface $entityManager)
48
    {
49 8
        $this->productStockRepository = $productStockRepository;
50 4
        $this->entityManager = $entityManager;
51 8
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context)
57
    {
58
        // 在庫を減らす
59
        $this->eachProductOrderItems($itemHolder, function ($currentStock, $itemQuantity) {
60 2
            return $currentStock - $itemQuantity;
61 2
        });
62 2
    }
63
64
    /**
65 10
     * {@inheritdoc}
66
     */
67
    public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context)
68 10
    {
69
        // 在庫を戻す
70
        $this->eachProductOrderItems($itemHolder, function ($currentStock, $itemQuantity) {
71
            return $currentStock + $itemQuantity;
72 10
        });
73
    }
74 10
75
    private function eachProductOrderItems(ItemHolderInterface $itemHolder, callable $callback)
76
    {
77
        // Order以外の場合は何もしない
78 6
        if (!$itemHolder instanceof Order) {
79 6
            return;
80
        }
81
82 6
        $productClasses = [];
83 6
84 10
        foreach ($itemHolder->getProductOrderItems() as $item) {
85
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
86
            if (!$item->getProductClass()->isStockUnlimited()) {
87
                // 在庫チェックあり
88
                /* @var ProductStock $productStock */
89
                $productStock = $item->getProductClass()->getProductStock();
90
                // 在庫に対してロックを実行
91
                $this->entityManager->lock($productStock, LockMode::PESSIMISTIC_WRITE);
92
                $this->entityManager->refresh($productStock);
93
                $ProductClass = $item->getProductClass();
94
                if (!array_key_exists($ProductClass->getId(), $productClasses)) {
95
                    $productClasses[$ProductClass->getId()] = 0;
96
                }
97
                $productClasses[$ProductClass->getId()] += (int) $item->getQuantity();
98
            }
99
        }
100
101
        foreach ($productClasses as $productClassId => $quantity) {
102
            $ProductClass = $this->entityManager->find(ProductClass::class, $productClassId);
103
            $productStock = $ProductClass->getProductStock();
104
            $stock = $callback($productStock->getStock(), $quantity);
105
            if ($stock < 0) {
106
                throw new ShoppingException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()]));
107
            }
108
            $productStock->setStock($stock);
109
            $ProductClass->setStock($stock);
110
        }
111
    }
112
}
113