Failed Conditions
Push — experimental/3.1 ( 7af380...69f38c )
by chihiro
29s
created

UsePointProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Service\PurchaseFlow\Processor;
4
5
use Doctrine\ORM\EntityManager;
6
use Eccube\Annotation\Inject;
7
use Eccube\Entity\ItemHolderInterface;
8
use Eccube\Entity\Master\OrderItemType;
9
use Eccube\Entity\Master\TaxDisplayType;
10
use Eccube\Entity\Master\TaxType;
11
use Eccube\Entity\BaseInfo;
12
use Eccube\Entity\Order;
13
use Eccube\Entity\OrderItem;
14
use Eccube\Entity\Shipping;
15
use Eccube\Service\PurchaseFlow\ItemHolderProcessor;
16
use Eccube\Service\PurchaseFlow\ProcessResult;
17
use Eccube\Service\PurchaseFlow\PurchaseContext;
18
use Eccube\Util\EntityUtil;
19
20
/**
21
 * 使用ポイント値引明細追加.
22
 */
23
class UsePointProcessor implements ItemHolderProcessor
24
{
25
    /**
26
     * @Inject("orm.em")
27
     * @var EntityManager
28
     */
29
    protected $entityManager;
30
31
    /**
32
     * @var BaseInfo
33
     */
34
    protected $BaseInfo;
35
36
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entityManager" missing
Loading history...
introduced by
Doc comment for parameter "$BaseInfo" missing
Loading history...
37
     * DeliveryFeeProcessor constructor.
38
     *
39
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
40
     */
41
    public function __construct(EntityManager $entityManager, BaseInfo $BaseInfo)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
42
    {
43
        $this->entityManager = $entityManager;
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
        if ($itemHolder->getUsePoint() > 0) {
56
            if ($itemHolder->getUsePoint() > $context->getUser()->getPoint()) {
57
                // TODO カートに戻さないように修正する
58
                return ProcessResult::error('利用ポイントが所有ポイントを上回っています.');
59
            }
60
61
            // XXX delete/insert ではなく, update/insert の方がいいかも
62
            $this->removePointDiscountItem($itemHolder);
63
            return $this->addPointDiscountItem($itemHolder);
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
64
        }
65
66
        return ProcessResult::success();
67
    }
68
69
    /**
70
     * 明細追加処理
71
     *
72
     * @param ItemHolderInterface $itemHolder
73
     */
74
    protected function addPointDiscountItem(ItemHolderInterface $itemHolder)
75
    {
76
        $DiscountType = $this->entityManager
77
            ->find(OrderItemType::class, OrderItemType::DISCOUNT);
78
        // TODO
79
        $TaxInclude = $this->entityManager
80
            ->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
81
        $Taxion = $this->entityManager
82
            ->find(TaxType::class, TaxType::TAXATION);
83
84
        /** @var Order $Order */
85
        $Order = $itemHolder;
86
87
        $priceOfUsePoint = $this->usePointToPrice($Order->getUsePoint());
88
        if (($itemHolder->getTotal() + $priceOfUsePoint) < 0) {
89
            // TODO カートに戻さないように修正する
90
            // TODO 送料・手数料も考慮する
91
            return ProcessResult::error('利用ポイントがお支払い金額を上回っています.');
92
        }
93
        $OrderItem = new OrderItem();
94
        $OrderItem->setProductName('ポイント値引')
95
            ->setPrice($priceOfUsePoint)
96
            ->setPriceIncTax($priceOfUsePoint)
97
            ->setTaxRate(8)
98
            ->setQuantity(1)
99
            ->setOrderItemType($DiscountType)
100
            ->setTaxDisplayType($TaxInclude)
101
            ->setTaxType($Taxion)
102
            ->setOrder($itemHolder);
103
        $itemHolder->addItem($OrderItem);
104
        return ProcessResult::success();
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
105
    }
106
107
    /**
108
     * 既存のポイント明細を削除する.
109
     *
110
     * @param ItemHolderInterface $itemHolder
111
     */
112
    protected function removePointDiscountItem(ItemHolderInterface $itemHolder)
113
    {
114
        foreach ($itemHolder->getItems() as $item) {
115
            if ($item->isDiscount() && $item->getProductName() == 'ポイント値引') {
116
                $itemHolder->removeOrderItem($item);
117
                $this->entityManager->remove($item);
118
            }
119
        }
120
    }
121
122
    /**
123
     * 利用ポイントを単価に換算する.
124
     *
125
     * @param integer $usePoint 利用ポイント
126
     * @return integer
127
     */
128
    protected function usePointToPrice($usePoint)
129
    {
130
        return ($usePoint * $this->BaseInfo->getPointConversionRate()) * -1;
131
    }
132
}
133