Failed Conditions
Pull Request — 4.0 (#3688)
by k-yamamura
06:04
created

StockReduceProcessor::prepare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.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 Eccube\Entity\ItemHolderInterface;
18
use Eccube\Entity\Order;
19
use Eccube\Entity\ProductStock;
20
use Eccube\Repository\ProductStockRepository;
21
use Eccube\Service\PurchaseFlow\PurchaseContext;
22
23
/**
24
 * 在庫制御.
25
 */
26
class StockReduceProcessor extends AbstractPurchaseProcessor
27
{
28
    /**
29
     * @var ProductStockRepository
30
     */
31
    protected $productStockRepository;
32
33
    /**
34
     * StockReduceProcessor constructor.
35
     *
36
     * @param ProductStockRepository $productStockRepository
37
     */
38 196
    public function __construct(ProductStockRepository $productStockRepository)
39
    {
40 196
        $this->productStockRepository = $productStockRepository;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context)
47
    {
48
        // 在庫を減らす
49 8
        $this->eachProductOrderItems($itemHolder, function ($currentStock, $itemQuantity) {
50 4
            return $currentStock - $itemQuantity;
51 8
        });
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context)
58
    {
59
        // 在庫を戻す
60 2
        $this->eachProductOrderItems($itemHolder, function ($currentStock, $itemQuantity) {
61 2
            return $currentStock + $itemQuantity;
62 2
        });
63
    }
64
65 10
    private function eachProductOrderItems(ItemHolderInterface $itemHolder, callable $callback)
66
    {
67
        // Order以外の場合は何もしない
68 10
        if (!$itemHolder instanceof Order) {
69
            return;
70
        }
71
72 10
        foreach ($itemHolder->getProductOrderItems() as $item) {
73
            // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
74 10
            if (!$item->getProductClass()->isStockUnlimited()) {
75
                // 在庫チェックあり
76
                // 在庫に対してロック(select ... for update)を実行
77
                /* @var ProductStock $productStock */
78 6
                $productStock = $this->productStockRepository->find(
0 ignored issues
show
Unused Code introduced by
$productStock is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
79 6
                    $item->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
80
                );
81
            }
82 6
        }
83 6
84 10
        foreach ($itemHolder->getProductOrderItems() as $item) {
85
            // 在庫が無制限かチェックし、制限ありなら在庫を減らす
86
            if (!$item->getProductClass()->isStockUnlimited()) {
87
                $productStock = $item->getProductClass()->getProductStock();
88
                $stock = $callback($productStock->getStock(), $item->getQuantity());
89
                $productStock->setStock($stock);
90
                $item->getProductClass()->setStock($stock);
91
            }
92
        }
93
    }
94
}
95