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 classes like OrderRepository 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 OrderRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | class OrderRepository extends AbstractRepository |
||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @Inject("config") |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $appConfig; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @Inject("eccube.queries") |
||
| 51 | * @var Queries |
||
| 52 | */ |
||
| 53 | protected $queries; |
||
| 54 | |||
| 55 | 7 | public function changeStatus($orderId, \Eccube\Entity\Master\OrderStatus $Status) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @param array $searchData |
||
| 79 | * @return QueryBuilder |
||
| 80 | */ |
||
| 81 | 19 | public function getQueryBuilderBySearchData($searchData) |
|
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * |
||
| 262 | * @param array $searchData |
||
| 263 | * @return QueryBuilder |
||
| 264 | */ |
||
| 265 | 30 | public function getQueryBuilderBySearchDataForAdmin($searchData) |
|
| 266 | { |
||
| 267 | 30 | $qb = $this->createQueryBuilder('o'); |
|
| 268 | |||
| 269 | // order_id_start |
||
| 270 | 30 | View Code Duplication | if (isset($searchData['order_id_start']) && Str::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 | 30 | View Code Duplication | if (isset( $searchData['multi']) && Str::isNotBlank($searchData['multi'])) { |
| 277 | 5 | $multi = preg_match('/^\d+$/', $searchData['multi']) ? $searchData['multi'] : null; |
|
| 278 | $qb |
||
| 279 | 5 | ->andWhere('o.id = :multi OR o.name01 LIKE :likemulti OR o.name02 LIKE :likemulti OR ' . |
|
| 280 | 5 | 'o.kana01 LIKE :likemulti OR o.kana02 LIKE :likemulti OR o.company_name LIKE :likemulti') |
|
| 281 | 5 | ->setParameter('multi', $multi) |
|
| 282 | 5 | ->setParameter('likemulti', '%' . $searchData['multi'] . '%'); |
|
| 283 | } |
||
| 284 | |||
| 285 | // order_id_end |
||
| 286 | 30 | View Code Duplication | if (isset($searchData['order_id_end']) && Str::isNotBlank($searchData['order_id_end'])) { |
| 287 | $qb |
||
| 288 | 1 | ->andWhere('o.id <= :order_id_end') |
|
| 289 | 1 | ->setParameter('order_id_end', $searchData['order_id_end']); |
|
| 290 | } |
||
| 291 | |||
| 292 | // status |
||
| 293 | 30 | $filterStatus = false; |
|
| 294 | 30 | View Code Duplication | if (!empty($searchData['status']) && $searchData['status']) { |
| 295 | $qb |
||
| 296 | 2 | ->andWhere('o.OrderStatus = :status') |
|
| 297 | 2 | ->setParameter('status', $searchData['status']); |
|
| 298 | 2 | $filterStatus = true; |
|
| 299 | } |
||
| 300 | 30 | if (!empty($searchData['multi_status']) && count($searchData['multi_status'])) { |
|
| 301 | $qb |
||
| 302 | 1 | ->andWhere($qb->expr()->in('o.OrderStatus', ':multi_status')) |
|
| 303 | 1 | ->setParameter('multi_status', $searchData['multi_status']->toArray()); |
|
| 304 | 1 | $filterStatus = true; |
|
| 305 | } |
||
| 306 | 30 | if (!$filterStatus) { |
|
| 307 | // 購入処理中は検索対象から除外 |
||
| 308 | 27 | $OrderStatuses = $this->getEntityManager() |
|
| 309 | 27 | ->getRepository('Eccube\Entity\Master\OrderStatus') |
|
| 310 | 27 | ->findNotContainsBy(array('id' => $this->appConfig['order_processing'])); |
|
| 311 | 27 | $qb->andWhere($qb->expr()->in('o.OrderStatus', ':status')) |
|
| 312 | 27 | ->setParameter('status', $OrderStatuses); |
|
| 313 | } |
||
| 314 | |||
| 315 | // name |
||
| 316 | 30 | View Code Duplication | if (isset($searchData['name']) && Str::isNotBlank($searchData['name'])) { |
| 317 | $qb |
||
| 318 | 1 | ->andWhere('CONCAT(o.name01, o.name02) LIKE :name') |
|
| 319 | 1 | ->setParameter('name', '%' . $searchData['name'] . '%'); |
|
| 320 | } |
||
| 321 | |||
| 322 | // kana |
||
| 323 | 30 | View Code Duplication | if (isset($searchData['kana']) && Str::isNotBlank($searchData['kana'])) { |
| 324 | $qb |
||
| 325 | 1 | ->andWhere('CONCAT(o.kana01, o.kana02) LIKE :kana') |
|
| 326 | 1 | ->setParameter('kana', '%' . $searchData['kana'] . '%'); |
|
| 327 | } |
||
| 328 | |||
| 329 | |||
| 330 | 30 | View Code Duplication | if (isset($searchData['email']) && Str::isNotBlank($searchData['email'])) { |
| 331 | $qb |
||
| 332 | 2 | ->andWhere('o.email like :email') |
|
| 333 | 2 | ->setParameter('email', '%' . $searchData['email'] . '%'); |
|
| 334 | } |
||
| 335 | |||
| 336 | // tel |
||
| 337 | 30 | View Code Duplication | if (isset($searchData['tel']) && Str::isNotBlank($searchData['tel'])) { |
| 338 | $qb |
||
| 339 | 1 | ->andWhere('CONCAT(o.tel01, o.tel02, o.tel03) LIKE :tel') |
|
| 340 | 1 | ->setParameter('tel', '%' . $searchData['tel'] . '%'); |
|
| 341 | } |
||
| 342 | |||
| 343 | // sex |
||
| 344 | 30 | if (!empty($searchData['sex']) && count($searchData['sex']) > 0) { |
|
| 345 | $qb |
||
| 346 | 2 | ->andWhere($qb->expr()->in('o.Sex', ':sex')) |
|
| 347 | 2 | ->setParameter('sex', $searchData['sex']->toArray()); |
|
| 348 | } |
||
| 349 | |||
| 350 | // payment |
||
| 351 | 30 | View Code Duplication | if (!empty($searchData['payment']) && count($searchData['payment'])) { |
| 352 | 1 | $payments = array(); |
|
| 353 | 1 | foreach ($searchData['payment'] as $payment) { |
|
| 354 | 1 | $payments[] = $payment->getId(); |
|
| 355 | } |
||
| 356 | $qb |
||
| 357 | 1 | ->leftJoin('o.Payment', 'p') |
|
| 358 | 1 | ->andWhere($qb->expr()->in('p.id', ':payments')) |
|
| 359 | 1 | ->setParameter('payments', $payments); |
|
| 360 | } |
||
| 361 | |||
| 362 | // oreder_date |
||
| 363 | 30 | View Code Duplication | if (!empty($searchData['order_date_start']) && $searchData['order_date_start']) { |
| 364 | 1 | $date = $searchData['order_date_start']; |
|
| 365 | $qb |
||
| 366 | 1 | ->andWhere('o.order_date >= :order_date_start') |
|
| 367 | 1 | ->setParameter('order_date_start', $date); |
|
| 368 | } |
||
| 369 | 30 | View Code Duplication | if (!empty($searchData['order_date_end']) && $searchData['order_date_end']) { |
| 370 | 1 | $date = clone $searchData['order_date_end']; |
|
| 371 | $date = $date |
||
| 372 | 1 | ->modify('+1 days'); |
|
| 373 | $qb |
||
| 374 | 1 | ->andWhere('o.order_date < :order_date_end') |
|
| 375 | 1 | ->setParameter('order_date_end', $date); |
|
| 376 | } |
||
| 377 | |||
| 378 | // payment_date |
||
| 379 | 30 | View Code Duplication | if (!empty($searchData['payment_date_start']) && $searchData['payment_date_start']) { |
| 380 | 1 | $date = $searchData['payment_date_start']; |
|
| 381 | $qb |
||
| 382 | 1 | ->andWhere('o.payment_date >= :payment_date_start') |
|
| 383 | 1 | ->setParameter('payment_date_start', $date); |
|
| 384 | } |
||
| 385 | 30 | View Code Duplication | if (!empty($searchData['payment_date_end']) && $searchData['payment_date_end']) { |
| 386 | 1 | $date = clone $searchData['payment_date_end']; |
|
| 387 | $date = $date |
||
| 388 | 1 | ->modify('+1 days'); |
|
| 389 | $qb |
||
| 390 | 1 | ->andWhere('o.payment_date < :payment_date_end') |
|
| 391 | 1 | ->setParameter('payment_date_end', $date); |
|
| 392 | } |
||
| 393 | |||
| 394 | // commit_date |
||
| 395 | 30 | View Code Duplication | if (!empty($searchData['commit_date_start']) && $searchData['commit_date_start']) { |
| 396 | 1 | $date = $searchData['commit_date_start']; |
|
| 397 | $qb |
||
| 398 | 1 | ->andWhere('o.commit_date >= :commit_date_start') |
|
| 399 | 1 | ->setParameter('commit_date_start', $date); |
|
| 400 | } |
||
| 401 | 30 | View Code Duplication | if (!empty($searchData['commit_date_end']) && $searchData['commit_date_end']) { |
| 402 | 1 | $date = clone $searchData['commit_date_end']; |
|
| 403 | $date = $date |
||
| 404 | 1 | ->modify('+1 days'); |
|
| 405 | $qb |
||
| 406 | 1 | ->andWhere('o.commit_date < :commit_date_end') |
|
| 407 | 1 | ->setParameter('commit_date_end', $date); |
|
| 408 | } |
||
| 409 | |||
| 410 | |||
| 411 | // update_date |
||
| 412 | 30 | View Code Duplication | if (!empty($searchData['update_date_start']) && $searchData['update_date_start']) { |
| 413 | 1 | $date = $searchData['update_date_start']; |
|
| 414 | $qb |
||
| 415 | 1 | ->andWhere('o.update_date >= :update_date_start') |
|
| 416 | 1 | ->setParameter('update_date_start', $date); |
|
| 417 | } |
||
| 418 | 30 | View Code Duplication | if (!empty($searchData['update_date_end']) && $searchData['update_date_end']) { |
| 419 | 1 | $date = clone $searchData['update_date_end']; |
|
| 420 | $date = $date |
||
| 421 | 1 | ->modify('+1 days'); |
|
| 422 | $qb |
||
| 423 | 1 | ->andWhere('o.update_date < :update_date_end') |
|
| 424 | 1 | ->setParameter('update_date_end', $date); |
|
| 425 | } |
||
| 426 | |||
| 427 | // payment_total |
||
| 428 | 30 | View Code Duplication | if (isset($searchData['payment_total_start']) && Str::isNotBlank($searchData['payment_total_start'])) { |
| 429 | $qb |
||
| 430 | 1 | ->andWhere('o.payment_total >= :payment_total_start') |
|
| 431 | 1 | ->setParameter('payment_total_start', $searchData['payment_total_start']); |
|
| 432 | } |
||
| 433 | 30 | View Code Duplication | if (isset($searchData['payment_total_end']) && Str::isNotBlank($searchData['payment_total_end'])) { |
| 434 | $qb |
||
| 435 | 1 | ->andWhere('o.payment_total <= :payment_total_end') |
|
| 436 | 1 | ->setParameter('payment_total_end', $searchData['payment_total_end']); |
|
| 437 | } |
||
| 438 | |||
| 439 | // buy_product_name |
||
| 440 | 30 | View Code Duplication | if (isset($searchData['buy_product_name']) && Str::isNotBlank($searchData['buy_product_name'])) { |
| 441 | $qb |
||
| 442 | 1 | ->leftJoin('o.OrderDetails', 'od') |
|
| 443 | 1 | ->andWhere('od.product_name LIKE :buy_product_name') |
|
| 444 | 1 | ->setParameter('buy_product_name', '%' . $searchData['buy_product_name'] . '%'); |
|
| 445 | } |
||
| 446 | |||
| 447 | // Order By |
||
| 448 | 30 | $qb->orderBy('o.update_date', 'DESC'); |
|
| 449 | 30 | $qb->addorderBy('o.id', 'DESC'); |
|
| 450 | |||
| 451 | 30 | return $this->queries->customize(QueryKey::ORDER_SEARCH_ADMIN, $qb, $searchData); |
|
| 452 | } |
||
| 453 | |||
| 454 | |||
| 455 | /** |
||
| 456 | * @param \Eccube\Entity\Customer $Customer |
||
| 457 | * @return QueryBuilder |
||
| 458 | */ |
||
| 459 | 2 | View Code Duplication | public function getQueryBuilderByCustomer(\Eccube\Entity\Customer $Customer) |
| 470 | |||
| 471 | /** |
||
| 472 | * 会員の合計購入金額を取得、回数を取得 |
||
| 473 | * |
||
| 474 | * @param \Eccube\Entity\Customer $Customer |
||
| 475 | * @param array $OrderStatuses |
||
| 476 | * @return array |
||
| 477 | */ |
||
| 478 | 2 | public function getCustomerCount(\Eccube\Entity\Customer $Customer, array $OrderStatuses) |
|
| 493 | } |
||
| 494 |