Failed Conditions
Push — experimental/3.1 ( 965511...751c7a )
by chihiro
21s
created

PurchaseFlow::calculate()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 2
dl 0
loc 25
rs 8.5806
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->calculateDeliveryFeeTotal($itemHolder);
51
        $this->calculateCharge($itemHolder);
52
        $this->calculateDiscount($itemHolder);
53
        $this->calculateSubTotal($itemHolder); // Order の場合のみ
54
        $this->calculateTax($itemHolder);
55
        $this->calculateTotal($itemHolder);
56
57
        $flowResult = new PurchaseFlowResult($itemHolder);
58
59
        foreach ($itemHolder->getItems() as $item) {
60
            foreach ($this->itemProcessors as $itemProsessor) {
61
                $result = $itemProsessor->process($item, $context);
62
                $flowResult->addProcessResult($result);
63
            }
64
        }
65
66
        foreach ($this->itemHolderProcessors as $holderProcessor) {
67
            $result = $holderProcessor->process($itemHolder, $context);
68
            $flowResult->addProcessResult($result);
69
        }
70
71
        return $flowResult;
72
    }
73
74
    /**
75
     * @param ItemHolderInterface $target
76
     * @param PurchaseContext     $context
77
     *
78
     * @throws PurchaseException
79
     */
80
    public function purchase(ItemHolderInterface $target, PurchaseContext $context)
81
    {
82
        foreach ($this->purchaseProcessors as $processor) {
83
            $processor->process($target, $context);
84
        }
85
    }
86
87
    public function addPurchaseProcessor(PurchaseProcessor $processor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
88
    {
89
        $this->purchaseProcessors[] = $processor;
90
    }
91
92
    public function addItemHolderProcessor(ItemHolderProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
93
    {
94
        $this->itemHolderProcessors[] = $prosessor;
95
    }
96
97
    public function addItemProcessor(ItemProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
98
    {
99
        $this->itemProcessors[] = $prosessor;
100
    }
101
102
    /**
103
     * @param ItemHolderInterface $itemHolder
104
     */
105
    protected function calculateTotal(ItemHolderInterface $itemHolder)
106
    {
107
        $total = $itemHolder->getItems()->reduce(function ($sum, ItemInterface $item) {
108
            $sum += $item->getPriceIncTax() * $item->getQuantity();
109
110
            return $sum;
111
        }, 0);
112
        $itemHolder->setTotal($total);
113
        // TODO
114
        if ($itemHolder instanceof \Eccube\Entity\Order) {
115
            // Order には PaymentTotal もセットする
116
            $itemHolder->setPaymentTotal($total);
117
        }
118
    }
119
120 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...
121
    {
122
        $total = $itemHolder->getItems()
123
            ->getProductClasses()
124
            ->reduce(function ($sum, ItemInterface $item) {
125
                $sum += $item->getPriceIncTax() * $item->getQuantity();
126
127
                return $sum;
128
            }, 0);
129
        // TODO
130
        if ($itemHolder instanceof \Eccube\Entity\Order) {
131
            // Order の場合は SubTotal をセットする
132
            $itemHolder->setSubTotal($total);
133
        }
134
    }
135
136
    /**
137
     * @param ItemHolderInterface $itemHolder
138
     */
139 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...
140
    {
141
        $total = $itemHolder->getItems()
142
            ->getDeliveryFees()
143
            ->reduce(function ($sum, ItemInterface $item) {
144
                $sum += $item->getPriceIncTax() * $item->getQuantity();
145
146
                return $sum;
147
            }, 0);
148
        $itemHolder->setDeliveryFeeTotal($total);
149
    }
150
151
    /**
152
     * @param ItemHolderInterface $itemHolder
153
     */
154 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...
155
    {
156
        $total = $itemHolder->getItems()
157
            ->getDiscounts()
158
            ->reduce(function ($sum, ItemInterface $item) {
159
                $sum += $item->getPriceIncTax() * $item->getQuantity();
160
161
                return $sum;
162
            }, 0);
163
        // TODO 後方互換のため discount には正の整数を代入する
164
        $itemHolder->setDiscount($total * -1);
165
    }
166
167
    /**
168
     * @param ItemHolderInterface $itemHolder
169
     */
170 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...
171
    {
172
        $total = $itemHolder->getItems()
173
            ->getCharges()
174
            ->reduce(function ($sum, ItemInterface $item) {
175
                $sum += $item->getPriceIncTax() * $item->getQuantity();
176
177
                return $sum;
178
            }, 0);
179
        $itemHolder->setCharge($total);
180
    }
181
182
    /**
183
     * @param ItemHolderInterface $itemHolder
184
     */
185 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...
186
    {
187
        $total = $itemHolder->getItems()
188
            ->reduce(function ($sum, ItemInterface $item) {
189
                $sum += ($item->getPriceIncTax() - $item->getPrice()) * $item->getQuantity();
190
191
                return $sum;
192
            }, 0);
193
        $itemHolder->setTax($total);
194
    }
195
}
196