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 OrderController 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 OrderController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class OrderController extends AbstractController |
||
|
|
|||
| 53 | { |
||
| 54 | /** |
||
| 55 | * @var PurchaseFlow |
||
| 56 | */ |
||
| 57 | protected $purchaseFlow; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var CsvExportService |
||
| 61 | */ |
||
| 62 | protected $csvExportService; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var CustomerRepository |
||
| 66 | */ |
||
| 67 | protected $customerRepository; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var PaymentRepository |
||
| 71 | */ |
||
| 72 | protected $paymentRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var SexRepository |
||
| 76 | */ |
||
| 77 | protected $sexRepository; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var OrderStatusRepository |
||
| 81 | */ |
||
| 82 | protected $orderStatusRepository; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var PageMaxRepository |
||
| 86 | */ |
||
| 87 | protected $pageMaxRepository; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var ProductStatusRepository |
||
| 91 | */ |
||
| 92 | protected $productStatusRepository; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var OrderRepository |
||
| 96 | */ |
||
| 97 | protected $orderRepository; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var ValidatorInterface |
||
| 101 | */ |
||
| 102 | protected $validator; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @var OrderStateMachine |
||
| 106 | */ |
||
| 107 | protected $orderStateMachine; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * OrderController constructor. |
||
| 111 | * |
||
| 112 | * @param PurchaseFlow $orderPurchaseFlow |
||
| 113 | * @param CsvExportService $csvExportService |
||
| 114 | * @param CustomerRepository $customerRepository |
||
| 115 | * @param PaymentRepository $paymentRepository |
||
| 116 | * @param SexRepository $sexRepository |
||
| 117 | * @param OrderStatusRepository $orderStatusRepository |
||
| 118 | * @param PageMaxRepository $pageMaxRepository |
||
| 119 | * @param ProductStatusRepository $productStatusRepository |
||
| 120 | * @param OrderRepository $orderRepository |
||
| 121 | * @param OrderStateMachine $orderStateMachine; |
||
| 122 | */ |
||
| 123 | 14 | public function __construct( |
|
| 148 | |||
| 149 | /** |
||
| 150 | * 受注一覧画面. |
||
| 151 | * |
||
| 152 | * - 検索条件, ページ番号, 表示件数はセッションに保持されます. |
||
| 153 | * - クエリパラメータでresume=1が指定された場合、検索条件, ページ番号, 表示件数をセッションから復旧します. |
||
| 154 | * - 各データの, セッションに保持するアクションは以下の通りです. |
||
| 155 | * - 検索ボタン押下時 |
||
| 156 | * - 検索条件をセッションに保存します |
||
| 157 | * - ページ番号は1で初期化し、セッションに保存します。 |
||
| 158 | * - 表示件数変更時 |
||
| 159 | * - クエリパラメータpage_countをセッションに保存します。 |
||
| 160 | * - ただし, mtb_page_maxと一致しない場合, eccube_default_page_countが保存されます. |
||
| 161 | * - ページング時 |
||
| 162 | * - URLパラメータpage_noをセッションに保存します. |
||
| 163 | * - 初期表示 |
||
| 164 | * - 検索条件は空配列, ページ番号は1で初期化し, セッションに保存します. |
||
| 165 | * |
||
| 166 | * @Route("/%eccube_admin_route%/order", name="admin_order") |
||
| 167 | * @Route("/%eccube_admin_route%/order/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_page") |
||
| 168 | * @Template("@admin/Order/index.twig") |
||
| 169 | */ |
||
| 170 | 8 | public function index(Request $request, $page_no = null, PaginatorInterface $paginator) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @Method("POST") |
||
| 297 | * @Route("/%eccube_admin_route%/order/bulk_delete", name="admin_order_bulk_delete") |
||
| 298 | */ |
||
| 299 | 1 | public function bulkDelete(Request $request) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * 受注CSVの出力. |
||
| 321 | * |
||
| 322 | * @Route("/%eccube_admin_route%/order/export/order", name="admin_order_export_order") |
||
| 323 | * |
||
| 324 | * @param Request $request |
||
| 325 | * |
||
| 326 | * @return StreamedResponse |
||
| 327 | */ |
||
| 328 | 1 | View Code Duplication | public function exportOrder(Request $request) |
| 336 | |||
| 337 | /** |
||
| 338 | * 配送CSVの出力. |
||
| 339 | * |
||
| 340 | * @Route("/%eccube_admin_route%/order/export/shipping", name="admin_order_export_shipping") |
||
| 341 | * |
||
| 342 | * @param Request $request |
||
| 343 | * |
||
| 344 | * @return StreamedResponse |
||
| 345 | */ |
||
| 346 | View Code Duplication | public function exportShipping(Request $request) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param Request $request |
||
| 357 | * @param $csvTypeId |
||
| 358 | * @param $fileName |
||
| 359 | * |
||
| 360 | * @return StreamedResponse |
||
| 361 | */ |
||
| 362 | 1 | private function exportCsv(Request $request, $csvTypeId, $fileName) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Update to order status |
||
| 437 | * |
||
| 438 | * @Method("PUT") |
||
| 439 | * @Route("/%eccube_admin_route%/shipping/{id}/order_status", requirements={"id" = "\d+"}, name="admin_shipping_update_order_status") |
||
| 440 | * |
||
| 441 | * @param Request $request |
||
| 442 | * @param Shipping $shipping |
||
| 443 | * |
||
| 444 | * @return RedirectResponse |
||
| 445 | */ |
||
| 446 | public function updateOrderStatus(Request $request, Shipping $Shipping) |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Bulk action to order status |
||
| 508 | * |
||
| 509 | * @Method("POST") |
||
| 510 | * @Route("/%eccube_admin_route%/order/bulk/order-status/{id}", requirements={"id" = "\d+"}, name="admin_order_bulk_order_status") |
||
| 511 | * |
||
| 512 | * @param Request $request |
||
| 513 | * @param OrderStatus $OrderStatus |
||
| 514 | * |
||
| 515 | * @return RedirectResponse |
||
| 516 | * |
||
| 517 | * @deprecated 使用していない |
||
| 518 | */ |
||
| 519 | 2 | public function bulkOrderStatus(Request $request, OrderStatus $OrderStatus) |
|
| 520 | { |
||
| 521 | 2 | $this->isTokenValid(); |
|
| 522 | |||
| 523 | /** @var Order[] $Orders */ |
||
| 524 | 2 | $Orders = $this->orderRepository->findBy(['id' => $request->get('ids')]); |
|
| 525 | |||
| 526 | 2 | $count = 0; |
|
| 527 | 2 | foreach ($Orders as $Order) { |
|
| 528 | try { |
||
| 529 | // TODO: should support event for plugin customize |
||
| 530 | // 編集前の受注情報を保持 |
||
| 531 | 2 | $OriginOrder = clone $Order; |
|
| 532 | |||
| 533 | 2 | $Order->setOrderStatus($OrderStatus); |
|
| 534 | |||
| 535 | 2 | $purchaseContext = new PurchaseContext($OriginOrder, $OriginOrder->getCustomer()); |
|
| 536 | |||
| 537 | 2 | $flowResult = $this->purchaseFlow->validate($Order, $purchaseContext); |
|
| 538 | 2 | if ($flowResult->hasWarning()) { |
|
| 539 | 1 | foreach ($flowResult->getWarning() as $warning) { |
|
| 540 | 1 | $msg = $this->translator->trans('admin.order.index.bulk_warning', [ |
|
| 541 | 1 | '%orderId%' => $Order->getId(), |
|
| 542 | 1 | '%message%' => $warning->getMessage(), |
|
| 543 | ]); |
||
| 544 | 1 | $this->addWarning($msg, 'admin'); |
|
| 545 | } |
||
| 546 | } |
||
| 547 | |||
| 548 | 2 | if ($flowResult->hasError()) { |
|
| 549 | foreach ($flowResult->getErrors() as $error) { |
||
| 550 | $msg = $this->translator->trans('admin.order.index.bulk_error', [ |
||
| 551 | '%orderId%' => $Order->getId(), |
||
| 552 | '%message%' => $error->getMessage(), |
||
| 553 | ]); |
||
| 554 | $this->addError($msg, 'admin'); |
||
| 555 | } |
||
| 556 | continue; |
||
| 557 | } |
||
| 558 | |||
| 559 | try { |
||
| 560 | 2 | $this->purchaseFlow->commit($Order, $purchaseContext); |
|
| 561 | } catch (PurchaseException $e) { |
||
| 562 | $msg = $this->translator->trans('admin.order.index.bulk_error', [ |
||
| 563 | '%orderId%' => $Order->getId(), |
||
| 564 | '%message%' => $e->getMessage(), |
||
| 565 | ]); |
||
| 566 | $this->addError($msg, 'admin'); |
||
| 567 | continue; |
||
| 568 | } |
||
| 569 | |||
| 570 | 2 | $this->orderRepository->save($Order); |
|
| 571 | |||
| 572 | 2 | $count++; |
|
| 573 | } catch (\Exception $e) { |
||
| 574 | 2 | $this->addError('#'.$Order->getId().': '.$e->getMessage(), 'admin'); |
|
| 575 | } |
||
| 576 | } |
||
| 577 | try { |
||
| 578 | 2 | View Code Duplication | if ($count) { |
| 579 | 2 | $this->entityManager->flush(); |
|
| 580 | 2 | $msg = $this->translator->trans('admin.order.index.bulk_order_status_success_count', [ |
|
| 581 | 2 | '%count%' => $count, |
|
| 582 | 2 | '%status%' => $OrderStatus->getName(), |
|
| 583 | ]); |
||
| 584 | 2 | $this->addSuccess($msg, 'admin'); |
|
| 585 | } |
||
| 586 | } catch (\Exception $e) { |
||
| 587 | log_error('Bulk order status error', [$e]); |
||
| 588 | $this->addError('admin.flash.register_failed', 'admin'); |
||
| 589 | } |
||
| 590 | |||
| 591 | 2 | return $this->redirectToRoute('admin_order', ['resume' => Constant::ENABLED]); |
|
| 592 | } |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Update to Tracking number. |
||
| 596 | * |
||
| 597 | * @Method("PUT") |
||
| 598 | * @Route("/%eccube_admin_route%/shipping/{id}/tracking_number", requirements={"id" = "\d+"}, name="admin_shipping_update_tracking_number") |
||
| 599 | * |
||
| 600 | * @param Request $request |
||
| 601 | * @param Shipping $shipping |
||
| 602 | * |
||
| 603 | * @return Response |
||
| 604 | */ |
||
| 605 | 2 | public function updateTrackingNumber(Request $request, Shipping $shipping) |
|
| 647 | } |
||
| 648 |