Failed Conditions
Pull Request — experimental/sf (#3416)
by
unknown
329:25 queued 320:39
created

PointDiffProcessor   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 170
Duplicated Lines 13.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 74.42%

Importance

Changes 0
Metric Value
dl 23
loc 170
ccs 32
cts 43
cp 0.7442
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 19 4
A prepare() 12 12 2
A commit() 0 4 1
A rollback() 11 11 2
A getDiffOfUsePoint() 0 11 2
B supports() 0 30 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\ItemHolderInterface;
19
use Eccube\Entity\Master\OrderStatus;
20
use Eccube\Entity\Order;
21
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
22
use Eccube\Service\PurchaseFlow\PurchaseContext;
23
use Eccube\Service\PurchaseFlow\PurchaseProcessor;
24
25
/**
26
 * 受注編集におけるポイント処理.
27
 */
28
class PointDiffProcessor extends ItemHolderValidator implements PurchaseProcessor
29
{
30
    /**
31
     * @var EntityManagerInterface
32
     */
33
    protected $entityManager;
34
35
    /**
36
     * @var BaseInfo
37
     */
38
    protected $BaseInfo;
39
40
    /**
41
     * AddPointProcessor constructor.
42
     *
43
     * @param EntityManagerInterface $entityManager
44
     * @param BaseInfo $BaseInfo
0 ignored issues
show
introduced by
Expected 15 spaces after parameter type; 1 found
Loading history...
45
     */
46 712
    public function __construct(EntityManagerInterface $entityManager, BaseInfo $BaseInfo)
47
    {
48 712
        $this->entityManager = $entityManager;
49 712
        $this->BaseInfo = $BaseInfo;
50
    }
51
52
    /*
53
     * ItemHolderValidator
54
     */
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 176
    protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
60
    {
61 176
        if (!$this->supports($itemHolder, $context)) {
62 153
            return;
63
        }
64
65 26
        $diffUsePoint = $this->getDiffOfUsePoint($itemHolder, $context);
66
67
        // 所有ポイント < 新規利用ポイント
68 26
        $Customer = $itemHolder->getCustomer();
69 26
        if ($Customer->getPoint() < $diffUsePoint) {
70 5
            $this->throwInvalidItemException('利用ポイントが所有ポイントを上回っています.');
71
        }
72
73
        // 支払い金額 < 利用ポイント
74 21
        if ($itemHolder->getTotal() < 0) {
75 2
            $this->throwInvalidItemException('利用ポイントがお支払い金額を上回っています.');
76
        }
77
    }
78
79
    /*
80
     * PurchaseProcessor
81
     */
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 16 View Code Duplication
    public function prepare(ItemHolderInterface $itemHolder, PurchaseContext $context)
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...
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
87
    {
88 16
        if (!$this->supports($itemHolder, $context)) {
89 2
            return;
90
        }
91
92 14
        $diffUsePoint = $this->getDiffOfUsePoint($itemHolder, $context);
93
94
        // ユーザの保有ポイントを減算
95 14
        $Customer = $itemHolder->getCustomer();
96 14
        $Customer->setPoint($Customer->getPoint() - $diffUsePoint);
97
    }
98
99
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$target" missing
Loading history...
introduced by
Doc comment for parameter "$context" missing
Loading history...
100
     * {@inheritdoc
101
     */
102
    public function commit(ItemHolderInterface $target, PurchaseContext $context)
103
    {
104
        // 何もしない
105
    }
106
107
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$itemHolder" missing
Loading history...
introduced by
Doc comment for parameter "$context" missing
Loading history...
108
     * {@inheritdoc
109
     */
110 View Code Duplication
    public function rollback(ItemHolderInterface $itemHolder, PurchaseContext $context)
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...
111
    {
112
        if (!$this->supports($itemHolder, $context)) {
113
            return;
114
        }
115
116
        $diffUsePoint = $this->getDiffOfUsePoint($itemHolder, $context);
117
118
        $Customer = $itemHolder->getCustomer();
119
        $Customer->setPoint($Customer->getPoint() + $diffUsePoint);
120
    }
121
122
    /*
123
     * Helper methods
124
     */
125
126
    /**
127
     * Processorが実行出来るかどうかを返す.
128
     *
129
     * 以下を満たす場合に実行できる.
130
     *
131
     * - ポイント設定が有効であること.
132
     * - $itemHolderがOrderエンティティであること.
133
     * - OrderStatusが新規受付、入金済み、対応中、発送済みのどれかであること
134
     * - 会員のOrderであること.
135
     * - PurchaseContextでOriginHolderが渡ってきている
136
     *
137
     * @param ItemHolderInterface $itemHolder
138
     * @param PurchaseContext $context
139
     *
140
     * @return bool
141
     */
142 187
    private function supports(ItemHolderInterface $itemHolder, PurchaseContext $context)
143
    {
144 187
        if (!$this->BaseInfo->isOptionPoint()) {
145
            return false;
146
        }
147
148 187
        if (!$itemHolder instanceof Order) {
149
            return false;
150
        }
151
152 187
        switch ($itemHolder->getOrderStatus()->getId()) {
153
            case OrderStatus::NEW:
154
            case OrderStatus::PAID:
155
            case OrderStatus::IN_PROGRESS:
156
            case OrderStatus::DELIVERED:
157 35
                break;
158
            default:
159 155
                return false;
160
        }
161
162 35
        if (!$itemHolder->getCustomer()) {
163
            return false;
164
        }
165
166 35
        if (is_null($context->getOriginHolder())) {
167
            return false;
168
        }
169
170 35
        return true;
171
    }
172
173
    /**
174
     * 利用ポイントの差を計算する
175
     * この差が新規利用ポイントとなる
176
     *
177
     * 使用ポイントが増えた場合プラスとなる
178
     * 50 -> 100 : 50
179
     * 100 -> 50 : -50
180
     *
181
     * @param ItemHolderInterface $itemHolder
182
     * @param PurchaseContext $context
183
     *
184
     * @return int
185
     */
186 35
    protected function getDiffOfUsePoint(ItemHolderInterface $itemHolder, PurchaseContext $context)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
187
    {
188 35
        if ($context->getOriginHolder()) {
189 35
            $fromUsePoint = $context->getOriginHolder()->getUsePoint();
190
        } else {
191
            $fromUsePoint = 0;
192
        }
193 35
        $toUsePoint = $itemHolder->getUsePoint();
194
195 35
        return $toUsePoint - $fromUsePoint;
196
    }
197
}
198