Failed Conditions
Push — fix-message-id-run-test ( 4fe74b...be7b70 )
by Kiyotaka
15:13 queued 09:02
created

DeliveryFeePreprocessor::containsDeliveryFeeItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
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 Doctrine\ORM\EntityManagerInterface;
17
use Eccube\Entity\BaseInfo;
18
use Eccube\Entity\DeliveryFee;
19
use Eccube\Entity\ItemHolderInterface;
20
use Eccube\Entity\Master\OrderItemType;
21
use Eccube\Entity\Master\TaxDisplayType;
22
use Eccube\Entity\Master\TaxType;
23
use Eccube\Entity\Order;
24
use Eccube\Entity\OrderItem;
25
use Eccube\Entity\Shipping;
26
use Eccube\Repository\BaseInfoRepository;
27
use Eccube\Repository\DeliveryFeeRepository;
28
use Eccube\Repository\TaxRuleRepository;
29
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
30
use Eccube\Service\PurchaseFlow\PurchaseContext;
31
32
/**
33
 * 送料明細追加.
34
 */
35
class DeliveryFeePreprocessor implements ItemHolderPreprocessor
36
{
37
    /** @var BaseInfo */
38
    protected $BaseInfo;
39
40
    /**
41
     * @var EntityManagerInterface
42
     */
43
    protected $entityManager;
44
45
    /**
46
     * @var TaxRuleRepository
47
     */
48
    protected $taxRuleRepository;
49
50
    /**
51
     * @var DeliveryFeeRepository
52
     */
53
    protected $deliveryFeeRepository;
54
55
    /**
56
     * DeliveryFeePreprocessor constructor.
57
     *
58
     * @param BaseInfoRepository $baseInfoRepository
59
     * @param EntityManagerInterface $entityManager
60
     * @param TaxRuleRepository $taxRuleRepository
61
     * @param DeliveryFeeRepository $deliveryFeeRepository
62
     */
63
    public function __construct(
64
        BaseInfoRepository $baseInfoRepository,
65
        EntityManagerInterface $entityManager,
66
        TaxRuleRepository $taxRuleRepository,
67
        DeliveryFeeRepository $deliveryFeeRepository
68
    ) {
69
        $this->BaseInfo = $baseInfoRepository->get();
70
        $this->entityManager = $entityManager;
71
        $this->taxRuleRepository = $taxRuleRepository;
72
        $this->deliveryFeeRepository = $deliveryFeeRepository;
73
    }
74
75
    /**
76
     * @param ItemHolderInterface $itemHolder
77
     * @param PurchaseContext $context
78
     *
79
     * @throws \Doctrine\ORM\NoResultException
80
     */
81
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
82
    {
83
        if ($this->containsDeliveryFeeItem($itemHolder) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
84
            $this->addDeliveryFeeItem($itemHolder);
85
        }
86
    }
87
88
    /**
89
     * @param ItemHolderInterface $itemHolder
90
     *
91
     * @return bool
92
     */
93
    private function containsDeliveryFeeItem(ItemHolderInterface $itemHolder)
94
    {
95
        foreach ($itemHolder->getItems() as $item) {
96
            if ($item->isDeliveryFee()) {
97
                return true;
98
            }
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * @param ItemHolderInterface $itemHolder
106
     *
107
     * @throws \Doctrine\ORM\NoResultException
108
     */
109
    private function addDeliveryFeeItem(ItemHolderInterface $itemHolder)
110
    {
111
        $DeliveryFeeType = $this->entityManager
112
            ->find(OrderItemType::class, OrderItemType::DELIVERY_FEE);
113
        $TaxInclude = $this->entityManager
114
            ->find(TaxDisplayType::class, TaxDisplayType::INCLUDED);
115
        $Taxion = $this->entityManager
116
            ->find(TaxType::class, TaxType::TAXATION);
117
118
        /** @var Order $Order */
119
        $Order = $itemHolder;
120
        /* @var Shipping $Shipping */
121
        foreach ($Order->getShippings() as $Shipping) {
122
            // 送料の計算
123
            $deliveryFeeProduct = 0;
124
            if ($this->BaseInfo->isOptionProductDeliveryFee()) {
125
                /** @var OrderItem $orderItem */
126
                foreach ($Shipping->getOrderItems() as $orderItem) {
127
                    if (!$orderItem->isProduct()) {
128
                        continue;
129
                    }
130
                    $deliveryFeeProduct += $orderItem->getProductClass()->getDeliveryFee() * $orderItem->getQuantity();
131
                }
132
            }
133
134
            /** @var DeliveryFee $DeliveryFee */
135
            $DeliveryFee = $this->deliveryFeeRepository->findOneBy([
136
                'Delivery' => $Shipping->getDelivery(),
137
                'Pref' => $Shipping->getPref(),
138
            ]);
139
140
            $Shipping->setShippingDeliveryFee($DeliveryFee->getFee() + $deliveryFeeProduct);
141
            $Shipping->setFeeId($DeliveryFee->getId());
142
143
            $OrderItem = new OrderItem();
144
            $OrderItem->setProductName($DeliveryFeeType->getName())
145
                ->setPrice($Shipping->getShippingDeliveryFee())
146
                ->setQuantity(1)
147
                ->setOrderItemType($DeliveryFeeType)
148
                ->setShipping($Shipping)
149
                ->setOrder($itemHolder)
0 ignored issues
show
Documentation introduced by
$itemHolder is of type object<Eccube\Entity\ItemHolderInterface>, but the function expects a null|object<Eccube\Entity\Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
                ->setTaxDisplayType($TaxInclude)
151
                ->setTaxType($Taxion);
152
153
            $itemHolder->addItem($OrderItem);
154
            $Shipping->addOrderItem($OrderItem);
155
        }
156
    }
157
}
158