These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\ORM\EntityManagerInterface; |
||
17 | use Eccube\Entity\ItemHolderInterface; |
||
18 | use Eccube\Entity\Order; |
||
19 | use Eccube\Service\PointHelper; |
||
20 | use Eccube\Service\PurchaseFlow\DiscountProcessor; |
||
21 | use Eccube\Service\PurchaseFlow\ProcessResult; |
||
22 | use Eccube\Service\PurchaseFlow\PurchaseContext; |
||
23 | use Eccube\Service\PurchaseFlow\PurchaseProcessor; |
||
24 | |||
25 | /** |
||
26 | * 購入フローにおけるポイント処理. |
||
27 | */ |
||
28 | class PointProcessor implements DiscountProcessor, PurchaseProcessor |
||
29 | { |
||
30 | /** |
||
31 | * @var EntityManagerInterface |
||
32 | */ |
||
33 | protected $entityManager; |
||
34 | |||
35 | /** |
||
36 | * @var PointHelper |
||
37 | */ |
||
38 | protected $pointHelper; |
||
39 | |||
40 | /** |
||
41 | * PointProcessor constructor. |
||
42 | * |
||
43 | * @param EntityManagerInterface $entityManager |
||
44 | * @param PointHelper $pointHelper |
||
45 | */ |
||
46 | public function __construct(EntityManagerInterface $entityManager, PointHelper $pointHelper) |
||
47 | { |
||
48 | $this->entityManager = $entityManager; |
||
49 | $this->pointHelper = $pointHelper; |
||
50 | } |
||
51 | |||
52 | 790 | /* |
|
53 | * DiscountProcessors |
||
54 | 790 | */ |
|
55 | 790 | ||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | public function removeDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
60 | { |
||
61 | if (!$this->supports($itemHolder)) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | 232 | $this->pointHelper->removePointDiscountItem($itemHolder); |
|
66 | } |
||
67 | 232 | ||
68 | 17 | /** |
|
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | 215 | public function addDiscountItem(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
72 | { |
||
73 | if (!$this->supports($itemHolder)) { |
||
74 | 215 | return; |
|
75 | 17 | } |
|
76 | 17 | ||
77 | $usePoint = $itemHolder->getUsePoint(); |
||
78 | $discount = $this->pointHelper->pointToDiscount($usePoint); |
||
79 | |||
80 | 215 | // 利用ポイントがある場合は割引明細を追加 |
|
81 | 215 | if ($usePoint > 0) { |
|
82 | $result = null; |
||
83 | |||
84 | // 購入フロー実行時 |
||
85 | if ($context->isShoppingFlow()) { |
||
86 | // 支払い金額 < 利用ポイントによる値引き額. |
||
87 | if ($itemHolder->getTotal() + $discount < 0) { |
||
88 | $minus = $itemHolder->getTotal() + $discount; |
||
89 | // 利用ポイントが支払い金額を上回っていた場合は支払い金額が0円以上となるようにポイントを調整 |
||
90 | $overPoint = $this->pointHelper->priceToPoint($minus); |
||
91 | 61 | $usePoint = $itemHolder->getUsePoint() + $overPoint; |
|
92 | $discount = $this->pointHelper->pointToDiscount($usePoint); |
||
93 | 61 | $result = ProcessResult::warn(trans('purchase_flow.over_payment_total'), self::class); |
|
94 | 17 | } |
|
95 | |||
96 | // 所有ポイント < 利用ポイント |
||
97 | $Customer = $itemHolder->getCustomer(); |
||
98 | 44 | if ($Customer->getPoint() < $usePoint) { |
|
99 | 44 | // 利用ポイントが所有ポイントを上回っていた場合は所有ポイントで上書き |
|
100 | $usePoint = $Customer->getPoint(); |
||
101 | 2 | $discount = $this->pointHelper->pointToDiscount($usePoint); |
|
102 | 2 | $result = ProcessResult::warn(trans('purchase_flow.over_customer_point'), self::class); |
|
103 | } |
||
104 | // 受注登録・編集実行時 |
||
105 | } else { |
||
106 | 42 | // 支払い金額 < 利用ポイントによる値引き額. |
|
107 | if ($itemHolder->getTotal() >= 0 && $itemHolder->getTotal() + $discount < 0) { |
||
108 | 1 | $result = ProcessResult::error(trans('purchase_flow.over_payment_total'), self::class); |
|
109 | 1 | } |
|
110 | 1 | } |
|
111 | |||
112 | $itemHolder->setUsePoint($usePoint); |
||
113 | $this->pointHelper->addPointDiscountItem($itemHolder, $discount); |
||
114 | |||
115 | if ($result) { |
||
116 | return $result; |
||
117 | } |
||
118 | } |
||
119 | } |
||
120 | |||
121 | 12 | /* |
|
122 | * PurchaseProcessor |
||
123 | 12 | */ |
|
124 | 3 | ||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 9 | public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context) |
|
129 | 9 | { |
|
130 | if (!$this->supports($itemHolder)) { |
||
131 | return; |
||
132 | } |
||
133 | |||
134 | // ユーザの保有ポイントを減算 |
||
135 | $this->pointHelper->prepare($itemHolder, $itemHolder->getUsePoint()); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * {@inheritdoc |
||
140 | */ |
||
141 | public function commit(ItemHolderInterface $target, PurchaseContext $context) |
||
142 | { |
||
143 | 2 | // 何もしない |
|
144 | } |
||
145 | |||
146 | 2 | /** |
|
147 | * {@inheritdoc |
||
148 | */ |
||
149 | public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context) |
||
150 | 2 | { |
|
151 | 2 | // 利用したポイントをユーザに戻す. |
|
152 | if (!$this->supports($itemHolder)) { |
||
153 | return; |
||
154 | } |
||
155 | |||
156 | $this->pointHelper->rollback($itemHolder, $itemHolder->getUsePoint()); |
||
157 | } |
||
158 | |||
159 | /* |
||
160 | * Helper methods |
||
161 | */ |
||
162 | |||
163 | /** |
||
164 | * Processorが実行出来るかどうかを返す. |
||
165 | * |
||
166 | * 以下を満たす場合に実行できる. |
||
167 | * |
||
168 | * - ポイント設定が有効であること. |
||
169 | * - $itemHolderがOrderエンティティであること. |
||
170 | * - 会員のOrderであること. |
||
171 | 239 | * |
|
172 | * @param ItemHolderInterface $itemHolder |
||
173 | 239 | * |
|
174 | * @return bool |
||
175 | */ |
||
176 | View Code Duplication | private function supports(ItemHolderInterface $itemHolder) |
|
0 ignored issues
–
show
|
|||
177 | 239 | { |
|
178 | if (!$this->pointHelper->isPointEnabled()) { |
||
179 | return false; |
||
180 | } |
||
181 | 239 | ||
182 | 17 | if (!$itemHolder instanceof Order) { |
|
183 | return false; |
||
184 | } |
||
185 | 222 | ||
186 | if (!$itemHolder->getCustomer()) { |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | return true; |
||
191 | } |
||
192 | } |
||
193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.