Failed Conditions
Pull Request — experimental/sf (#31)
by Kentaro
06:59
created

PointDiffProcessor::supports()   B

Complexity

Conditions 9
Paths 15

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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