Failed Conditions
Pull Request — experimental/sf (#3247)
by Kiyotaka
114:07 queued 103:28
created

PurchaseFlow::prepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 2
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
21
class PurchaseFlow
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
22
{
23
    /**
24
     * @var ArrayCollection|ItemPreprocessor[]
25
     */
26
    protected $itemPreprocessors;
27
28
    /**
29
     * @var ArrayCollection|ItemHolderPreprocessor[]
30
     */
31
    protected $itemHolderPreprocessors;
32
33
    /**
34
     * @var ArrayCollection|ItemValidator[]
35
     */
36
    protected $itemValidators;
37
38
    /**
39
     * @var ArrayCollection|ItemHolderValidator[]
40
     */
41
    protected $itemHolderValidators;
42
43
    /**
44
     * @var ArrayCollection|PurchaseProcessor[]
45
     */
46
    protected $purchaseProcessors;
47
48 691
    public function __construct()
49
    {
50 691
        $this->purchaseProcessors = new ArrayCollection();
51 691
        $this->itemValidators = new ArrayCollection();
52 691
        $this->itemHolderValidators = new ArrayCollection();
53 691
        $this->itemPreprocessors = new ArrayCollection();
54 691
        $this->itemHolderPreprocessors = new ArrayCollection();
55
    }
56
57 652
    public function setPurchaseProcessors(ArrayCollection $processors)
58
    {
59 652
        $this->purchaseProcessors = $processors;
60
    }
61
62 685
    public function setItemValidators(ArrayCollection $itemValidators)
63
    {
64 685
        $this->itemValidators = $itemValidators;
65
    }
66
67 685
    public function setItemHolderValidators(ArrayCollection $itemHolderValidators)
68
    {
69 685
        $this->itemHolderValidators = $itemHolderValidators;
70
    }
71
72
    public function setItemPreprocessors(ArrayCollection $itemPreprocessors)
73
    {
74
        $this->itemPreprocessors = $itemPreprocessors;
75
    }
76
77 685
    public function setItemHolderPreprocessors(ArrayCollection $itemHolderPreprocessors)
78
    {
79 685
        $this->itemHolderPreprocessors = $itemHolderPreprocessors;
80
    }
81
82 228
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
83
    {
84 228
        $this->calculateAll($itemHolder);
85
86 228
        $flowResult = new PurchaseFlowResult($itemHolder);
87
88 228
        foreach ($itemHolder->getItems() as $item) {
89 224
            foreach ($this->itemPreprocessors as $itemPreprocessor) {
90 224
                $itemPreprocessor->process($item, $context);
91
            }
92
        }
93
94 228
        $this->calculateAll($itemHolder);
95
96 228
        foreach ($this->itemHolderPreprocessors as $holderPreprocessor) {
97 212
            $holderPreprocessor->process($itemHolder, $context);
98
        }
99
100 228
        $this->calculateAll($itemHolder);
101
102 228
        foreach ($itemHolder->getItems() as $item) {
103 224
            foreach ($this->itemValidators as $itemValidator) {
104 213
                $result = $itemValidator->execute($item, $context);
105 224
                $flowResult->addProcessResult($result);
106
            }
107
        }
108
109 228
        $this->calculateAll($itemHolder);
110
111 228
        foreach ($this->itemHolderValidators as $itemHolderValidator) {
112 222
            $result = $itemHolderValidator->execute($itemHolder, $context);
113 222
            $flowResult->addProcessResult($result);
114
        }
115
116 228
        $this->calculateAll($itemHolder);
117
118 228
        return $flowResult;
119
    }
120
121
    /**
122
     * 購入フロー仮確定処理.
123
     *
124
     * @param ItemHolderInterface $target
125
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
126
     *
127
     * @throws PurchaseException
128
     */
129 12
    public function prepare(ItemHolderInterface $target, PurchaseContext $context)
130
    {
131 12
        foreach ($this->purchaseProcessors as $processor) {
132 12
            $processor->prepare($target, $context);
133
        }
134
    }
135
136
    /**
137
     * 購入フロー確定処理.
138
     *
139
     * @param ItemHolderInterface $target
140
     * @param PurchaseContext     $context
141
     *
142
     * @throws PurchaseException
143
     */
144 14
    public function commit(ItemHolderInterface $target, PurchaseContext $context)
145
    {
146 14
        foreach ($this->purchaseProcessors as $processor) {
147 14
            $processor->commit($target, $context);
148
        }
149
    }
150
151
    /**
152
     * 購入フロー仮確定取り消し処理.
153
     *
154
     * @param ItemHolderInterface $target
155
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
156
     */
157
    public function rollback(ItemHolderInterface $target, PurchaseContext $context)
158
    {
159
        foreach ($this->purchaseProcessors as $processor) {
160
            $processor->rollback($target, $context);
161
        }
162
    }
163
164 1
    public function addPurchaseProcessor(PurchaseProcessor $processor)
165
    {
166 1
        $this->purchaseProcessors[] = $processor;
167
    }
168
169 1
    public function addItemHolderPreprocessor(ItemHolderPreprocessor $holderPreprocessor)
170
    {
171 1
        $this->itemHolderPreprocessors[] = $holderPreprocessor;
172
    }
173
174 1
    public function addItemPreprocessor(ItemPreprocessor $itemPreprocessor)
175
    {
176 1
        $this->itemPreprocessors[] = $itemPreprocessor;
177
    }
178
179 2
    public function addItemValidator(ItemValidator $itemValidator)
180
    {
181 2
        $this->itemValidators[] = $itemValidator;
182
    }
183
184 11
    public function addItemHolderValidator(ItemHolderValidator $itemHolderValidator)
185
    {
186 11
        $this->itemHolderValidators[] = $itemHolderValidator;
187
    }
188
189
    /**
190
     * @param ItemHolderInterface $itemHolder
191
     */
192
    protected function calculateTotal(ItemHolderInterface $itemHolder)
193
    {
194 228
        $total = $itemHolder->getItems()->reduce(function ($sum, ItemInterface $item) {
195 224
            $sum += $item->getPriceIncTax() * $item->getQuantity();
196
197 224
            return $sum;
198 228
        }, 0);
199 228
        $itemHolder->setTotal($total);
200
        // TODO
201 228
        if ($itemHolder instanceof Order) {
202
            // Order には PaymentTotal もセットする
203 197
            $itemHolder->setPaymentTotal($total);
204
        }
205
    }
206
207 228 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...
208
    {
209 228
        $total = $itemHolder->getItems()
210 228
            ->getProductClasses()
211 228
            ->reduce(function ($sum, ItemInterface $item) {
212 209
                $sum += $item->getPriceIncTax() * $item->getQuantity();
213
214 209
                return $sum;
215 228
            }, 0);
216
        // TODO
217 228
        if ($itemHolder instanceof Order) {
218
            // Order の場合は SubTotal をセットする
219 197
            $itemHolder->setSubTotal($total);
220
        }
221
    }
222
223
    /**
224
     * @param ItemHolderInterface $itemHolder
225
     */
226 228 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...
227
    {
228 228
        $total = $itemHolder->getItems()
229 228
            ->getDeliveryFees()
230 228
            ->reduce(function ($sum, ItemInterface $item) {
231 182
                $sum += $item->getPriceIncTax() * $item->getQuantity();
232
233 182
                return $sum;
234 228
            }, 0);
235 228
        $itemHolder->setDeliveryFeeTotal($total);
236
    }
237
238
    /**
239
     * @param ItemHolderInterface $itemHolder
240
     */
241 228 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...
242
    {
243 228
        $total = $itemHolder->getItems()
244 228
            ->getDiscounts()
245 228
            ->reduce(function ($sum, ItemInterface $item) {
246 131
                $sum += $item->getPriceIncTax() * $item->getQuantity();
247
248 131
                return $sum;
249 228
            }, 0);
250
        // TODO 後方互換のため discount には正の整数を代入する
251 228
        $itemHolder->setDiscount($total * -1);
252
    }
253
254
    /**
255
     * @param ItemHolderInterface $itemHolder
256
     */
257 228 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...
258
    {
259 228
        $total = $itemHolder->getItems()
260 228
            ->getCharges()
261 228
            ->reduce(function ($sum, ItemInterface $item) {
262 131
                $sum += $item->getPriceIncTax() * $item->getQuantity();
263
264 131
                return $sum;
265 228
            }, 0);
266 228
        $itemHolder->setCharge($total);
267
    }
268
269
    /**
270
     * @param ItemHolderInterface $itemHolder
271
     */
272 228 View Code Duplication
    protected function calculateTax(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...
273
    {
274 228
        $total = $itemHolder->getItems()
275 228
            ->reduce(function ($sum, ItemInterface $item) {
276 224
                $sum += ($item->getPriceIncTax() - $item->getPrice()) * $item->getQuantity();
277
278 224
                return $sum;
279 228
            }, 0);
280 228
        $itemHolder->setTax($total);
281
    }
282
283
    /**
284
     * @param ItemHolderInterface $itemHolder
285
     */
286 228
    protected function calculateAll(ItemHolderInterface $itemHolder)
287
    {
288 228
        $this->calculateDeliveryFeeTotal($itemHolder);
289 228
        $this->calculateCharge($itemHolder);
290 228
        $this->calculateDiscount($itemHolder);
291 228
        $this->calculateSubTotal($itemHolder); // Order の場合のみ
292 228
        $this->calculateTax($itemHolder);
293 228
        $this->calculateTotal($itemHolder);
294
    }
295
}
296