Completed
Pull Request — experimental/sf (#3416)
by
unknown
54:55
created

ItemCollection::getProductClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
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;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Eccube\Entity\ItemInterface;
19
use Eccube\Entity\Master\OrderItemType;
20
use Eccube\Entity\Order;
21
22
class ItemCollection extends ArrayCollection
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
23
{
24
    protected $type;
25
26 323
    public function __construct($Items, $type = null)
27
    {
28 323
        $this->type = is_null($type) ? Order::class : $type;
29
30 323
        if ($Items instanceof Collection) {
31 323
            $Items = $Items->toArray();
32
        }
33 323
        parent::__construct($Items);
34
    }
35
36 293
    public function reduce(\Closure $func, $initial = null)
37
    {
38 293
        return array_reduce($this->toArray(), $func, $initial);
39
    }
40
41
    // 明細種別ごとに返すメソッド作る
42 288
    public function getProductClasses()
0 ignored issues
show
introduced by
You must use "/**" style comments for a function comment
Loading history...
43
    {
44 288
        return $this->filter(
45 288
            function (ItemInterface $OrderItem) {
46 284
                return $OrderItem->isProduct();
47 288
            });
48
    }
49
50 288
    public function getDeliveryFees()
51
    {
52 288
        return $this->filter(
53 288
            function (ItemInterface $OrderItem) {
54 284
                return $OrderItem->isDeliveryFee();
55 288
            });
56
    }
57
58 288
    public function getCharges()
59
    {
60 288
        return $this->filter(
61 288
            function (ItemInterface $OrderItem) {
62 284
                return $OrderItem->isCharge();
63 288
            });
64
    }
65
66 288
    public function getDiscounts()
67
    {
68 288
        return $this->filter(
69 288
            function (ItemInterface $OrderItem) {
70 284
                return $OrderItem->isDiscount() || $OrderItem->isPoint();
71 288
            });
72
    }
73
74
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$productName" missing
Loading history...
75
     * 同名の明細が存在するかどうか.
76
     *
77
     * TODO 暫定対応. 本来は明細種別でチェックする.
78
     */
0 ignored issues
show
introduced by
Missing @return tag in function comment
Loading history...
79 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...
80
    {
81
        $OrderItems = $this->filter(
82
            function (ItemInterface $OrderItem) use ($productName) {
83
                /* @var OrderItem $OrderItem */
84
                return $OrderItem->getProductName() == $productName;
85
            });
86
87
        return !$OrderItems->isEmpty();
88
    }
89
90
    /**
91
     * 指定した受注明細区分の明細が存在するかどうか.
92
     *
93
     * @param OrderItemType $OrderItemType 受注区分
94
     *
95
     * @return bool
96
     */
97 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...
98
    {
99 1
        $filteredItems = $this->filter(function (ItemInterface $OrderItem) use ($OrderItemType) {
100
            /* @var OrderItem $OrderItem */
101 1
            return $OrderItem->getOrderItemType() && $OrderItem->getOrderItemType()->getId() == $OrderItemType->getId();
102 1
        });
103
104 1
        return !$filteredItems->isEmpty();
105
    }
106
107
    public function getType()
108
    {
109
        return $this->type;
110
    }
111
112 323
    public function sort()
113
    {
114 323
        $Items = $this->toArray();
115 323
        usort($Items, function (ItemInterface $a, ItemInterface $b) {
116 234
            if ($a->getOrderItemType() === $b->getOrderItemType()) {
117 39
                return ($a->getId() < $b->getId()) ? -1 : 1;
118 226
            } elseif ($a->isProduct()) {
119 209
                return -1;
120 176
            } elseif ($a->isDeliveryFee()) {
121 163
                if ($b->isProduct()) {
122 11
                    return 1;
123
                }
124
125 153
                return -1;
126 166
            } elseif ($a->isCharge()) {
127 153
                if ($b->isDeliveryFee() || $b->isProduct()) {
128 3
                    return 1;
129
                }
130
131 153
                return -1;
132 15
            } elseif ($a->isDiscount() || $a->isPoint()) {
133 2
                if ($b->isDiscount()) {
134
                    return -1;
135
                }
136
137 2
                if ($b->isPoint()) {
138
                    return 1;
139
                }
140
141 2
                if (!$b->isTax()) {
142 2
                    return 1;
143
                }
144
145
                return -1;
146 13
            } elseif ($a->isTax()) {
147
                return 1;
148
            }
149
150 13
            return 0;
151 323
        });
152
153 323
        return new self($Items);
154
    }
155
}
156