Completed
Pull Request — 4.0 (#4223)
by Kentaro
04:57
created

TaxProcessor::process()   B

Complexity

Conditions 9
Paths 38

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 9.0033

Importance

Changes 0
Metric Value
cc 9
nc 38
nop 2
dl 0
loc 55
ccs 28
cts 29
cp 0.9655
crap 9.0033
rs 7.4262
c 0
b 0
f 0

How to fix   Long Method   

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) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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 Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Entity\Master\OrderItemType;
19
use Eccube\Entity\Master\TaxDisplayType;
20
use Eccube\Entity\Master\TaxType;
21
use Eccube\Entity\Order;
22
use Eccube\Repository\TaxRuleRepository;
23
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
24
use Eccube\Service\PurchaseFlow\PurchaseContext;
25
use Eccube\Service\TaxRuleService;
26
27
class TaxProcessor implements ItemHolderPreprocessor
28
{
29
    /**
30
     * @var EntityManagerInterface
31
     */
32
    protected $entityManager;
33
34
    /**
35
     * @var TaxRuleRepository
36
     */
37
    protected $taxRuleRepository;
38
39
    /**
40
     * @var TaxRuleService
41
     */
42
    protected $taxRuleService;
43
44
    /**
45
     * TaxProcessor constructor.
46
     *
47
     * @param TaxRuleRepository $taxRuleRepository
48
     * @param TaxRuleService $taxRuleService
49
     */
50 728
    public function __construct(
51
        EntityManagerInterface $entityManager,
52
        TaxRuleRepository $taxRuleRepository,
53
        TaxRuleService $taxRuleService
54
    ) {
55 728
        $this->entityManager = $entityManager;
56 728
        $this->taxRuleRepository = $taxRuleRepository;
57 728
        $this->taxRuleService = $taxRuleService;
58
    }
59
60
    /**
61
     * @param ItemHolderInterface $itemHolder
62
     * @param PurchaseContext $context
63
     *
64
     * @throws \Doctrine\ORM\NoResultException
65
     */
66 207
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
67
    {
68 207
        if (!$itemHolder instanceof Order) {
69
            return;
70
        }
71
72 207
        foreach ($itemHolder->getOrderItems() as $item) {
73
            // 明細種別に応じて税区分, 税表示区分を設定する,
74 207
            $OrderItemType = $item->getOrderItemType();
75
76 207
            if (!$item->getTaxType()) {
77 53
                $item->setTaxType($this->getTaxType($OrderItemType));
78
            }
79 207
            if (!$item->getTaxDisplayType()) {
80 53
                $item->setTaxDisplayType($this->getTaxDisplayType($OrderItemType));
81
            }
82
83
            // 税区分: 非課税, 不課税
84 207
            if ($item->getTaxType()->getId() != TaxType::TAXATION) {
85 154
                $item->setTax(0);
86 154
                $item->setTaxRate(0);
87 154
                $item->setRoundingType(null);
88 154
                $item->setTaxRuleId(null);
89
90 154
                continue;
91
            }
92
93 207
            if ($item->getTaxRuleId()) {
94 19
                $TaxRule = $this->taxRuleRepository->find($item->getTaxRuleId());
95
            } else {
96 207
                $TaxRule = $this->taxRuleRepository->getByRule($item->getProduct(), $item->getProductClass());
97
            }
98
99
            // $TaxRuleを取得出来ない場合は基本税率設定を使用.
100 207
            if (null === $TaxRule) {
101 205
                $TaxRule = $this->taxRuleRepository->getByRule();
102 205
            }
103 205
104
            // 税込表示の場合は, priceが税込金額のため割り戻す.
105 207
            if ($item->getTaxDisplayType()->getId() == TaxDisplayType::INCLUDED) {
106 207
                $tax = $this->taxRuleService->calcTaxIncluded(
107 207
                    $item->getPrice(), $TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(),
108
                    $TaxRule->getTaxAdjust());
109
            } else {
110 207
                $tax = $this->taxRuleService->calcTax(
111 207
                    $item->getPrice(), $TaxRule->getTaxRate(), $TaxRule->getRoundingType()->getId(),
112 207
                    $TaxRule->getTaxAdjust());
113 207
            }
114
115
            $item->setTax($tax);
116
            $item->setTaxRate($TaxRule->getTaxRate());
117
            $item->setRoundingType($TaxRule->getRoundingType());
118
            $item->setTaxRuleId($TaxRule->getId());
119
        }
120
    }
121
122
    /**
123
     * 税区分を取得する.
124
     *
125
     * - 商品: 課税
126
     * - 送料: 課税
127
     * - 値引き: 課税
128
     * - 手数料: 課税
129
     * - ポイント値引き: 不課税
130 53
     *
131
     * @param $OrderItemType
132 53
     *
133 53
     * @return TaxType
134
     */
135
    protected function getTaxType($OrderItemType)
136 53
    {
137
        if ($OrderItemType instanceof OrderItemType) {
138 53
            $OrderItemType = $OrderItemType->getId();
139
        }
140 53
141
        $TaxType = $OrderItemType == OrderItemType::POINT
142
            ? TaxType::NON_TAXABLE
143
            : TaxType::TAXATION;
144
145
        return $this->entityManager->find(TaxType::class, $TaxType);
146
    }
147
148
    /**
149
     * 税表示区分を取得する.
150
     *
151
     * - 商品: 税抜
152
     * - 送料: 税込
153
     * - 値引き: 税抜
154
     * - 手数料: 税込
155
     * - ポイント値引き: 税込
156 53
     *
157
     * @param $OrderItemType
158 53
     *
159 53
     * @return TaxType
160
     */
161
    protected function getTaxDisplayType($OrderItemType)
162
    {
163 53
        if ($OrderItemType instanceof OrderItemType) {
164 53
            $OrderItemType = $OrderItemType->getId();
165
        }
166
167
        switch ($OrderItemType) {
168
            case OrderItemType::PRODUCT:
169
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
170
            case OrderItemType::DELIVERY_FEE:
171
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
172
            case OrderItemType::DISCOUNT:
173
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
174
            case OrderItemType::CHARGE:
175
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
176
            case OrderItemType::POINT:
177
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
178
            default:
179
                return $this->entityManager->find(TaxDisplayType::class, TaxDisplayType::EXCLUDED);
180
        }
181
    }
182
183
    public function __toString()
184
    {
185
        return get_class($this);
186
    }
187
}
188