Completed
Pull Request — experimental/sf (#3420)
by chihiro
244:13 queued 235:15
created

PurchaseFlow::setItemHolderPreprocessors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
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;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Entity\ItemInterface;
19
use Eccube\Entity\Order;
20
use Eccube\Entity\OrderItem;
21
22
class PurchaseFlow
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
23
{
24
    /**
25
     * @var ArrayCollection|ItemPreprocessor[]
26
     */
27
    protected $itemPreprocessors;
28
29
    /**
30
     * @var ArrayCollection|ItemHolderPreprocessor[]
31
     */
32
    protected $itemHolderPreprocessors;
33
34
    /**
35
     * @var ArrayCollection|ItemValidator[]
36
     */
37
    protected $itemValidators;
38
39
    /**
40
     * @var ArrayCollection|ItemHolderValidator[]
41
     */
42
    protected $itemHolderValidators;
43
44
    /**
45
     * @var ArrayCollection|PurchaseProcessor[]
46
     */
47
    protected $purchaseProcessors;
48
49 725
    public function __construct()
50
    {
51 725
        $this->purchaseProcessors = new ArrayCollection();
52 725
        $this->itemValidators = new ArrayCollection();
53 725
        $this->itemHolderValidators = new ArrayCollection();
54 725
        $this->itemPreprocessors = new ArrayCollection();
55 725
        $this->itemHolderPreprocessors = new ArrayCollection();
56
    }
57
58 686
    public function setPurchaseProcessors(ArrayCollection $processors)
59
    {
60 686
        $this->purchaseProcessors = $processors;
61
    }
62
63 719
    public function setItemValidators(ArrayCollection $itemValidators)
64
    {
65 719
        $this->itemValidators = $itemValidators;
66
    }
67
68 719
    public function setItemHolderValidators(ArrayCollection $itemHolderValidators)
69
    {
70 719
        $this->itemHolderValidators = $itemHolderValidators;
71
    }
72
73
    public function setItemPreprocessors(ArrayCollection $itemPreprocessors)
74
    {
75
        $this->itemPreprocessors = $itemPreprocessors;
76
    }
77
78 719
    public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors)
79
    {
80 719
        $this->itemHolderPreprocessors = $itemHolderPreprocessors;
81
    }
82
83 253
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
84
    {
85 253
        $this->calculateAll($itemHolder);
86
87 253
        $flowResult = new PurchaseFlowResult($itemHolder);
88
89 253
        foreach ($itemHolder->getItems() as $item) {
90 249
            foreach ($this->itemPreprocessors as $itemPreprocessor) {
91 249
                $itemPreprocessor->process($item, $context);
92
            }
93
        }
94
95 253
        $this->calculateAll($itemHolder);
96
97 253
        foreach ($this->itemHolderPreprocessors as $holderPreprocessor) {
98 237
            $holderPreprocessor->process($itemHolder, $context);
99
        }
100
101 253
        $this->calculateAll($itemHolder);
102
103 253
        foreach ($itemHolder->getItems() as $item) {
104 249
            foreach ($this->itemValidators as $itemValidator) {
105 81
                $result = $itemValidator->execute($item, $context);
106 249
                $flowResult->addProcessResult($result);
107
            }
108
        }
109
110 253
        $this->calculateAll($itemHolder);
111
112 253
        foreach ($this->itemHolderValidators as $itemHolderValidator) {
113 247
            $result = $itemHolderValidator->execute($itemHolder, $context);
114 247
            $flowResult->addProcessResult($result);
115
        }
116
117 253
        $this->calculateAll($itemHolder);
118
119 253
        return $flowResult;
120
    }
121
122
    /**
123
     * 購入フロー仮確定処理.
124
     *
125
     * @param ItemHolderInterface $target
126
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
127
     *
128
     * @throws PurchaseException
129
     */
130 20
    public function prepare(ItemHolderInterface $target, PurchaseContext $context)
131
    {
132 20
        foreach ($this->purchaseProcessors as $processor) {
133 13
            $processor->prepare($target, $context);
134
        }
135
    }
136
137
    /**
138
     * 購入フロー確定処理.
139
     *
140
     * @param ItemHolderInterface $target
141
     * @param PurchaseContext     $context
142
     *
143
     * @throws PurchaseException
144
     */
145 12
    public function commit(ItemHolderInterface $target, PurchaseContext $context)
146
    {
147 12
        foreach ($this->purchaseProcessors as $processor) {
148 12
            $processor->commit($target, $context);
149
        }
150
    }
151
152
    /**
153
     * 購入フロー仮確定取り消し処理.
154
     *
155
     * @param ItemHolderInterface $target
156
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
157
     */
158
    public function rollback(ItemHolderInterface $target, PurchaseContext $context)
159
    {
160
        foreach ($this->purchaseProcessors as $processor) {
161
            $processor->rollback($target, $context);
162
        }
163
    }
164
165 2
    public function addPurchaseProcessor(PurchaseProcessor $processor)
166
    {
167 2
        $this->purchaseProcessors[] = $processor;
168
    }
169
170 2
    public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor)
171
    {
172 2
        $this->itemHolderPreprocessors[] = $holderPreprocessor;
173
    }
174
175 2
    public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor)
176
    {
177 2
        $this->itemPreprocessors[] = $itemPreprocessor;
178
    }
179
180 3
    public function addItemValidator(ItemValidator $itemValidator)
181
    {
182 3
        $this->itemValidators[] = $itemValidator;
183
    }
184
185 15
    public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator)
186
    {
187 15
        $this->itemHolderValidators[] = $itemHolderValidator;
188
    }
189
190
    /**
191
     * @param ItemHolderInterface $itemHolder
192
     */
193
    protected function calculateTotal(ItemHolderInterface $itemHolder)
194
    {
195 253
        $total = $itemHolder->getItems()->reduce(function ($sum, ItemInterface $item) {
196 249
            $sum += $item->getPriceIncTax() * $item->getQuantity();
197
198 249
            return $sum;
199 253
        }, 0);
200 253
        $itemHolder->setTotal($total);
201
        // TODO
202 253
        if ($itemHolder instanceof Order) {
203
            // Order には PaymentTotal もセットする
204 222
            $itemHolder->setPaymentTotal($total);
205
        }
206
    }
207
208 253 View Code Duplication
    protected function calculateSubTotal(ItemHolderInterface $itemHolder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
209
    {
210 253
        $total = $itemHolder->getItems()
211 253
            ->getProductClasses()
212 253
            ->reduce(function ($sum, ItemInterface $item) {
213 236
                $sum += $item->getPriceIncTax() * $item->getQuantity();
214
215 236
                return $sum;
216 253
            }, 0);
217
        // TODO
218 253
        if ($itemHolder instanceof Order) {
219
            // Order の場合は SubTotal をセットする
220 222
            $itemHolder->setSubTotal($total);
221
        }
222
    }
223
224
    /**
225
     * @param ItemHolderInterface $itemHolder
226
     */
227 253 View Code Duplication
    protected function calculateDeliveryFeeTotal(ItemHolderInterface $itemHolder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
228
    {
229 253
        $total = $itemHolder->getItems()
230 253
            ->getDeliveryFees()
231 253
            ->reduce(function ($sum, ItemInterface $item) {
232 207
                $sum += $item->getPriceIncTax() * $item->getQuantity();
233
234 207
                return $sum;
235 253
            }, 0);
236 253
        $itemHolder->setDeliveryFeeTotal($total);
237
    }
238
239
    /**
240
     * @param ItemHolderInterface $itemHolder
241
     */
242 253 View Code Duplication
    protected function calculateDiscount(ItemHolderInterface $itemHolder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
243
    {
244 253
        $total = $itemHolder->getItems()
245 253
            ->getDiscounts()
246 253
            ->reduce(function ($sum, ItemInterface $item) {
247 156
                $sum += $item->getPriceIncTax() * $item->getQuantity();
248
249 156
                return $sum;
250 253
            }, 0);
251
        // TODO 後方互換のため discount には正の整数を代入する
252 253
        $itemHolder->setDiscount($total * -1);
253
    }
254
255
    /**
256
     * @param ItemHolderInterface $itemHolder
257
     */
258 253 View Code Duplication
    protected function calculateCharge(ItemHolderInterface $itemHolder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
259
    {
260 253
        $total = $itemHolder->getItems()
261 253
            ->getCharges()
262 253
            ->reduce(function ($sum, ItemInterface $item) {
263 156
                $sum += $item->getPriceIncTax() * $item->getQuantity();
264
265 156
                return $sum;
266 253
            }, 0);
267 253
        $itemHolder->setCharge($total);
268
    }
269
270
    /**
271
     * @param ItemHolderInterface $itemHolder
272
     */
273 253
    protected function calculateTax(ItemHolderInterface $itemHolder)
274
    {
275 253
        $total = $itemHolder->getItems()
276 253
            ->reduce(function ($sum, ItemInterface $item) {
277 249
                if ($item instanceof OrderItem) {
278 222
                    $sum += $item->getTax() * $item->getQuantity();
279
                } else {
280 79
                    $sum += ($item->getPriceIncTax() - $item->getPrice()) * $item->getQuantity();
281
                }
282
283 249
                return $sum;
284 253
            }, 0);
285 253
        $itemHolder->setTax($total);
286
    }
287
288
    /**
289
     * @param ItemHolderInterface $itemHolder
290
     */
291 253
    protected function calculateAll(ItemHolderInterface $itemHolder)
292
    {
293 253
        $this->calculateDeliveryFeeTotal($itemHolder);
294 253
        $this->calculateCharge($itemHolder);
295 253
        $this->calculateDiscount($itemHolder);
296 253
        $this->calculateSubTotal($itemHolder); // Order の場合のみ
297 253
        $this->calculateTax($itemHolder);
298 253
        $this->calculateTotal($itemHolder);
299
    }
300
}
301