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( |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.