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

AddPointProcessor::__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
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Service\PurchaseFlow\Processor;
25
26
use Doctrine\ORM\EntityManager;
27
use Eccube\Annotation\Inject;
28
use Eccube\Entity\BaseInfo;
29
use Eccube\Entity\ItemHolderInterface;
30
use Eccube\Entity\Master\OrderItemType;
31
use Eccube\Entity\Master\TaxDisplayType;
32
use Eccube\Entity\Master\TaxType;
33
use Eccube\Entity\Order;
34
use Eccube\Entity\OrderItem;
35
use Eccube\Entity\Shipping;
36
use Eccube\Service\PurchaseFlow\ItemHolderProcessor;
37
use Eccube\Service\PurchaseFlow\ProcessResult;
38
use Eccube\Service\PurchaseFlow\PurchaseContext;
39
40
/**
41
 * 加算ポイント.
42
 */
43
class AddPointProcessor implements ItemHolderProcessor
44
{
45
    /**
46
     * @Inject("orm.em")
47
     * @var EntityManager
48
     */
49
    protected $entityManager;
50
51
    /**
52
     * @var BaseInfo
53
     */
54
    protected $BaseInfo;
55
56
57
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$entityManager" missing
Loading history...
introduced by
Doc comment for parameter "$BaseInfo" missing
Loading history...
58
     * AddPointProcessor constructor.
59
     *
60
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
61
     */
62
    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...
63
    {
64
        $this->entityManager = $entityManager;
65
        $this->BaseInfo = $BaseInfo;
66
    }
67
68
    /**
69
     * @param ItemHolderInterface $itemHolder
70
     * @param PurchaseContext     $context
71
     *
72
     * @return ProcessResult
73
     */
74
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
75
    {
76
        $addPoint = 0;
77
        foreach ($itemHolder->getItems() as $item) {
78
            $rate = $item->getPointRate();
79
            if ($rate === null) {
80
                $rate = $this->BaseInfo->getBasicPointRate();
81
            }
82
            $addPoint += $this->priceToAddPoint($rate, $item->getPriceIncTax(), $item->getQuantity());
83
        }
84
        $itemHolder->setAddPoint($addPoint);
85
        return ProcessResult::success();
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
86
    }
87
88
    /**
89
     * 単価と数量から加算ポイントに換算する.
90
     *
91
     * @param integer $pointRate ポイント付与率(%)
92
     * @param integer $price 単価
93
     * @param integer $quantity 数量
94
     * @return integer additional point
95
     */
96
    protected function priceToAddPoint($pointRate, $price, $quantity)
97
    {
98
        return round($price * ($pointRate / 100)) * $quantity;
99
    }
100
}
101