Completed
Push — 4.0 ( b48f64...137622 )
by chihiro
20:21 queued 10s
created

Processor/PaymentChargePreprocessor.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Service\PurchaseFlow\ItemHolderPreprocessor;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Service\PurchaseFlow\PurchaseContext;
19
use Eccube\Entity\OrderItem;
20
use Eccube\Repository\Master\OrderItemTypeRepository;
21
use Eccube\Repository\Master\TaxDisplayTypeRepository;
22
use Eccube\Entity\Master\OrderItemType;
23
use Eccube\Entity\Master\TaxDisplayType;
24
use Eccube\Entity\Order;
25
use Eccube\Entity\Payment;
26
use Eccube\Repository\Master\TaxTypeRepository;
27
use Eccube\Entity\Master\TaxType;
28
29
class PaymentChargePreprocessor implements ItemHolderPreprocessor
30
{
31
    /**
32
     * @var OrderItemTypeRepository
33
     */
34
    protected $orderItemTypeRepository;
35
36
    /**
37
     * @var TaxDisplayTypeRepository
38
     */
39
    protected $taxDisplayTypeRepository;
40
41
    /**
42
     * @var TaxTypeRepository
43
     */
44
    protected $taxTypeRepository;
45
46
    /**
47
     * PaymentChargePreprocessor constructor.
48
     *
49
     * @param OrderItemTypeRepository $orderItemTypeRepository
50
     * @param TaxDisplayTypeRepository $taxDisplayTypeRepository
51
     * @param TaxTypeRepository $taxTypeRepository
52
     */
53
    public function __construct(
54
        OrderItemTypeRepository $orderItemTypeRepository,
55
        TaxDisplayTypeRepository $taxDisplayTypeRepository,
56
        TaxTypeRepository $taxTypeRepository
57
    ) {
58
        $this->orderItemTypeRepository = $orderItemTypeRepository;
59
        $this->taxDisplayTypeRepository = $taxDisplayTypeRepository;
60
        $this->taxTypeRepository = $taxTypeRepository;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     *
66
     * @param ItemHolderInterface $itemHolder
67
     * @param PurchaseContext $context
68
     */
69
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
70
    {
71
        if (!$itemHolder instanceof Order) {
72
            return;
73
        }
74
        if (!$itemHolder->getPayment() instanceof Payment || !$itemHolder->getPayment()->getId()) {
75
            return;
76
        }
77
78
        foreach ($itemHolder->getItems() as $item) {
79
            if ($item->getProcessorName() == PaymentChargePreprocessor::class) {
80
                $item->setPrice($itemHolder->getPayment()->getCharge());
81
82
                return;
83
            }
84
        }
85
86
        $this->addChargeItem($itemHolder);
87
    }
88
89
    /**
90
     * Add charge item to item holder
91
     *
92
     * @param ItemHolderInterface $itemHolder
93
     */
94
    protected function addChargeItem(ItemHolderInterface $itemHolder)
95
    {
96
        /** @var Order $itemHolder */
97
        $OrderItemType = $this->orderItemTypeRepository->find(OrderItemType::CHARGE);
98
        $TaxDisplayType = $this->taxDisplayTypeRepository->find(TaxDisplayType::INCLUDED);
99
        $Taxation = $this->taxTypeRepository->find(TaxType::TAXATION);
100
        $item = new OrderItem();
101
        $item->setProductName($OrderItemType->getName())
102
            ->setQuantity(1)
103
            ->setPrice($itemHolder->getPayment()->getCharge())
104
            ->setOrderItemType($OrderItemType)
0 ignored issues
show
It seems like $OrderItemType defined by $this->orderItemTypeRepo...\OrderItemType::CHARGE) on line 97 can also be of type object; however, Eccube\Entity\OrderItem::setOrderItemType() does only seem to accept null|object<Eccube\Entity\Master\OrderItemType>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
105
            ->setOrder($itemHolder)
106
            ->setTaxDisplayType($TaxDisplayType)
0 ignored issues
show
It seems like $TaxDisplayType defined by $this->taxDisplayTypeRep...xDisplayType::INCLUDED) on line 98 can also be of type object; however, Eccube\Entity\OrderItem::setTaxDisplayType() does only seem to accept null|object<Eccube\Entity\Master\TaxDisplayType>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
107
            ->setTaxType($Taxation)
108
            ->setProcessorName(PaymentChargePreprocessor::class);
109
        $itemHolder->addItem($item);
110
    }
111
}
112