Completed
Pull Request — 4.0 (#4223)
by Kentaro
04:57
created

DeliveryFeeFreePreprocessor::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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\Repository\BaseInfoRepository;
19
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
20
use Eccube\Service\PurchaseFlow\PurchaseContext;
21
22
/**
23
 * 送料無料条件を適用します.
24
 */
25
class DeliveryFeeFreePreprocessor implements ItemHolderPreprocessor
26
{
27
    /**
28
     * @var BaseInfo
29
     */
30
    protected $BaseInfo;
31
32
    /**
33
     * DeliveryFeeProcessor constructor.
34
     *
35
     * @param BaseInfoRepository $baseInfoRepository
36
     */
37 135
    public function __construct(BaseInfoRepository $baseInfoRepository)
38
    {
39 135
        $this->BaseInfo = $baseInfoRepository->get();
40
    }
41
42
    /**
43
     * @param ItemHolderInterface $itemHolder
44
     * @param PurchaseContext     $context
45
     */
46 81
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
47
    {
48 81
        $isDeliveryFree = false;
49
50 81
        if ($this->BaseInfo->getDeliveryFreeAmount()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->BaseInfo->getDeliveryFreeAmount() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51 1
            if ($this->BaseInfo->getDeliveryFreeAmount() <= $itemHolder->getTotal()) {
52
                // 送料無料(金額)を超えている
53 1
                $isDeliveryFree = true;
54
            }
55
        }
56
57 81
        if ($this->BaseInfo->getDeliveryFreeQuantity()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->BaseInfo->getDeliveryFreeQuantity() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
58 1
            if ($this->BaseInfo->getDeliveryFreeQuantity() <= $itemHolder->getQuantity()) {
59
                // 送料無料(個数)を超えている
60 1
                $isDeliveryFree = true;
61
            }
62
        }
63
64
        // 送料無料条件に合致した場合は、送料明細の個数を0に設定
65 81
        if ($isDeliveryFree) {
66 2
            $items = $itemHolder->getItems();
67 2
            foreach ($items as $item) {
68 2
                if ($item->isDeliveryFee()) {
69 2
                    $item->setQuantity(0);
70
                }
71
            }
72
        }
73
    }
74
75
    public function __toString()
76
    {
77
        return get_class($this);
78
    }
79
}
80