Failed Conditions
Pull Request — experimental/sf (#3241)
by k-yamamura
43:19 queued 23:09
created

OrderRepository::getQueryBuilderBySearchData()   F

Complexity

Conditions 42
Paths > 20000

Size

Total Lines 167

Duplication

Lines 108
Ratio 64.67 %

Code Coverage

Tests 79
CRAP Score 42.6207

Importance

Changes 0
Metric Value
cc 42
nc 884736
nop 1
dl 108
loc 167
rs 0
c 0
b 0
f 0
ccs 79
cts 85
cp 0.9294
crap 42.6207

How to fix   Long Method    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\QueryBuilder;
17
use Eccube\Doctrine\Query\Queries;
18
use Eccube\Entity\Order;
19
use Eccube\Entity\Master\OrderStatus;
20
use Eccube\Util\StringUtil;
21
use Symfony\Bridge\Doctrine\RegistryInterface;
22
23
/**
24
 * OrderRepository
25
 *
26
 * This class was generated by the Doctrine ORM. Add your own custom
27
 * repository methods below.
28
 */
29
class OrderRepository extends AbstractRepository
30
{
31
    /**
32
     * @var Queries
33
     */
34
    protected $queries;
35
36
    /**
37
     * OrderRepository constructor.
38
     *
39
     * @param RegistryInterface $registry
40
     * @param Queries $queries
0 ignored issues
show
introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
41
     */
42 720
    public function __construct(RegistryInterface $registry, Queries $queries)
43
    {
44 720
        parent::__construct($registry, Order::class);
45 720
        $this->queries = $queries;
46
    }
47
48
    /**
49
     * @param int $orderId
0 ignored issues
show
introduced by
Expected 9 spaces after parameter type; 1 found
Loading history...
50
     * @param OrderStatus $Status
51
     */
52 9
    public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status)
53
    {
54
        $Order = $this
55 9
            ->find($orderId)
56 9
            ->setOrderStatus($Status)
57
        ;
58
59 9
        switch ($Status->getId()) {
60 9
            case '5': // 発送済へ
61 3
                $Order->setShippingDate(new \DateTime());
62 3
                break;
63 6
            case '6': // 入金済へ
64 3
                $Order->setPaymentDate(new \DateTime());
65 3
                break;
66
        }
67
68 9
        $em = $this->getEntityManager();
69 9
        $em->persist($Order);
70 9
        $em->flush();
71
    }
72
73
    /**
74
     * @param array $searchData
75
     *
76
     * @return QueryBuilder
77
     */
78 17
    public function getQueryBuilderBySearchData($searchData)
79
    {
80 17
        $qb = $this->createQueryBuilder('o');
81
82 17
        $joinedCustomer = false;
83
84
        // order_id_start
85 17 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
86
            $qb
87 1
                ->andWhere('o.id >= :order_id_start')
88 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
89
        }
90
91
        // order_id_end
92 17 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
93
            $qb
94 1
                ->andWhere('o.id <= :order_id_end')
95 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
96
        }
97
98
        // status
99 17
        if (!empty($searchData['status']) && $searchData['status']) {
100
            $qb
101 1
                ->andWhere('o.OrderStatus = :status')
102 1
                ->setParameter('status', $searchData['status']);
103
        }
104
105
        // name
106 17 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
107
            $qb
108 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
109 1
                ->setParameter('name', '%'.$searchData['name'].'%');
110
        }
111
112
        // kana
113 17 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
114
            $qb
115 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
116 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
117
        }
118
119
        // email
120 17 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
121
            $qb
122 1
                ->andWhere('o.email = :email')
123 1
                ->setParameter('email', $searchData['email']);
124
        }
125
126
        // tel
127 17 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
128
            $qb
129 1
                ->andWhere('o.phone_number = :phone_number')
130 1
                ->setParameter('phone_number', $searchData['phone_number']);
131
        }
132
133
        // birth
134 17 View Code Duplication
        if (!empty($searchData['birth_start']) && $searchData['birth_start']) {
135 1
            if (!$joinedCustomer) {
136 1
                $qb->leftJoin('o.Customer', 'c');
137 1
                $joinedCustomer = true;
138
            }
139
140 1
            $date = $searchData['birth_start'];
141
            $qb
142 1
                ->andWhere('c.birth >= :birth_start')
143 1
                ->setParameter('birth_start', $date);
144
        }
145 17 View Code Duplication
        if (!empty($searchData['birth_end']) && $searchData['birth_end']) {
146 1
            if (!$joinedCustomer) {
147 1
                $qb->leftJoin('o.Customer', 'c');
148 1
                $joinedCustomer = true;
149
            }
150
151 1
            $date = clone $searchData['birth_end'];
152
            $date = $date
153 1
                ->modify('+1 days');
154
            $qb
155 1
                ->andWhere('c.birth < :birth_end')
156 1
                ->setParameter('birth_end', $date);
157
        }
158
159
        // sex
160 17
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
161 1
            if (!$joinedCustomer) {
162 1
                $qb->leftJoin('o.Customer', 'c');
163 1
                $joinedCustomer = true;
0 ignored issues
show
Unused Code introduced by
$joinedCustomer 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...
164
            }
165
166 1
            $sexs = [];
167 1
            foreach ($searchData['sex'] as $sex) {
168 1
                $sexs[] = $sex->getId();
169
            }
170
171
            $qb
172 1
                ->andWhere($qb->expr()->in('c.Sex', ':sexs'))
173 1
                ->setParameter('sexs', $sexs);
174
        }
175
176
        // payment
177 17 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
178
            $payments = [];
179
            foreach ($searchData['payment'] as $payment) {
180
                $payments[] = $payment->getId();
181
            }
182
            $qb
183
                ->leftJoin('o.Payment', 'p')
184
                ->andWhere($qb->expr()->in('p.id', ':payments'))
185
                ->setParameter('payments', $payments);
186
        }
187
188
        // oreder_date
189 17 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
190 1
            $date = $searchData['order_date_start'];
191
            $qb
192 1
                ->andWhere('o.create_date >= :order_date_start')
193 1
                ->setParameter('order_date_start', $date);
194
        }
195 17 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
196 1
            $date = clone $searchData['order_date_end'];
197
            $date = $date
198 1
                ->modify('+1 days');
199
            $qb
200 1
                ->andWhere('o.create_date < :order_date_end')
201 1
                ->setParameter('order_date_end', $date);
202
        }
203
204
        // create_date
205 17 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
206 1
            $date = $searchData['update_date_start'];
207
            $qb
208 1
                ->andWhere('o.update_date >= :update_date_start')
209 1
                ->setParameter('update_date_start', $date);
210
        }
211 17 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
212 1
            $date = clone $searchData['update_date_end'];
213
            $date = $date
214 1
                ->modify('+1 days');
215
            $qb
216 1
                ->andWhere('o.update_date < :update_date_end')
217 1
                ->setParameter('update_date_end', $date);
218
        }
219
220
        // payment_total
221 17 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
222
            $qb
223 1
                ->andWhere('o.payment_total >= :payment_total_start')
224 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
225
        }
226 17 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
227
            $qb
228 1
                ->andWhere('o.payment_total <= :payment_total_end')
229 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
230
        }
231
232
        // buy_product_name
233 17 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
234
            $qb
235 1
                ->leftJoin('o.OrderItems', 'oi')
236 1
                ->andWhere('oi.product_name LIKE :buy_product_name')
237 1
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
238
        }
239
240
        // Order By
241 17
        $qb->addOrderBy('o.update_date', 'DESC');
242
243 17
        return $this->queries->customize(QueryKey::ORDER_SEARCH, $qb, $searchData);
244
    }
245
246
    /**
247
     * @param  array        $searchData
0 ignored issues
show
introduced by
Expected 1 spaces after parameter type; 8 found
Loading history...
248
     *
249
     * @return QueryBuilder
250
     */
251 34
    public function getQueryBuilderBySearchDataForAdmin($searchData)
252
    {
253 34
        $qb = $this->createQueryBuilder('o');
254
255
        // order_id_start
256 34 View Code Duplication
        if (isset($searchData['order_id']) && StringUtil::isNotBlank($searchData['order_id'])) {
257
            $qb
258
                ->andWhere('o.id = :order_id')
259
                ->setParameter('order_id', $searchData['order_id']);
260
        }
261
262
        // order_no
263 34 View Code Duplication
        if (isset($searchData['order_no']) && StringUtil::isNotBlank($searchData['order_no'])) {
264
            $qb
265 1
                ->andWhere('o.order_no = :order_no')
266 1
                ->setParameter('order_no', $searchData['order_no']);
267
        }
268
269
        // order_id_start
270 34 View Code Duplication
        if (isset($searchData['order_id_start']) && StringUtil::isNotBlank($searchData['order_id_start'])) {
271
            $qb
272 1
                ->andWhere('o.id >= :order_id_start')
273 1
                ->setParameter('order_id_start', $searchData['order_id_start']);
274
        }
275
        // multi
276 34 View Code Duplication
        if (isset($searchData['multi']) && StringUtil::isNotBlank($searchData['multi'])) {
277 5
            $multi = preg_match('/^\d{0,10}$/', $searchData['multi']) ? $searchData['multi'] : null;
278
            $qb
279 5
                ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
280
                           'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti OR '.
0 ignored issues
show
Coding Style introduced by
Concat operator must not be surrounded by spaces
Loading history...
281 5
                           'o.order_no 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...
282 5
                ->setParameter('multi', $multi)
283 5
                ->setParameter('likemulti', '%'.$searchData['multi'].'%');
284
        }
285
286
        // order_id_end
287 34 View Code Duplication
        if (isset($searchData['order_id_end']) && StringUtil::isNotBlank($searchData['order_id_end'])) {
288
            $qb
289 1
                ->andWhere('o.id <= :order_id_end')
290 1
                ->setParameter('order_id_end', $searchData['order_id_end']);
291
        }
292
293
        // status
294 34
        $filterStatus = false;
295 34 View Code Duplication
        if (!empty($searchData['status']) && count($searchData['status'])) {
296
            $qb
297 2
                ->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
298 2
                ->setParameter('status', $searchData['status']);
299 2
            $filterStatus = true;
300
        }
301
302 34
        if (!$filterStatus) {
303
            // 購入処理中は検索対象から除外
304 32
            $OrderStatuses = $this->getEntityManager()
305 32
                ->getRepository('Eccube\Entity\Master\OrderStatus')
306 32
                ->findNotContainsBy(['id' => OrderStatus::PROCESSING]);
307 32
            $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status'))
308 32
                ->setParameter('status', $OrderStatuses);
309
        }
310
311
        // company_name
312 34 View Code Duplication
        if (isset($searchData['company_name']) && StringUtil::isNotBlank($searchData['company_name'])) {
313
            $qb
314 1
                ->andWhere('o.company_name LIKE :company_name')
315 1
                ->setParameter('company_name', '%'.$searchData['company_name'].'%');
316
        }
317
318
        // name
319 34 View Code Duplication
        if (isset($searchData['name']) && StringUtil::isNotBlank($searchData['name'])) {
320
            $qb
321 1
                ->andWhere('CONCAT(o.name01, o.name02) LIKE :name')
322 1
                ->setParameter('name', '%'.$searchData['name'].'%');
323
        }
324
325
        // kana
326 34 View Code Duplication
        if (isset($searchData['kana']) && StringUtil::isNotBlank($searchData['kana'])) {
327
            $qb
328 1
                ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana')
329 1
                ->setParameter('kana', '%'.$searchData['kana'].'%');
330
        }
331
332
        // email
333 34 View Code Duplication
        if (isset($searchData['email']) && StringUtil::isNotBlank($searchData['email'])) {
334
            $qb
335 4
                ->andWhere('o.email like :email')
336 4
                ->setParameter('email', '%'.$searchData['email'].'%');
337
        }
338
339
        // tel
340 34 View Code Duplication
        if (isset($searchData['phone_number']) && StringUtil::isNotBlank($searchData['phone_number'])) {
341 1
            $tel = preg_replace('/[^0-9]/ ', '', $searchData['phone_number']);
342
            $qb
343 1
                ->andWhere('o.phone_number LIKE :phone_number')
344 1
                ->setParameter('phone_number', '%'.$tel.'%');
345
        }
346
347
        // sex
348 34
        if (!empty($searchData['sex']) && count($searchData['sex']) > 0) {
349
            $qb
350 2
                ->andWhere($qb->expr()->in('o.Sex', ':sex'))
351 2
                ->setParameter('sex', $searchData['sex']->toArray());
352
        }
353
354
        // payment
355 34 View Code Duplication
        if (!empty($searchData['payment']) && count($searchData['payment'])) {
356 1
            $payments = [];
357 1
            foreach ($searchData['payment'] as $payment) {
358 1
                $payments[] = $payment->getId();
359
            }
360
            $qb
361 1
                ->leftJoin('o.Payment', 'p')
362 1
                ->andWhere($qb->expr()->in('p.id', ':payments'))
363 1
                ->setParameter('payments', $payments);
364
        }
365
366
        // oreder_date
367 34 View Code Duplication
        if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) {
368 1
            $date = $searchData['order_date_start'];
369
            $qb
370 1
                ->andWhere('o.order_date >= :order_date_start')
371 1
                ->setParameter('order_date_start', $date);
372
        }
373 34 View Code Duplication
        if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) {
374 1
            $date = clone $searchData['order_date_end'];
375
            $date = $date
376 1
                ->modify('+1 days');
377
            $qb
378 1
                ->andWhere('o.order_date < :order_date_end')
379 1
                ->setParameter('order_date_end', $date);
380
        }
381
382
        // payment_date
383 34 View Code Duplication
        if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) {
384 1
            $date = $searchData['payment_date_start'];
385
            $qb
386 1
                ->andWhere('o.payment_date >= :payment_date_start')
387 1
                ->setParameter('payment_date_start', $date);
388
        }
389 34 View Code Duplication
        if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) {
390 1
            $date = clone $searchData['payment_date_end'];
391
            $date = $date
392 1
                ->modify('+1 days');
393
            $qb
394 1
                ->andWhere('o.payment_date < :payment_date_end')
395 1
                ->setParameter('payment_date_end', $date);
396
        }
397
398
        // shipping_date
399 34 View Code Duplication
        if (!empty($searchData['shipping_date_start']) && $searchData['shipping_date_start']) {
400 1
            $date = $searchData['shipping_date_start'];
401
            $qb
402 1
                ->andWhere('o.shipping_date >= :shipping_date_start')
403 1
                ->setParameter('shipping_date_start', $date);
404
        }
405 34 View Code Duplication
        if (!empty($searchData['shipping_date_end']) && $searchData['shipping_date_end']) {
406 1
            $date = clone $searchData['shipping_date_end'];
407
            $date = $date
408 1
                ->modify('+1 days');
409
            $qb
410 1
                ->andWhere('o.shipping_date < :shipping_date_end')
411 1
                ->setParameter('shipping_date_end', $date);
412
        }
413
414
        // update_date
415 34 View Code Duplication
        if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) {
416 1
            $date = $searchData['update_date_start'];
417
            $qb
418 1
                ->andWhere('o.update_date >= :update_date_start')
419 1
                ->setParameter('update_date_start', $date);
420
        }
421 34 View Code Duplication
        if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) {
422 1
            $date = clone $searchData['update_date_end'];
423
            $date = $date
424 1
                ->modify('+1 days');
425
            $qb
426 1
                ->andWhere('o.update_date < :update_date_end')
427 1
                ->setParameter('update_date_end', $date);
428
        }
429
430
        // payment_total
431 34 View Code Duplication
        if (isset($searchData['payment_total_start']) && StringUtil::isNotBlank($searchData['payment_total_start'])) {
432
            $qb
433 1
                ->andWhere('o.payment_total >= :payment_total_start')
434 1
                ->setParameter('payment_total_start', $searchData['payment_total_start']);
435
        }
436 34 View Code Duplication
        if (isset($searchData['payment_total_end']) && StringUtil::isNotBlank($searchData['payment_total_end'])) {
437
            $qb
438 1
                ->andWhere('o.payment_total <= :payment_total_end')
439 1
                ->setParameter('payment_total_end', $searchData['payment_total_end']);
440
        }
441
442
        // buy_product_name
443 34 View Code Duplication
        if (isset($searchData['buy_product_name']) && StringUtil::isNotBlank($searchData['buy_product_name'])) {
444
            $qb
445 1
                ->leftJoin('o.OrderItems', 'oi')
446 1
                ->andWhere('oi.product_name LIKE :buy_product_name')
447 1
                ->setParameter('buy_product_name', '%'.$searchData['buy_product_name'].'%');
448
        }
449
450
        // Order By
451 34
        $qb->orderBy('o.update_date', 'DESC');
452 34
        $qb->addorderBy('o.id', 'DESC');
453
454 34
        return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData);
455
    }
456
457
    /**
458
     * @param  \Eccube\Entity\Customer $Customer
459
     *
460
     * @return QueryBuilder
461
     */
462 2 View Code Duplication
    public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer)
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...
463
    {
464 2
        $qb = $this->createQueryBuilder('o')
465 2
            ->where('o.Customer = :Customer')
466 2
            ->setParameter('Customer', $Customer);
467
468
        // Order By
469 2
        $qb->addOrderBy('o.id', 'DESC');
470
471 2
        return $this->queries->customize(QueryKey::ORDER_SEARCH_BY_CUSTOMER, $qb, ['customer' => $Customer]);
472
    }
473
474
    /**
475
     * 会員の合計購入金額を取得、回数を取得
476
     *
477
     * @param  \Eccube\Entity\Customer $Customer
478
     * @param  array $OrderStatuses
0 ignored issues
show
introduced by
Expected 19 spaces after parameter type; 1 found
Loading history...
479
     *
480
     * @return array
481
     */
482 1
    public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses)
483
    {
484 1
        $result = $this->createQueryBuilder('o')
485 1
            ->select('COUNT(o.id) AS buy_times, SUM(o.total)  AS buy_total')
486 1
            ->where('o.Customer = :Customer')
487 1
            ->andWhere('o.OrderStatus in (:OrderStatuses)')
488 1
            ->setParameter('Customer', $Customer)
489 1
            ->setParameter('OrderStatuses', $OrderStatuses)
490 1
            ->groupBy('o.Customer')
491 1
            ->getQuery()
492 1
            ->getResult();
493
494 1
        return $result;
495
    }
496
497
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$Customer" missing
Loading history...
498
     * 会員が保持する最新の購入処理中の Order を取得する.
499
     *
500
     * @param Customer
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
501
     *
502
     * @return Order
503
     */
504 2
    public function getExistsOrdersByCustomer(\Eccube\Entity\Customer $Customer)
505
    {
506 2
        $qb = $this->createQueryBuilder('o');
507
        $Order = $qb
508 2
            ->select('o')
509 2
            ->where('o.Customer = :Customer')
510 2
            ->setParameter('Customer', $Customer)
511 2
            ->orderBy('o.id', 'DESC')
512 2
            ->getQuery()
513 2
            ->setMaxResults(1)
514 2
            ->getOneOrNullResult();
515
516 2
        if ($Order && $Order->getOrderStatus()->getId() == OrderStatus::PROCESSING) {
517 1
            return $Order;
518
        }
519
520 1
        return null;
521
    }
522
}
523