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

AddPointProcessor::calculateAddPoint()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 2
nop 1
dl 0
loc 29
ccs 0
cts 4
cp 0
crap 56
rs 8.5226
c 0
b 0
f 0
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 Eccube\Entity\BaseInfo;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Entity\ItemInterface;
19
use Eccube\Entity\Order;
20
use Eccube\Repository\BaseInfoRepository;
21
use Eccube\Service\PurchaseFlow\ItemHolderPostValidator;
22
use Eccube\Service\PurchaseFlow\PurchaseContext;
23
24
/**
25
 * 加算ポイント.
26
 */
27
class AddPointProcessor extends ItemHolderPostValidator
28
{
29
    /**
30
     * @var BaseInfo
31
     */
32
    protected $BaseInfo;
33
34
    /**
35
     * AddPointProcessor constructor.
36
     *
37
     * @param BaseInfoRepository $baseInfoRepository
38
     */
39
    public function __construct(BaseInfoRepository $baseInfoRepository)
40
    {
41
        $this->BaseInfo = $baseInfoRepository->get();
42
    }
43
44
    /**
45
     * @param ItemHolderInterface $itemHolder
46
     * @param PurchaseContext $context
47
     */
48
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
49
    {
50
        if (!$this->supports($itemHolder)) {
51
            return;
52
        }
53
54
        // 付与ポイントを計算
55
        $addPoint = $this->calculateAddPoint($itemHolder);
56
        $itemHolder->setAddPoint($addPoint);
57
    }
58
59
    /**
60
     * 付与ポイントを計算.
61
     *
62
     * @param ItemHolderInterface $itemHolder
63
     *
64
     * @return int
65
     */
66
    private function calculateAddPoint(ItemHolderInterface $itemHolder)
67
    {
68
        $basicPointRate = $this->BaseInfo->getBasicPointRate();
69
70
        // 明細ごとのポイントを集計
71
        $totalPoint = array_reduce($itemHolder->getItems()->toArray(),
0 ignored issues
show
Bug introduced by
The method toArray cannot be called on $itemHolder->getItems() (of type array<integer,object<Ecc...\Entity\ItemInterface>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
72
            function ($carry, ItemInterface $item) use ($basicPointRate) {
73
                $pointRate = $item->isProduct() ? $item->getProductClass()->getPointRate() : null;
74
                if ($pointRate === null) {
75
                    $pointRate = $basicPointRate;
76
                }
77
78
                // TODO: ポイントは税抜き分しか割引されない、ポイント明細は税抜きのままでいいのか?
79
                $point = 0;
80
                if ($item->isPoint()) {
81
                    $point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity();
82
                // Only calc point on product
83
                } elseif ($item->isProduct()) {
84
                    // ポイント = 単価 * ポイント付与率 * 数量
85
                    $point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity();
86
                } elseif($item->isDiscount()) {
87
                    $point = round($item->getPrice() * ($pointRate / 100)) * $item->getQuantity();
88
                }
89
90
                return $carry + $point;
91
            }, 0);
92
93
        return $totalPoint < 0 ? 0 : $totalPoint;
94
    }
95
96
    /**
97
     * Processorが実行出来るかどうかを返す.
98
     *
99
     * 以下を満たす場合に実行できる.
100
     *
101
     * - ポイント設定が有効であること.
102
     * - $itemHolderがOrderエンティティであること.
103
     * - 会員のOrderであること.
104
     *
105
     * @param ItemHolderInterface $itemHolder
106
     *
107
     * @return bool
108
     */
109 View Code Duplication
    private function supports(ItemHolderInterface $itemHolder)
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...
110
    {
111
        if (!$this->BaseInfo->isOptionPoint()) {
112
            return false;
113
        }
114
115
        if (!$itemHolder instanceof Order) {
116
            return false;
117
        }
118
119
        if (!$itemHolder->getCustomer()) {
120
            return false;
121
        }
122
123
        return true;
124
    }
125
126
    public function __toString()
127
    {
128
        return get_class($this);
129
    }
130
}
131