Completed
Pull Request — master (#2026)
by
unknown
150:18 queued 143:21
created

PaymentRepository   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 17.89 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 22
loc 123
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 4

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findOrCreate() 3 15 3
A createPayment() 19 19 2
A findAllArray() 0 12 1
A findPayments() 0 20 2
B findAllowedPayments() 0 31 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 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
25
namespace Eccube\Repository;
26
27
use Doctrine\ORM\EntityRepository;
28
use Doctrine\ORM\Query;
29
30
/**
31
 * PaymentRepository
32
 *
33
 * This class was generated by the Doctrine ORM. Add your own custom
34
 * repository methods below.
35
 */
36
class PaymentRepository extends EntityRepository
37
{
38 11
    public function findOrCreate($id, $Creator = null)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
39
    {
40 11
        if ($id == 0) {
41 8
            $em = $this->getEntityManager();
42 8 View Code Duplication
            if ($Creator == null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
43 8
                $Creator = $em->getRepository('\Eccube\Entity\Member')->findOneBy(array(), array('rank' => 'DESC'));
44
            }
45
46 8
            $Payment = $this->createPayment($Creator);
47
        } else {
48 3
            $Payment = $this->find($id);
49
        }
50
51 11
        return $Payment;
52
    }
53
54
    /**
55
     * create Payment
56
     * @param \Eccube\Entity\Member $Creator
57
     * @return \Eccube\Entity\Payment
58
     */
59 8 View Code Duplication
    private function createPayment($Creator)
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...
60
    {
61 8
        $PaymentOld = $this->findOneBy(array(), array('rank' => 'DESC'));
62
63 8
        $rank = 1;
64 8
        if ($PaymentOld) {
65 8
            $rank = $PaymentOld->getRank() + 1;
66
        }
67
68 8
        $Payment = new \Eccube\Entity\Payment();
69
        $Payment
70 8
            ->setRank($rank)
71 8
            ->setDelFlg(0)
72 8
            ->setFixFlg(1)
73 8
            ->setChargeFlg(1)
74 8
            ->setCreator($Creator);
75
76 8
        return $Payment;
77
    }
78
79 1
    public function findAllArray()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
80
    {
81
82
        $query = $this
83 1
            ->getEntityManager()
84 1
            ->createQuery('SELECT p FROM Eccube\Entity\Payment p INDEX BY p.id');
85
        $result = $query
86 1
            ->getResult(Query::HYDRATE_ARRAY);
87
88 1
        return $result;
89
90
    }
91
92
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$delivery" missing
Loading history...
introduced by
Doc comment for parameter "$returnType" missing
Loading history...
93
     * 支払方法を取得
94
     * 条件によってはDoctrineのキャッシュが返されるため、arrayで結果を返すパターンも用意
95
     *
96
     * @param $delivery
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
97
     * @param $returnType true : Object、false: arrayが戻り値
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
98
     * @return array
99
     */
100 52
    public function findPayments($delivery, $returnType = false)
101
    {
102
103 52
        $query = $this->createQueryBuilder('p')
104 52
            ->innerJoin('Eccube\Entity\PaymentOption', 'po', 'WITH', 'po.payment_id = p.id')
105 52
            ->where('po.Delivery = (:delivery)')
106 52
            ->orderBy('p.rank', 'DESC')
107 52
            ->setParameter('delivery', $delivery)
108 52
            ->getQuery();
109
110 52
        $query->expireResultCache(false);
111
112 52
        if ($returnType) {
113 35
            $payments = $query->getResult();
114
        } else {
115 49
            $payments = $query->getArrayResult();
116
        }
117
118 52
        return $payments;
119
    }
120
121
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$deliveries" missing
Loading history...
122
     * 共通の支払方法を取得
123
     *
124
     * @param $deliveries
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
125
     * @return array
126
     */
127 49
    public function findAllowedPayments($deliveries)
128
    {
129 49
        $payments = array();
130 49
        $i = 0;
131
132 49
        foreach ($deliveries as $Delivery) {
133 48
            $p = $this->findPayments($Delivery);
134
135 48
            if ($i != 0) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
136
137 5
                $arr = array();
138 5
                foreach ($p as $payment) {
139 5
                    foreach ($payments as $pay) {
140 5
                        if ($payment['id'] == $pay['id']) {
141 3
                            $arr[] = $payment;
142 5
                            break;
143
                        }
144
                    }
145
                }
146
147 5
                $payments = $arr;
148
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
149
            } else {
150 48
                $payments = $p;
151
            }
152 49
            $i++;
153
        }
154
155 49
        return $payments;
156
157
    }
158
}
159