Completed
Pull Request — experimental/3.1 (#2435)
by chihiro
84:29 queued 21:04
created

PurchaseFlow::calculateAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Service\PurchaseFlow;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Eccube\Entity\ItemHolderInterface;
7
use Eccube\Entity\ItemInterface;
8
9
class PurchaseFlow
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
10
{
11
    /**
12
     * @var ArrayCollection|ItemHolderProcessor[]
13
     */
14
    protected $itemHolderProcessors;
15
16
    /**
17
     * @var ArrayCollection|ItemProcessor[]
18
     */
19
    protected $itemProcessors;
20
21
    /**
22
     * @var ArrayCollection|PurchaseProcessor[]
23
     */
24
    protected $purchaseProcessors;
25
26
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
27
    {
28
        $this->itemProcessors = new ArrayCollection();
29
        $this->itemHolderProcessors = new ArrayCollection();
30
        $this->purchaseProcessors = new ArrayCollection();
31
    }
32
33
    public function setItemProcessors(ArrayCollection $processors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
34
    {
35
        $this->itemProcessors = $processors;
36
    }
37
38
    public function setItemHolderProcessors(ArrayCollection $processors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
39
    {
40
        $this->itemHolderProcessors = $processors;
41
    }
42
43
    public function setPurchaseProcessors(ArrayCollection $processors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
44
    {
45
        $this->purchaseProcessors = $processors;
46
    }
47
48
    public function calculate(ItemHolderInterface $itemHolder, PurchaseContext $context)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
49
    {
50
        $this->calculateAll($itemHolder);
51
52
        $flowResult = new PurchaseFlowResult($itemHolder);
53
54
        foreach ($itemHolder->getItems() as $item) {
55
            foreach ($this->itemProcessors as $itemProsessor) {
56
                $result = $itemProsessor->process($item, $context);
57
                $flowResult->addProcessResult($result);
58
            }
59
        }
60
61
        $this->calculateAll($itemHolder);
62
63
        foreach ($this->itemHolderProcessors as $holderProcessor) {
64
            $result = $holderProcessor->process($itemHolder, $context);
65
            $flowResult->addProcessResult($result);
66
        }
67
68
        $this->calculateAll($itemHolder);
69
70
        return $flowResult;
71
    }
72
73
    /**
74
     * @param ItemHolderInterface $target
75
     * @param PurchaseContext     $context
76
     *
77
     * @throws PurchaseException
78
     */
79
    public function purchase(ItemHolderInterface $target, PurchaseContext $context)
80
    {
81
        foreach ($this->purchaseProcessors as $processor) {
82
            $processor->process($target, $context);
83
        }
84
    }
85
86
    public function addPurchaseProcessor(PurchaseProcessor $processor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
87
    {
88
        $this->purchaseProcessors[] = $processor;
89
    }
90
91
    public function addItemHolderProcessor(ItemHolderProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
92
    {
93
        $this->itemHolderProcessors[] = $prosessor;
94
    }
95
96
    public function addItemProcessor(ItemProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
97
    {
98
        $this->itemProcessors[] = $prosessor;
99
    }
100
101
    /**
102
     * @param ItemHolderInterface $itemHolder
103
     */
104
    protected function calculateTotal(ItemHolderInterface $itemHolder)
105
    {
106
        $total = $itemHolder->getItems()->reduce(function ($sum, ItemInterface $item) {
107
            $sum += $item->getPriceIncTax() * $item->getQuantity();
108
109
            return $sum;
110
        }, 0);
111
        $itemHolder->setTotal($total);
112
        // TODO
113
        if ($itemHolder instanceof \Eccube\Entity\Order) {
114
            // Order には PaymentTotal もセットする
115
            $itemHolder->setPaymentTotal($total);
116
        }
117
    }
118
119 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...
120
    {
121
        $total = $itemHolder->getItems()
122
            ->getProductClasses()
123
            ->reduce(function ($sum, ItemInterface $item) {
124
                $sum += $item->getPriceIncTax() * $item->getQuantity();
125
126
                return $sum;
127
            }, 0);
128
        // TODO
129
        if ($itemHolder instanceof \Eccube\Entity\Order) {
130
            // Order の場合は SubTotal をセットする
131
            $itemHolder->setSubTotal($total);
132
        }
133
    }
134
135
    /**
136
     * @param ItemHolderInterface $itemHolder
137
     */
138 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...
139
    {
140
        $total = $itemHolder->getItems()
141
            ->getDeliveryFees()
142
            ->reduce(function ($sum, ItemInterface $item) {
143
                $sum += $item->getPriceIncTax() * $item->getQuantity();
144
145
                return $sum;
146
            }, 0);
147
        $itemHolder->setDeliveryFeeTotal($total);
148
    }
149
150
    /**
151
     * @param ItemHolderInterface $itemHolder
152
     */
153 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...
154
    {
155
        $total = $itemHolder->getItems()
156
            ->getDiscounts()
157
            ->reduce(function ($sum, ItemInterface $item) {
158
                $sum += $item->getPriceIncTax() * $item->getQuantity();
159
160
                return $sum;
161
            }, 0);
162
        // TODO 後方互換のため discount には正の整数を代入する
163
        $itemHolder->setDiscount($total * -1);
164
    }
165
166
    /**
167
     * @param ItemHolderInterface $itemHolder
168
     */
169 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...
170
    {
171
        $total = $itemHolder->getItems()
172
            ->getCharges()
173
            ->reduce(function ($sum, ItemInterface $item) {
174
                $sum += $item->getPriceIncTax() * $item->getQuantity();
175
176
                return $sum;
177
            }, 0);
178
        $itemHolder->setCharge($total);
179
    }
180
181
    /**
182
     * @param ItemHolderInterface $itemHolder
183
     */
184 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...
185
    {
186
        $total = $itemHolder->getItems()
187
            ->reduce(function ($sum, ItemInterface $item) {
188
                $sum += ($item->getPriceIncTax() - $item->getPrice()) * $item->getQuantity();
189
190
                return $sum;
191
            }, 0);
192
        $itemHolder->setTax($total);
193
    }
194
195
    /**
196
     * @param ItemHolderInterface $itemHolder
197
     */
198
    protected function calculateAll(ItemHolderInterface $itemHolder)
199
    {
200
        $this->calculateDeliveryFeeTotal($itemHolder);
201
        $this->calculateCharge($itemHolder);
202
        $this->calculateDiscount($itemHolder);
203
        $this->calculateSubTotal($itemHolder); // Order の場合のみ
204
        $this->calculateTax($itemHolder);
205
        $this->calculateTotal($itemHolder);
206
    }
207
}
208