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

SubtractPointPreprocessor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
ccs 0
cts 10
cp 0
wmc 5
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 10 2
A subtract() 0 6 2
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\ProcessResult;
21
use Eccube\Service\PurchaseFlow\PurchaseContext;
22
23
/**
24
 * 利用ポイントに応じてポイントを減算する.
25
 *
26
 * 例) ポイント付与率10%で、1000円分購入したとき
27
 * ポイント利用なし -> 1000円 * 10% = 100ポイント付与
28
 * 500ポイント利用して購入 -> (1000円 - 500p) * 10% = 50ポイント付与
29
 */
30
class SubtractPointPreprocessor implements ItemHolderPreprocessor
31
{
32
    /**
33
     * @var BaseInfo
34
     */
35
    protected $BaseInfo;
36
37
    /**
38
     * SubstractPointProcessor constructor.
39
     *
40
     * @param BaseInfo $BaseInfo
41
     */
42
    public function __construct(BaseInfo $BaseInfo)
43
    {
44
        $this->BaseInfo = $BaseInfo;
45
    }
46
47
    /**
48
     * @param ItemHolderInterface $itemHolder
49
     * @param PurchaseContext     $context
50
     *
51
     * @return ProcessResult
52
     */
53
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
54
    {
55
        /** @var Order $Order */
56
        $Order = $itemHolder;
57
        if ($Order->getUsePoint() > 0) {
58
            $Order->setAddPoint($this->subtract($Order->getAddPoint(), $Order->getUsePoint(), $this->BaseInfo->getBasicPointRate()));
59
        }
60
61
        return ProcessResult::success();
62
    }
63
64
    /**
65
     * Subtract point.
66
     *
67
     * @param integer $totalPoint 合計ポイント
68
     * @param integer $usePoint 利用ポイント
69
     * @param integer $pointRate ポイント付与率(%)
70
     *
71
     * @return integer Point after subtraction
72
     */
73
    protected function subtract($totalPoint, $usePoint, $pointRate)
74
    {
75
        $add_point = $totalPoint - intval($usePoint * ($pointRate / 100));
76
77
        return $add_point < 0 ? 0 : $add_point;
78
    }
79
}
80