Failed Conditions
Push — sf/last-boss ( 53d963...58156b )
by Kiyotaka
09:54 queued 04:25
created

AddPointProcessor::supports()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 1
dl 16
loc 16
rs 9.7333
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 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->getPointRate();
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
                }
87
88
                return $carry + $point;
89
            }, 0);
90
91
        return $totalPoint < 0 ? 0 : $totalPoint;
92
    }
93
94
    /**
95
     * Processorが実行出来るかどうかを返す.
96
     *
97
     * 以下を満たす場合に実行できる.
98
     *
99
     * - ポイント設定が有効であること.
100
     * - $itemHolderがOrderエンティティであること.
101
     * - 会員のOrderであること.
102
     *
103
     * @param ItemHolderInterface $itemHolder
104
     *
105
     * @return bool
106
     */
107 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...
108
    {
109
        if (!$this->BaseInfo->isOptionPoint()) {
110
            return false;
111
        }
112
113
        if (!$itemHolder instanceof Order) {
114
            return false;
115
        }
116
117
        if (!$itemHolder->getCustomer()) {
118
            return false;
119
        }
120
121
        return true;
122
    }
123
}
124