Failed Conditions
Push — experimental/sf ( 069d05...1f2df0 )
by chihiro
157:56 queued 150:35
created

ShippingRepository   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 230
Duplicated Lines 58.7 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 10.71%

Importance

Changes 0
Metric Value
dl 135
loc 230
rs 6.96
c 0
b 0
f 0
ccs 12
cts 112
cp 0.1071
wmc 53
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findShippingsProduct() 0 13 1
F getQueryBuilderBySearchDataForAdmin() 135 194 51

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
/*
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 Eccube\Entity\Shipping;
17
use Eccube\Util\StringUtil;
18
use Symfony\Bridge\Doctrine\RegistryInterface;
19
use Doctrine\ORM\QueryBuilder;
20
21
/**
22
 * ShippingRepository
23
 *
24
 * This class was generated by the Doctrine ORM. Add your own custom
25
 * repository methods below.
26
 */
27
class ShippingRepository extends AbstractRepository
28
{
29 108
    public function __construct(RegistryInterface $registry)
30
    {
31 108
        parent::__construct($registry, Shipping::class);
32
    }
33
34
    /**
35
     * Build query
36
     *
37
     * @param  array $searchData
38
     *
39
     * @return QueryBuilder
40
     */
41
    public function getQueryBuilderBySearchDataForAdmin($searchData)
42
    {
43
        $qb = $this->createQueryBuilder('s');
44
45
        $qb->leftJoin('s.OrderItems', 'si')
46
            ->leftJoin('si.Order', 'o');
47
        // order_id_start
48 View Code Duplication
        if (isset($searchData['shipping_id_start']) && StringUtil::isNotBlank($searchData['shipping_id_start'])) {
49
            $qb
50
                ->andWhere('s.id >= :shipping_id_start')
51
                ->setParameter('shipping_id_start', $searchData['shipping_id_start']);
52
        }
53
        // multi
54 View Code Duplication
        if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
55
            $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null;
56
            $qb
57
                ->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...
58
                           '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...
59
                ->setParameter('multi', $multi)
60
                ->setParameter('likemulti', '%'.$searchData['multi'].'%');
61
        }
62
63
        // shipping_id_end
64 View Code Duplication
        if (isset($searchData['shipping_id_end']) && StringUtil::isNotBlank($searchData['shipping_id_end'])) {
65
            $qb
66
                ->andWhere('s.id <= :shipping_id_end')
67
                ->setParameter('shipping_id_end', $searchData['shipping_id_end']);
68
        }
69
70
        // order_id
71 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
72
            $qb
73
                ->andWhere('o.id = :order_id')
74
                ->setParameter('order_id', $searchData['order_id']);
75
        }
76
77
        // order_no
78
        if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) {
79
            $qb
80
                ->andWhere('o.order_no LIKE :order_no')
81
                ->setParameter('order_no', "%{$searchData['order_no']}%");
82
        }
83
84
        // order status
85
        if (isset($searchData['order_status']) && count($searchData['order_status'])) {
86
            $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...
87
            $qb
88
                ->andWhere($qb->expr()->in('o.OrderStatus', ':order_status'))
89
                ->setParameter('order_status', $searchData['order_status']);
90
        }
91
        // name
92 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
93
            $qb
94
                ->andWhere('CONCAT(s.name01, s.name02) LIKE :name')
95
                ->setParameter('name', '%'.$searchData['name'].'%');
96
        }
97
98
        // kana
99 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
100
            $qb
101
                ->andWhere('CONCAT(s.kana01, s.kana02) LIKE :kana')
102
                ->setParameter('kana', '%'.$searchData['kana'].'%');
103
        }
104
105
        // order_name
106 View Code Duplication
        if (isset($searchData['order_name']) && StringUtil::isNotBlank($searchData['order_name'])) {
107
            $qb
108
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :order_name')
109
                ->setParameter('order_name', '%'.$searchData['order_name'].'%');
110
        }
111
112
        // order_kana
113 View Code Duplication
        if (isset($searchData['order_kana']) && StringUtil::isNotBlank($searchData['order_kana'])) {
114
            $qb
115
                ->andWhere('CONCAT(o.kana01, s.kana02) LIKE :order_kana')
116
                ->setParameter('order_kana', '%'.$searchData['order_kana'].'%');
117
        }
118
119
        // order_email
120 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
121
            $qb
122
                ->andWhere('o.email like :email')
123
                ->setParameter('email', '%'.$searchData['email'].'%');
124
        }
125
126
        // tel
127 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
128
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']);
129
            $qb
130
                ->andWhere('s.phone_number LIKE :phone_number')
131
                ->setParameter('phone_number', '%'.$tel.'%');
132
        }
133
134
        // payment
135 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
136
            $payments = [];
137
            foreach ($searchData['payment'] as $payment) {
138
                $payments[] = $payment->getId();
139
            }
140
            $qb
141
                ->leftJoin('o.Payment', 'p')
142
                ->andWhere($qb->expr()->in('p.id', ':payments'))
143
                ->setParameter('payments', $payments);
144
        }
145
146
        // oreder_date
147 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
148
            $date = $searchData['order_date_start'];
149
            $qb
150
                ->andWhere('o.order_date >= :order_date_start')
151
                ->setParameter('order_date_start', $date);
152
        }
153 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
154
            $date = clone $searchData['order_date_end'];
155
            $date = $date
156
                ->modify('+1 days');
157
            $qb
158
                ->andWhere('o.order_date < :order_date_end')
159
                ->setParameter('order_date_end', $date);
160
        }
161
162
        // shipping_delivery_date
163 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_start']) && $searchData['shipping_delivery_date_start']) {
164
            $date = $searchData['shipping_delivery_date_start'];
165
            $qb
166
                ->andWhere('s.shipping_delivery_date >= :shipping_delivery_date_start')
167
                ->setParameter('shipping_delivery_date_start', $date);
168
        }
169 View Code Duplication
        if (!empty($searchData['shipping_delivery_date_end']) && $searchData['shipping_delivery_date_end']) {
170
            $date = clone $searchData['shipping_delivery_date_end'];
171
            $date = $date
172
                ->modify('+1 days');
173
            $qb
174
                ->andWhere('s.shipping_delivery_date < :shipping_delivery_date_end')
175
                ->setParameter('shipping_delivery_date_end', $date);
176
        }
177
178
        // shipping_date
179 View Code Duplication
        if (!empty($searchData['shipping_date_start']) && $searchData['shipping_date_start']) {
180
            $date = $searchData['shipping_date_start'];
181
            $qb
182
                ->andWhere('s.shipping_date >= :shipping_date_start')
183
                ->setParameter('shipping_date_start', $date);
184
        }
185 View Code Duplication
        if (!empty($searchData['shipping_date_end']) && $searchData['shipping_date_end']) {
186
            $date = clone $searchData['shipping_date_end'];
187
            $date = $date
188
                ->modify('+1 days');
189
            $qb
190
                ->andWhere('s.shipping_date < :shipping_date_end')
191
                ->setParameter('shipping_date_end', $date);
192
        }
193
194
        // update_date
195 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
196
            $date = $searchData['update_date_start'];
197
            $qb
198
                ->andWhere('s.update_date >= :update_date_start')
199
                ->setParameter('update_date_start', $date);
200
        }
201 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
202
            $date = clone $searchData['update_date_end'];
203
            $date = $date
204
                ->modify('+1 days');
205
            $qb
206
                ->andWhere('s.update_date < :update_date_end')
207
                ->setParameter('update_date_end', $date);
208
        }
209
210
        // payment_total
211 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
212
            $qb
213
                ->andWhere('o.payment_total >= :payment_total_start')
214
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
215
        }
216 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
217
            $qb
218
                ->andWhere('o.payment_total <= :payment_total_end')
219
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
220
        }
221
222
        // buy_product_name
223 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
224
            $qb
225
                ->andWhere('si.product_name LIKE :buy_product_name')
226
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
227
        }
228
229
        // Order By
230
        $qb->orderBy('s.update_date', 'DESC');
231
        $qb->addorderBy('s.id', 'DESC');
232
233
        return $qb;
234
    }
235
236
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Order" missing
Loading history...
introduced by
Doc comment for parameter "$productClass" missing
Loading history...
237
     * 同一商品のお届け先情報を取得
238
     *
239
     * @param $Order
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
240
     *
241
     * @return array
242
     */
243 27
    public function findShippingsProduct($Order, $productClass)
244
    {
245 27
        $shippings = $this->createQueryBuilder('s')
246 27
            ->innerJoin('Eccube\Entity\OrderItem', 'si', 'WITH', 'si.Shipping = s.id')
247 27
            ->where('si.Order = (:order)')
248 27
            ->andWhere('si.ProductClass = (:productClass)')
249 27
            ->setParameter('order', $Order)
250 27
            ->setParameter('productClass', $productClass)
251 27
            ->getQuery()
252 27
            ->getResult();
253
254 27
        return $shippings;
255
    }
256
}
257