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

PaymentProcessor::validate()   C

Complexity

Conditions 7
Paths 13

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 13
nop 2
dl 0
loc 35
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube\Service\PurchaseFlow\Processor;
25
26
use Doctrine\Common\Collections\ArrayCollection;
27
use Eccube\Application;
28
use Eccube\Entity\Delivery;
29
use Eccube\Entity\ItemHolderInterface;
30
use Eccube\Entity\Master\ProductType;
31
use Eccube\Service\PurchaseFlow\ItemValidateException;
32
use Eccube\Service\PurchaseFlow\ValidatableItemHolderProcessor;
33
use Eccube\Service\PurchaseFlow\PurchaseContext;
34
35
/**
36
 * 支払い方法が一致しない明細がないかどうか.
37
 */
38
class PaymentProcessor extends ValidatableItemHolderProcessor
39
{
40
    /**
41
     * @var Application
42
     */
43
    private $app;
44
45
    /**
46
     * DeliveryFeeProcessor constructor.
47
     *
48
     * @param Application $app
49
     */
50
    public function __construct(Application $app)
51
    {
52
        $this->app = $app;
53
    }
54
55
    protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
56
    {
57
        // 明細の個数が1以下の場合はOK
58
        if (count($itemHolder->getItems()) <= 1) {
59
            return;
60
        }
61
62
        // a, ab, c
63
        $i = 0;
64
        $paymentIds = [];
65
        foreach ($itemHolder->getItems() as $item) {
66
            if (false === $item->isProduct()) {
67
                continue;
68
            }
69
            $Deliveries = $this->getDeliveries($item->getProductClass()->getProductType());
70
            $Payments = $this->getPayments($Deliveries);
71
72
            $ids = [];
73
            foreach ($Payments as $Payment) {
74
                $ids[] = $Payment->getId();
75
            }
76
            if ($i === 0) {
77
                $paymentIds = $ids;
78
                ++$i;
79
                continue;
80
            }
81
82
            $paymentIds = array_intersect($paymentIds, $ids);
83
        }
84
85
        // 共通項がなければエラー
86
        if (empty($paymentIds)) {
87
            throw new ItemValidateException('支払い方法が異なる');
88
        }
89
    }
90
91
    private function getDeliveries(ProductType $ProductType)
92
    {
93
        $Deliveries = $this->app['orm.em']->getRepository(Delivery::class)
94
            ->findBy(
95
                [
96
                    'ProductType' => $ProductType,
97
                ]
98
            );
99
100
        return $Deliveries;
101
    }
102
103
    private function getPayments($Deliveries)
104
    {
105
        $Payments = new ArrayCollection();
106
        foreach ($Deliveries as $Delivery) {
107
            $PaymentOptions = $Delivery->getPaymentOptions();
108
            foreach ($PaymentOptions as $PaymentOption) {
109
                $Payments->add($PaymentOption->getPayment());
110
            }
111
        }
112
113
        return $Payments;
114
    }
115
}
116