Failed Conditions
Pull Request — experimental/sf (#3247)
by Kiyotaka
114:07 queued 103:28
created

PaymentValidator::getPayments()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 7
cts 7
cp 1
crap 3
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\Common\Collections\ArrayCollection;
17
use Eccube\Entity\Delivery;
18
use Eccube\Entity\ItemHolderInterface;
19
use Eccube\Entity\Master\SaleType;
20
use Eccube\Entity\Payment;
21
use Eccube\Repository\DeliveryRepository;
22
use Eccube\Service\PurchaseFlow\PurchaseContext;
23
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
24
25
/**
26
 * 支払い方法が一致しない明細がないかどうか.
27
 */
28
class PaymentValidator extends ItemHolderValidator
29
{
30
    /**
31
     * @var DeliveryRepository
32
     */
33
    protected $deliveryRepository;
34
35
    /**
36
     * PaymentProcessor constructor.
37
     *
38
     * @param DeliveryRepository $deliveryRepository
39
     */
40 163
    public function __construct(DeliveryRepository $deliveryRepository)
41
    {
42 163
        $this->deliveryRepository = $deliveryRepository;
43
    }
44
45 82
    protected function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
46
    {
47
        // 明細の個数が1以下の場合はOK
48 82
        if (count($itemHolder->getItems()) <= 1) {
49 80
            return;
50
        }
51
52
        // a, ab, c
53 17
        $i = 0;
54 17
        $paymentIds = [];
55 17
        foreach ($itemHolder->getItems() as $item) {
56 17
            if (false === $item->isProduct()) {
57
                continue;
58
            }
59 17
            $Deliveries = $this->getDeliveries($item->getProductClass()->getSaleType());
60 17
            $Payments = $this->getPayments($Deliveries);
61
62 17
            $ids = [];
63 17
            foreach ($Payments as $Payment) {
64 17
                $ids[] = $Payment->getId();
65
            }
66 17
            if ($i === 0) {
67 17
                $paymentIds = $ids;
68 17
                ++$i;
69 17
                continue;
70
            }
71
72 17
            $paymentIds = array_intersect($paymentIds, $ids);
73
        }
74
75
        // 共通項がなければエラー
76 17
        if (empty($paymentIds)) {
77 1
            $this->throwInvalidItemException('paymentprocessor.label.different_payment_method');
78
        }
79
    }
80
81 17
    private function getDeliveries(SaleType $SaleType)
82
    {
83 17
        $Deliveries = $this->deliveryRepository->findBy(
84
            [
85 17
                'SaleType' => $SaleType,
86
                'visible' => true,
87
            ]
88
        );
89
90 17
        return $Deliveries;
91
    }
92
93
    /**
94
     * @param Delivery[] $Deliveries
95
     *
96
     * @return ArrayCollection|Payment[]
97
     */
98 17
    private function getPayments($Deliveries)
99
    {
100 17
        $Payments = new ArrayCollection();
101 17
        foreach ($Deliveries as $Delivery) {
102 17
            $PaymentOptions = $Delivery->getPaymentOptions();
103 17
            foreach ($PaymentOptions as $PaymentOption) {
104 17
                $Payments->add($PaymentOption->getPayment());
105
            }
106
        }
107
108 17
        return $Payments;
109
    }
110
}
111