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

DeliveryFeeFreeByShippingPreprocessor::process()   C

Complexity

Conditions 13
Paths 75

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13

Importance

Changes 0
Metric Value
cc 13
nc 75
nop 2
dl 0
loc 40
rs 6.6166
c 0
b 0
f 0
ccs 22
cts 22
cp 1
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Processor;
15
16
use Eccube\Entity\BaseInfo;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Entity\Order;
19
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
20
use Eccube\Service\PurchaseFlow\PurchaseContext;
21
22
/**
23
 * 送料無料条件を適用する.
24
 * お届け先ごとに条件判定を行う.
25
 */
26
class DeliveryFeeFreeByShippingPreprocessor implements ItemHolderPreprocessor
27
{
28
    /**
29
     * @var BaseInfo
30
     */
31
    protected $BaseInfo;
32
33
    /**
34
     * DeliveryFeeProcessor constructor.
35
     *
36
     * @param BaseInfo $BaseInfo
37
     */
38 70
    public function __construct(BaseInfo $BaseInfo)
39
    {
40 70
        $this->BaseInfo = $BaseInfo;
41
    }
42
43
    /**
44
     * @param ItemHolderInterface $itemHolder
45
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
46
     */
47 62
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
48
    {
49 62
        if (!($this->BaseInfo->getDeliveryFreeAmount() || $this->BaseInfo->getDeliveryFreeQuantity())) {
50 52
            return;
51
        }
52
53
        // Orderの場合はお届け先ごとに判定する.
54 10
        if ($itemHolder instanceof Order) {
55
            /** @var Order $Order */
56 10
            $Order = $itemHolder;
57 10
            foreach ($Order->getShippings() as $Shipping) {
58 10
                $isFree = false;
59 10
                $total = 0;
60 10
                $quantity = 0;
61 10
                foreach ($Shipping->getProductOrderItems() as $Item) {
62 10
                    $total += $Item->getPriceIncTax() * $Item->getQuantity();
63 10
                    $quantity += $Item->getQuantity();
64
                }
65
                // 送料無料(金額)を超えている
66 10
                if ($this->BaseInfo->getDeliveryFreeAmount()) {
67 5
                    if ($total >= $this->BaseInfo->getDeliveryFreeAmount()) {
68 3
                        $isFree = true;
69
                    }
70
                }
71
                // 送料無料(個数)を超えている
72 10
                if ($this->BaseInfo->getDeliveryFreeQuantity()) {
73 5
                    if ($quantity >= $this->BaseInfo->getDeliveryFreeQuantity()) {
74 3
                        $isFree = true;
75
                    }
76
                }
77 10
                if ($isFree) {
78 6
                    foreach ($Shipping->getOrderItems() as $Item) {
79 6
                        if ($Item->isDeliveryFee()) {
80 10
                            $Item->setQuantity(0);
81
                        }
82
                    }
83
                }
84
            }
85
        }
86
    }
87
}
88