Failed Conditions
Push — experimental/3.1 ( 965511...751c7a )
by chihiro
21s
created

ItemCollection::getDeliveryFees()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Service\PurchaseFlow;
4
5
use Eccube\Entity\Master\OrderItemType;
6
use Eccube\Entity\ItemInterface;
7
use Eccube\Entity\Order;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\Common\Collections\Collection;
10
11
class ItemCollection extends ArrayCollection
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
12
{
13
    protected $type;
14
15
    public function __construct($Items, $type = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
16
    {
17
        $this->type = is_null($type) ? Order::class : $type;
18
19
        if ($Items instanceof Collection) {
20
            $Items = $Items->toArray();
21
        }
22
        parent::__construct($Items);
23
    }
24
25
    public function reduce(\Closure $func, $initial = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
26
    {
27
        return array_reduce($this->toArray(), $func, $initial);
28
    }
29
30
    // 明細種別ごとに返すメソッド作る
31
    public function getProductClasses()
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
32
    {
33
        return $this->filter(
34
            function (ItemInterface $ShipmentItem) {
35
                return $ShipmentItem->isProduct();
36
            });
37
    }
38
39
    public function getDeliveryFees()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
40
    {
41
        return $this->filter(
42
            function (ItemInterface $ShipmentItem) {
43
                return $ShipmentItem->isDeliveryFee();
44
            });
45
    }
46
47
    public function getCharges()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
48
    {
49
        return $this->filter(
50
            function (ItemInterface $ShipmentItem) {
51
                return $ShipmentItem->isCharge();
52
            });
53
    }
54
55
    public function getDiscounts()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
56
    {
57
        return $this->filter(
58
            function (ItemInterface $ShipmentItem) {
59
                return $ShipmentItem->isDiscount();
60
            });
61
    }
62
63
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productName" missing
Loading history...
64
     * 同名の明細が存在するかどうか.
65
     *
66
     * TODO 暫定対応. 本来は明細種別でチェックする.
67
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
68 View Code Duplication
    public function hasProductByName($productName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $ShipmentItems = $this->filter(
71
            function (ItemInterface $ShipmentItem) use ($productName) {
72
                /* @var ShipmentItem $ShipmentItem */
73
                return $ShipmentItem->getProductName() == $productName;
74
            });
75
76
        return !$ShipmentItems->isEmpty();
77
    }
78
79
    /**
80
     * 指定した受注明細区分の明細が存在するかどうか.
81
     *
82
     * @param OrderItemType $OrderItemType 受注区分
83
     *
84
     * @return bool
85
     */
86 View Code Duplication
    public function hasItemByOrderItemType($OrderItemType)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $filteredItems = $this->filter(function (ItemInterface $ShipmentItem) use ($OrderItemType) {
89
            /* @var ShipmentItem $ShipmentItem */
90
            return $ShipmentItem->getOrderItemType() && $ShipmentItem->getOrderItemType()->getId() == $OrderItemType->getId();
91
        });
92
93
        return !$filteredItems->isEmpty();
94
    }
95
96
    public function getType()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
97
    {
98
        return $this->type;
99
    }
100
101
    public function sort()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
102
    {
103
        $Items = $this->toArray();
104
        usort($Items, function (ItemInterface $a, ItemInterface $b) {
105
            if ($a->getOrderItemType() === $b->getOrderItemType()) {
106
                return ($a->getId() < $b->getId()) ? -1 : 1;
107
            } elseif ($a->isProduct()) {
108
                return -1;
109
            } elseif ($a->isDeliveryFee()) {
110
                if ($b->isProduct()) {
111
                    return 1;
112
                }
113
114
                return -1;
115
            } elseif ($a->isCharge()) {
116
                if ($b->isDeliveryFee() || $b->isProduct()) {
117
                    return 1;
118
                }
119
120
                return -1;
121
            } elseif ($a->isDiscount()) {
122
                if (!$b->isTax()) {
123
                    return 1;
124
                }
125
126
                return -1;
127
            } elseif ($a->isTax()) {
128
                return 1;
129
            }
130
131
            return 0;
132
        });
133
134
        return new self($Items);
135
    }
136
}
137