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\ORM\EntityManagerInterface; |
17
|
|
|
use Eccube\Entity\ItemHolderInterface; |
18
|
|
|
use Eccube\Entity\Order; |
19
|
|
|
use Eccube\Service\PurchaseFlow\InvalidItemException; |
20
|
|
|
use Eccube\Service\PurchaseFlow\ItemHolderValidator; |
21
|
|
|
use Eccube\Service\PurchaseFlow\PurchaseContext; |
22
|
|
|
|
23
|
|
|
class EmptyItemsValidator extends ItemHolderValidator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EntityManagerInterface |
27
|
|
|
*/ |
28
|
|
|
protected $entityManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* EmptyItemsProcessor constructor. |
32
|
|
|
* |
33
|
|
|
* @param EntityManagerInterface $entityManager |
34
|
|
|
*/ |
35
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
36
|
|
|
{ |
37
|
|
|
$this->entityManager = $entityManager; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ItemHolderInterface $itemHolder |
42
|
|
|
* @param PurchaseContext $context |
43
|
|
|
* |
44
|
|
|
* @throws InvalidItemException |
45
|
|
|
*/ |
46
|
|
|
protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context) |
47
|
|
|
{ |
48
|
|
|
foreach ($itemHolder->getItems() as $item) { |
49
|
|
|
if ($item->isProduct() && $item->getQuantity() == 0) { |
50
|
|
|
if ($itemHolder instanceof Order) { |
51
|
|
|
foreach ($itemHolder->getShippings() as $Shipping) { |
52
|
|
|
$Shipping->removeOrderItem($item); |
53
|
|
|
} |
54
|
|
|
$itemHolder->removeOrderItem($item); |
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
$itemHolder->removeItem($item); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
$this->entityManager->remove($item); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$itemHolder instanceof Order) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// 受注の場合は, Shippingに紐づく商品明細がない場合はShippingも削除する. |
67
|
|
|
foreach ($itemHolder->getShippings() as $Shipping) { |
68
|
|
|
$hasProductItem = false; |
69
|
|
|
foreach ($Shipping->getOrderItems() as $item) { |
70
|
|
|
if ($item->isProduct()) { |
71
|
|
|
$hasProductItem = true; |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!$hasProductItem) { |
77
|
|
|
foreach ($Shipping->getOrderItems() as $item) { |
78
|
|
|
$this->entityManager->remove($item); |
79
|
|
|
} |
80
|
|
|
$itemHolder->removeShipping($Shipping); |
81
|
|
|
$this->entityManager->remove($Shipping); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Shippingがなくなれば購入エラー. |
86
|
|
|
if (count($itemHolder->getShippings()) < 1) { |
87
|
|
|
$this->throwInvalidItemException('front.shopping.empty_items_error'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.