Failed Conditions
Pull Request — experimental/3.1 (#2486)
by
unknown
68:04 queued 25:47
created

PurchaseFlow::calculateDeliveryFeeTotal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 11
loc 11
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
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
    /**
27
     * @var ArrayCollection|ItemProcessor[]
28
     */
29
    protected $addItemProcessors;
30
31 49
    public function __construct()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
32
    {
33 49
        $this->itemProcessors = new ArrayCollection();
34 49
        $this->itemHolderProcessors = new ArrayCollection();
35 49
        $this->purchaseProcessors = new ArrayCollection();
36 49
        $this->addItemProcessors = new ArrayCollection();
37
    }
38
39 24
    public function setItemProcessors(ArrayCollection $processors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41 24
        $this->itemProcessors = $processors;
42
    }
43
44 24
    public function setItemHolderProcessors(ArrayCollection $processors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
45
    {
46 24
        $this->itemHolderProcessors = $processors;
47
    }
48
49
    public function setPurchaseProcessors(ArrayCollection $processors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
50
    {
51
        $this->purchaseProcessors = $processors;
52
    }
53
54 32
    public function setAddItemProcessors(ArrayCollection $addItemProcessors)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
55
    {
56 32
        $this->addItemProcessors = $addItemProcessors;
57
    }
58
59 15
    public function addItem(ItemInterface $item, PurchaseContext $context)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
60
    {
61 15
        $holder = $context->getOriginHolder();
62 15
        $flowResult = new PurchaseFlowResult($holder);
0 ignored issues
show
Bug introduced by
It seems like $holder defined by $context->getOriginHolder() on line 61 can be null; however, Eccube\Service\PurchaseF...owResult::__construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
63
64 15
        foreach ($this->addItemProcessors as $addItemProcessor) {
65 15
            $result = $addItemProcessor->process($item, $context);
66 15
            $flowResult->addProcessResult($result);
67
        }
68
69 15
        return $flowResult;
70
    }
71
72 20
    public function calculate(ItemHolderInterface $itemHolder, PurchaseContext $context)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
73
    {
74 20
        $this->calculateAll($itemHolder);
75
76 20
        $flowResult = new PurchaseFlowResult($itemHolder);
77
78 20
        foreach ($itemHolder->getItems() as $item) {
79 15
            foreach ($this->itemProcessors as $itemProsessor) {
80 15
                $result = $itemProsessor->process($item, $context);
81 15
                $flowResult->addProcessResult($result);
82
            }
83
        }
84
85 20
        $this->calculateAll($itemHolder);
86
87 20
        foreach ($this->itemHolderProcessors as $holderProcessor) {
88 16
            $result = $holderProcessor->process($itemHolder, $context);
89 16
            $flowResult->addProcessResult($result);
90
        }
91
92 20
        $this->calculateAll($itemHolder);
93
94 20
        return $flowResult;
95
    }
96
97
    /**
98
     * @param ItemHolderInterface $target
99
     * @param PurchaseContext     $context
100
     *
101
     * @throws PurchaseException
102
     */
103
    public function purchase(ItemHolderInterface $target, PurchaseContext $context)
104
    {
105
        foreach ($this->purchaseProcessors as $processor) {
106
            $processor->process($target, $context);
107
        }
108
    }
109
110 8
    public function addPurchaseProcessor(PurchaseProcessor $processor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
111
    {
112 8
        $this->purchaseProcessors[] = $processor;
113
    }
114
115 32
    public function addItemHolderProcessor(ItemHolderProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
116
    {
117 32
        $this->itemHolderProcessors[] = $prosessor;
118
    }
119
120 33
    public function addItemProcessor(ItemProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
121
    {
122 33
        $this->itemProcessors[] = $prosessor;
123
    }
124
125 2
    public function addAddItemProcessor(ItemProcessor $prosessor)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
126
    {
127 2
        $this->addItemProcessors[] = $prosessor;
128
    }
129
130
    /**
131
     * @param ItemHolderInterface $itemHolder
132
     */
133 20
    protected function calculateTotal(ItemHolderInterface $itemHolder)
134
    {
135
        $total = $itemHolder->getItems()->reduce(function ($sum, ItemInterface $item) {
136 15
            $sum += $item->getPriceIncTax() * $item->getQuantity();
137
138 15
            return $sum;
139 20
        }, 0);
140 20
        $itemHolder->setTotal($total);
141
        // TODO
142 20
        if ($itemHolder instanceof \Eccube\Entity\Order) {
143
            // Order には PaymentTotal もセットする
144 13
            $itemHolder->setPaymentTotal($total);
145
        }
146
    }
147
148 20 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...
149
    {
150 20
        $total = $itemHolder->getItems()
151 20
            ->getProductClasses()
152
            ->reduce(function ($sum, ItemInterface $item) {
153 13
                $sum += $item->getPriceIncTax() * $item->getQuantity();
154
155 13
                return $sum;
156 20
            }, 0);
157
        // TODO
158 20
        if ($itemHolder instanceof \Eccube\Entity\Order) {
159
            // Order の場合は SubTotal をセットする
160 13
            $itemHolder->setSubTotal($total);
161
        }
162
    }
163
164
    /**
165
     * @param ItemHolderInterface $itemHolder
166
     */
167 20 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...
168
    {
169 20
        $total = $itemHolder->getItems()
170 20
            ->getDeliveryFees()
171
            ->reduce(function ($sum, ItemInterface $item) {
172 11
                $sum += $item->getPriceIncTax() * $item->getQuantity();
173
174 11
                return $sum;
175 20
            }, 0);
176 20
        $itemHolder->setDeliveryFeeTotal($total);
177
    }
178
179
    /**
180
     * @param ItemHolderInterface $itemHolder
181
     */
182 20 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...
183
    {
184 20
        $total = $itemHolder->getItems()
185 20
            ->getDiscounts()
186
            ->reduce(function ($sum, ItemInterface $item) {
187
                $sum += $item->getPriceIncTax() * $item->getQuantity();
188
189
                return $sum;
190 20
            }, 0);
191
        // TODO 後方互換のため discount には正の整数を代入する
192 20
        $itemHolder->setDiscount($total * -1);
193
    }
194
195
    /**
196
     * @param ItemHolderInterface $itemHolder
197
     */
198 20 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...
199
    {
200 20
        $total = $itemHolder->getItems()
201 20
            ->getCharges()
202
            ->reduce(function ($sum, ItemInterface $item) {
203
                $sum += $item->getPriceIncTax() * $item->getQuantity();
204
205
                return $sum;
206 20
            }, 0);
207 20
        $itemHolder->setCharge($total);
208
    }
209
210
    /**
211
     * @param ItemHolderInterface $itemHolder
212
     */
213 20 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...
214
    {
215 20
        $total = $itemHolder->getItems()
216 20
            ->reduce(function ($sum, ItemInterface $item) {
217 15
                $sum += ($item->getPriceIncTax() - $item->getPrice()) * $item->getQuantity();
218
219 15
                return $sum;
220 20
            }, 0);
221 20
        $itemHolder->setTax($total);
222
    }
223
224
    /**
225
     * @param ItemHolderInterface $itemHolder
226
     */
227 20
    protected function calculateAll(ItemHolderInterface $itemHolder)
228
    {
229 20
        $this->calculateDeliveryFeeTotal($itemHolder);
230 20
        $this->calculateCharge($itemHolder);
231 20
        $this->calculateDiscount($itemHolder);
232 20
        $this->calculateSubTotal($itemHolder); // Order の場合のみ
233 20
        $this->calculateTax($itemHolder);
234 20
        $this->calculateTotal($itemHolder);
235
    }
236
}
237