Completed
Pull Request — experimental/3.1 (#2688)
by
unknown
28:04
created

ShippingRepository   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 231
Duplicated Lines 60.61 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 9.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 140
loc 231
ccs 10
cts 109
cp 0.0917
rs 6.8539
c 1
b 0
f 0
wmc 54
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
F getQueryBuilderBySearchDataForAdmin() 140 201 53
A findShippingsProduct() 0 14 1

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ShippingRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ShippingRepository, and based on these observations, apply Extract Interface, too.

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 Eccube\Annotation\Repository;
28
use Eccube\Util\StringUtil;
29
30
/**
31
 * ShippingRepository
32
 *
33
 * This class was generated by the Doctrine ORM. Add your own custom
34
 * repository methods below.
35
 *
36
 * @Repository
37
 */
38
class ShippingRepository extends AbstractRepository
39
{
40
    /**
41
     *
42
     * @param  array        $searchData
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
43
     * @return QueryBuilder
44
     */
45
    public function getQueryBuilderBySearchDataForAdmin($searchData)
46
    {
47
        $qb = $this->createQueryBuilder('s');
48
49
        $qb->leftJoin('s.OrderItems', 'si')
50
            ->leftJoin('si.Order', 'o');
51
        // order_id_start
52 View Code Duplication
        if (isset($searchData['shipping_id_start']) && StringUtil::isNotBlank($searchData['shipping_id_start'])) {
53
            $qb
54
                ->andWhere('s.id >= :shipping_id_start')
55
                ->setParameter('shipping_id_start', $searchData['shipping_id_start']);
56
        }
57
        // multi
58 View Code Duplication
        if (isset( $searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
59
            $multi = preg_match('/^\d+$/', $searchData['multi']) ? $searchData['multi'] : null;
60
            $qb
61
                ->andWhere('s.id = :multi OR s.name01 LIKE :likemulti OR s.name02 LIKE :likemulti OR ' .
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
62
                           's.kana01 LIKE :likemulti OR s.kana02 LIKE :likemulti OR s.company_name LIKE :likemulti')
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 16 spaces, but found 27.
Loading history...
63
                ->setParameter('multi', $multi)
64
                ->setParameter('likemulti', '%' . $searchData['multi'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
65
        }
66
67
        // shipping_id_end
68 View Code Duplication
        if (isset($searchData['shipping_id_end']) && StringUtil::isNotBlank($searchData['shipping_id_end'])) {
69
            $qb
70
                ->andWhere('s.id <= :shipping_id_end')
71
                ->setParameter('shipping_id_end', $searchData['shipping_id_end']);
72
        }
73
74
        // order_id
75 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
76
            $qb
77
                ->andWhere('o.id = :order_id')
78
                ->setParameter('order_id', $searchData['order_id']);
79
        }
80
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
81
        // order_code
82 View Code Duplication
        if (isset($searchData['order_code']) && StringUtil::isNotBlank($searchData['order_code'])) {
83
            $qb
84
                ->andWhere('o.code LIKE :order_code')
85
                ->setParameter('order_code', "%{$searchData['order_code']}%");
86
        }
87
88
        // order status
89
        if (isset($searchData['order_status']) && count($searchData['order_status'])) {
90
            $s = $searchData['order_status'];
0 ignored issues
show
Unused Code introduced by
$s is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
            $qb
92
                ->andWhere($qb->expr()->in('o.OrderStatus', ':order_status'))
93
                ->setParameter('order_status', $searchData['order_status']);
94
        }
95
        // shipping status
96
        if (isset($searchData['shipping_status']) && count($searchData['shipping_status'])) {
97
            $qb
98
                ->andWhere($qb->expr()->in('s.ShippingStatus', ':shipping_status'))
99
                ->setParameter('shipping_status', $searchData['shipping_status']);
100
        }
101
        // name
102 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
103
            $qb
104
                ->andWhere('CONCAT(s.name01, s.name02) LIKE :name')
105
                ->setParameter('name', '%' . $searchData['name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
106
        }
107
108
        // kana
109 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
110
            $qb
111
                ->andWhere('CONCAT(s.kana01, s.kana02) LIKE :kana')
112
                ->setParameter('kana', '%' . $searchData['kana'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
113
        }
114
115
        // order_name
116 View Code Duplication
        if (isset($searchData['order_name']) && StringUtil::isNotBlank($searchData['order_name'])) {
117
            $qb
118
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :order_name')
119
                ->setParameter('order_name', '%' . $searchData['order_name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
120
        }
121
122
        // order_kana
123 View Code Duplication
        if (isset($searchData['order_kana']) && StringUtil::isNotBlank($searchData['order_kana'])) {
124
            $qb
125
                ->andWhere('CONCAT(o.kana01, s.kana02) LIKE :order_kana')
126
                ->setParameter('kana', '%' . $searchData['order_kana'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
127
        }
128
129
        // order_email
130 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
131
            $qb
132
                ->andWhere('o.email like :email')
133
                ->setParameter('email', '%' . $searchData['email'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
134
        }
135
136
        // tel
137 View Code Duplication
        if (isset($searchData['tel']) && StringUtil::isNotBlank($searchData['tel'])) {
138
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['tel']);
139
            $qb
140
                ->andWhere('CONCAT(s.tel01, s.tel02, s.tel03) LIKE :tel')
141
                ->setParameter('tel', '%' . $tel . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
142
        }
143
144
        // payment
145 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
146
            $payments = array();
147
            foreach ($searchData['payment'] as $payment) {
148
                $payments[] = $payment->getId();
149
            }
150
            $qb
151
                ->leftJoin('o.Payment', 'p')
152
                ->andWhere($qb->expr()->in('p.id', ':payments'))
153
                ->setParameter('payments', $payments);
154
        }
155
156
        // oreder_date
157 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
158
            $date = $searchData['order_date_start'];
159
            $qb
160
                ->andWhere('o.order_date >= :order_date_start')
161
                ->setParameter('order_date_start', $date);
162
        }
163 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
164
            $date = clone $searchData['order_date_end'];
165
            $date = $date
166
                ->modify('+1 days');
167
            $qb
168
                ->andWhere('o.order_date < :order_date_end')
169
                ->setParameter('order_date_end', $date);
170
        }
171
172
        // shipping_delivery_date
173 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
174
            $date = $searchData['shipping_delivery_date_start'];
175
            $qb
176
                ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
177
                ->setParameter('shipping_delivery_date_start', $date);
178
        }
179 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
180
            $date = clone $searchData['shipping_delivery_date_end'];
181
            $date = $date
182
                ->modify('+1 days');
183
            $qb
184
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
185
                ->setParameter('shipping_delivery_date_end', $date);
186
        }
187
188
        // shipping_date
189 View Code Duplication
        if (!empty($searchData['shipping_date_start']) && $searchData['shipping_date_start']) {
190
            $date = $searchData['shipping_date_start'];
191
            $qb
192
                ->andWhere('s.shipping_date >= :shipping_date_start')
193
                ->setParameter('shipping_date_start', $date);
194
        }
195 View Code Duplication
        if (!empty($searchData['shipping_date_end']) && $searchData['shipping_date_end']) {
196
            $date = clone $searchData['shipping_date_end'];
197
            $date = $date
198
                ->modify('+1 days');
199
            $qb
200
                ->andWhere('s.shipping_date < :shipping_date_end')
201
                ->setParameter('shipping_date_end', $date);
202
        }
203
204
205
        // update_date
206 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
207
            $date = $searchData['update_date_start'];
208
            $qb
209
                ->andWhere('s.update_date >= :update_date_start')
210
                ->setParameter('update_date_start', $date);
211
        }
212 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
213
            $date = clone $searchData['update_date_end'];
214
            $date = $date
215
                ->modify('+1 days');
216
            $qb
217
                ->andWhere('s.update_date < :update_date_end')
218
                ->setParameter('update_date_end', $date);
219
        }
220
221
        // payment_total
222 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
223
            $qb
224
                ->andWhere('o.payment_total >= :payment_total_start')
225
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
226
        }
227 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
228
            $qb
229
                ->andWhere('o.payment_total <= :payment_total_end')
230
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
231
        }
232
233
        // buy_product_name
234 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
235
            $qb
236
                ->andWhere('si.product_name LIKE :buy_product_name')
237
                ->setParameter('buy_product_name', '%' . $searchData['buy_product_name'] . '%');
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
238
        }
239
240
        // Order By
241
        $qb->orderBy('s.update_date', 'DESC');
242
        $qb->addorderBy('s.id', 'DESC');
243
244
        return $qb;
245 1
    }
246
247 1
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Order" missing
Loading history...
introduced by
Doc comment for parameter "$productClass" missing
Loading history...
248 1
     * 同一商品のお届け先情報を取得
249 1
     *
250 1
     * @param $Order
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
251 1
     * @return array
252 1
     */
253 1
    public function findShippingsProduct($Order, $productClass)
254 1
    {
255
        $shippings = $this->createQueryBuilder('s')
256 1
            ->innerJoin('Eccube\Entity\OrderItem', 'si', 'WITH', 'si.Shipping = s.id')
257
            ->where('si.Order = (:order)')
258
            ->andWhere('si.ProductClass = (:productClass)')
259
            ->setParameter('order', $Order)
260
            ->setParameter('productClass', $productClass)
261
            ->getQuery()
262
            ->getResult();
263
264
        return $shippings;
265
266
    }
267
268
}
269