Completed
Push — 4.0 ( 6759a0...9e24a2 )
by chihiro
04:51 queued 11s
created

StockReduceProcessor::eachProductOrderItems()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 7
nop 2
dl 0
loc 29
ccs 6
cts 6
cp 1
crap 6
rs 8.8337
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\ProductStock;
21
use Eccube\Exception\ShoppingException;
22
use Eccube\Repository\ProductStockRepository;
23
use Eccube\Service\PurchaseFlow\PurchaseContext;
24
25
/**
26
 * 在庫制御.
27
 */
28
class StockReduceProcessor extends AbstractPurchaseProcessor
29
{
30
    /**
31
     * @var ProductStockRepository
32
     */
33
    protected $productStockRepository;
34
35
    /**
36
     * @var EntityManagerInterface
37
     */
38 196
    protected $entityManager;
39
40 196
    /**
41
     * StockReduceProcessor constructor.
42
     *
43
     * @param ProductStockRepository $productStockRepository
44
     * @param EntityManagerInterface $entityManager
45
     */
46
    public function __construct(ProductStockRepository $productStockRepository, EntityManagerInterface $entityManager)
47
    {
48
        $this->productStockRepository = $productStockRepository;
49 8
        $this->entityManager = $entityManager;
50 4
    }
51 8
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context)
56
    {
57
        // 在庫を減らす
58
        $this->eachProductOrderItems($itemHolder, function ($currentStock, $itemQuantity) {
59
            return $currentStock - $itemQuantity;
60 2
        });
61 2
    }
62 2
63
    /**
64
     * {@inheritdoc}
65 10
     */
66
    public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context)
67
    {
68 10
        // 在庫を戻す
69
        $this->eachProductOrderItems($itemHolder, function ($currentStock, $itemQuantity) {
70
            return $currentStock + $itemQuantity;
71
        });
72 10
    }
73
74 10
    private function eachProductOrderItems(ItemHolderInterface $itemHolder, callable $callback)
75
    {
76
        // Order以外の場合は何もしない
77
        if (!$itemHolder instanceof Order) {
78 6
            return;
79 6
        }
80
81
        foreach ($itemHolder->getProductOrderItems() as $item) {
82 6
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
83 6
            if (!$item->getProductClass()->isStockUnlimited()) {
84 10
                // 在庫チェックあり
85
                /* @var ProductStock $productStock */
86
                $productStock = $item->getProductClass()->getProductStock();
87
                if ($productStock->getProductClassId() === null) {
88
                    // 在庫に対してロックを実行
89
                    $this->entityManager->lock($productStock, LockMode::PESSIMISTIC_WRITE);
90
                    $this->entityManager->refresh($productStock);
91
                    $productStock->setProductClassId($item->getProductClass()->getId());
92
                }
93
                $ProductClass = $item->getProductClass();
94
                $stock = $callback($productStock->getStock(), $item->getQuantity());
95
                if ($stock < 0) {
96
                    throw new ShoppingException(trans('purchase_flow.over_stock', ['%name%' => $ProductClass->formattedProductName()]));
97
                }
98
                $productStock->setStock($stock);
99
                $ProductClass->setStock($stock);
100
            }
101
        }
102
    }
103
}
104