|
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 Eccube\Entity\ItemHolderInterface; |
|
17
|
|
|
use Eccube\Repository\ProductClassRepository; |
|
18
|
|
|
use Eccube\Service\PurchaseFlow\ItemHolderValidator; |
|
19
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseContext; |
|
20
|
|
|
|
|
21
|
|
|
class StockMultipleValidator extends ItemHolderValidator |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var ProductClassRepository |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $productClassRepository; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* StockProcessor constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param ProductClassRepository $productClassRepository |
|
32
|
|
|
*/ |
|
33
|
64 |
|
public function __construct(ProductClassRepository $productClassRepository) |
|
34
|
|
|
{ |
|
35
|
64 |
|
$this->productClassRepository = $productClassRepository; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param ItemHolderInterface $itemHolder |
|
40
|
|
|
* @param PurchaseContext $context |
|
41
|
|
|
* |
|
42
|
|
|
* @throws \Eccube\Service\PurchaseFlow\InvalidItemException |
|
43
|
|
|
*/ |
|
44
|
55 |
|
public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
45
|
|
|
{ |
|
46
|
55 |
|
$OrderItemsByProductClass = []; |
|
47
|
55 |
View Code Duplication |
foreach ($itemHolder->getItems() as $Item) { |
|
48
|
55 |
|
if ($Item->isProduct()) { |
|
49
|
55 |
|
$id = $Item->getProductClass()->getId(); |
|
50
|
55 |
|
$OrderItemsByProductClass[$id][] = $Item; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
55 |
|
foreach ($OrderItemsByProductClass as $id => $Items) { |
|
55
|
55 |
|
$ProductClass = $this->productClassRepository->find($id); |
|
56
|
55 |
|
if ($ProductClass->isStockUnlimited()) { |
|
57
|
24 |
|
continue; |
|
58
|
|
|
} |
|
59
|
34 |
|
$stock = $ProductClass->getStock(); |
|
60
|
34 |
|
if ($stock == 0) { |
|
61
|
3 |
|
$this->throwInvalidItemException('cart.zero.stock', $ProductClass); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
32 |
|
$total = 0; |
|
64
|
32 |
|
foreach ($Items as $Item) { |
|
65
|
32 |
|
$total += $Item->getQuantity(); |
|
66
|
32 |
|
if ($stock < $total) { |
|
67
|
32 |
|
$this->throwInvalidItemException('cart.over.stock', $ProductClass); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.