Completed
Pull Request — experimental/sf (#3293)
by
unknown
135:44 queued 125:34
created

PaymentRepository::findAllowedPayments()   C

Complexity

Conditions 13
Paths 34

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 13.0076

Importance

Changes 0
Metric Value
cc 13
nc 34
nop 3
dl 0
loc 45
ccs 27
cts 28
cp 0.9643
crap 13.0076
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Repository;
15
16
use Doctrine\ORM\Query;
17
use Eccube\Entity\Order;
18
use Eccube\Entity\Payment;
19
use Symfony\Bridge\Doctrine\RegistryInterface;
20
21
/**
22
 * PaymentRepository
23
 *
24
 * This class was generated by the Doctrine ORM. Add your own custom
25
 * repository methods below.
26
 */
27
class PaymentRepository extends AbstractRepository
28
{
29
    /**
30
     * PaymentRepository constructor.
31
     *
32
     * @param RegistryInterface $registry
33
     */
34 722
    public function __construct(RegistryInterface $registry)
35
    {
36 722
        parent::__construct($registry, Payment::class);
37
    }
38
39
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$id" missing
Loading history...
40
     * @deprecated 呼び出し元で制御する
41
     *
42
     * @param $id
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
43
     *
44
     * @return \Eccube\Entity\Payment|null|object
45
     */
46 8
    public function findOrCreate($id)
47
    {
48 8
        if ($id == 0) {
49 8
            $Payment = $this->findOneBy([], ['sort_no' => 'DESC']);
50
51 8
            $sortNo = 1;
52 8
            if ($Payment) {
53 8
                $sortNo = $Payment->getSortNo() + 1;
54
            }
55
56 8
            $Payment = new \Eccube\Entity\Payment();
57
            $Payment
58 8
                ->setSortNo($sortNo)
59 8
                ->setFixed(true)
60 8
                ->setVisible(true);
61
        } else {
62
            $Payment = $this->find($id);
63
        }
64
65 8
        return $Payment;
66
    }
67
68
    /**
69
     * @return array
70
     */
71 1
    public function findAllArray()
72
    {
73
        $query = $this
74 1
            ->getEntityManager()
75 1
            ->createQuery('SELECT p FROM Eccube\Entity\Payment p INDEX BY p.id');
76
        $result = $query
77 1
            ->getResult(Query::HYDRATE_ARRAY);
78
79 1
        return $result;
80
    }
81
82
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$delivery" missing
Loading history...
introduced by
Doc comment for parameter "$returnType" missing
Loading history...
83
     * 支払方法を取得
84
     * 条件によってはDoctrineのキャッシュが返されるため、arrayで結果を返すパターンも用意
85
     *
86
     * @param $delivery
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
87
     * @param $returnType true : Object、false: arrayが戻り値
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
88
     *
89
     * @return array
90
     */
91 58
    public function findPayments($delivery, $returnType = false)
92
    {
93 58
        $query = $this->createQueryBuilder('p')
94 58
            ->innerJoin('Eccube\Entity\PaymentOption', 'po', 'WITH', 'po.payment_id = p.id')
95 58
            ->where('po.Delivery = (:delivery) AND p.visible = true')
96 58
            ->orderBy('p.sort_no', 'DESC')
97 58
            ->setParameter('delivery', $delivery)
98 58
            ->getQuery();
99
100 58
        $query->expireResultCache(false);
101
102 58
        if ($returnType) {
103 52
            $payments = $query->getResult();
104
        } else {
105 6
            $payments = $query->getArrayResult();
106
        }
107
108 58
        return $payments;
109
    }
110
111
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
112
     * 共通の支払方法を取得
113
     *
114
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
115
     * @param bool $returnType
0 ignored issues
show
introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
116
     * @param Order $Order
0 ignored issues
show
introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
117
     *
118
     * @return array
119
     */
120 57
    public function findAllowedPayments($deliveries, $returnType = false, Order $Order = null)
121
    {
122 57
        $deliveries = array_unique($deliveries);
123 57
        $payments = [];
124 57
        $isFirstLoop = true;
125 57
        $totalPayment = 0;
126 57
        if (!is_null($Order)) {
127 51
            $totalPayment = $Order->getSubtotal();
128
        }
129 57
        foreach ($deliveries as $Delivery) {
130 56
            $paymentTmp = [];
131 56
            $p = $this->findPayments($Delivery, $returnType);
132 56
            if ($p == null) {
133
                continue;
134
            }
135 56
            foreach ($p as $payment) {
136 56
                if (empty($totalPayment)) {
137 56
                    $paymentTmp[$payment['id']] = $payment;
138 56
                    continue;
139
                }
140 50
                if (is_null($payment['rule_min'])
141 50
                    || $payment['rule_min'] <= $totalPayment
142
                ) {
143 50
                    if (is_null($payment['rule_max'])
144 50
                        || ($payment['rule_max'] && $payment['rule_max'] >= $totalPayment)
145
                    ) {
146 50
                        $paymentTmp[$payment['id']] = $payment;
147
                    }
148
                }
149
            }
150
151 56
            if ($isFirstLoop) {
152 56
                $payments = $paymentTmp;
153 56
                $isFirstLoop = false;
154
            } else {
155 4
                $payments = array_intersect_key($payments, $paymentTmp);
156
157 4
                if (count($payments) == 0) {
158 56
                    break;
159
                }
160
            }
161
        }
162
163 57
        return $payments;
164
    }
165
}
166