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:
1 | <?php |
||
50 | class OrderController extends AbstractController |
||
|
|||
51 | { |
||
52 | /** |
||
53 | * @var PurchaseFlow |
||
54 | */ |
||
55 | protected $purchaseFlow; |
||
56 | |||
57 | /** |
||
58 | * @var CsvExportService |
||
59 | */ |
||
60 | protected $csvExportService; |
||
61 | |||
62 | /** |
||
63 | * @var CustomerRepository |
||
64 | */ |
||
65 | protected $customerRepository; |
||
66 | |||
67 | /** |
||
68 | * @var PaymentRepository |
||
69 | */ |
||
70 | protected $paymentRepository; |
||
71 | |||
72 | /** |
||
73 | * @var SexRepository |
||
74 | */ |
||
75 | protected $sexRepository; |
||
76 | |||
77 | /** |
||
78 | * @var OrderStatusRepository |
||
79 | */ |
||
80 | protected $orderStatusRepository; |
||
81 | |||
82 | /** |
||
83 | * @var PageMaxRepository |
||
84 | */ |
||
85 | protected $pageMaxRepository; |
||
86 | |||
87 | /** |
||
88 | * @var ProductStatusRepository |
||
89 | */ |
||
90 | protected $productStatusRepository; |
||
91 | |||
92 | /** |
||
93 | * @var OrderRepository |
||
94 | */ |
||
95 | protected $orderRepository; |
||
96 | |||
97 | /** |
||
98 | * @var ValidatorInterface |
||
99 | */ |
||
100 | protected $validator; |
||
101 | |||
102 | /** |
||
103 | * OrderController constructor. |
||
104 | * |
||
105 | * @param PurchaseFlow $orderPurchaseFlow |
||
106 | * @param CsvExportService $csvExportService |
||
107 | * @param CustomerRepository $customerRepository |
||
108 | * @param PaymentRepository $paymentRepository |
||
109 | * @param SexRepository $sexRepository |
||
110 | * @param OrderStatusRepository $orderStatusRepository |
||
111 | * @param PageMaxRepository $pageMaxRepository |
||
112 | * @param ProductStatusRepository $productStatusRepository |
||
113 | * @param OrderRepository $orderRepository |
||
114 | */ |
||
115 | 14 | public function __construct( |
|
138 | |||
139 | /** |
||
140 | * 受注一覧画面. |
||
141 | * |
||
142 | * - 検索条件, ページ番号, 表示件数はセッションに保持されます. |
||
143 | * - クエリパラメータでresume=1が指定された場合、検索条件, ページ番号, 表示件数をセッションから復旧します. |
||
144 | * - 各データの, セッションに保持するアクションは以下の通りです. |
||
145 | * - 検索ボタン押下時 |
||
146 | * - 検索条件をセッションに保存します |
||
147 | * - ページ番号は1で初期化し、セッションに保存します。 |
||
148 | * - 表示件数変更時 |
||
149 | * - クエリパラメータpage_countをセッションに保存します。 |
||
150 | * - ただし, mtb_page_maxと一致しない場合, eccube_default_page_countが保存されます. |
||
151 | * - ページング時 |
||
152 | * - URLパラメータpage_noをセッションに保存します. |
||
153 | * - 初期表示 |
||
154 | * - 検索条件は空配列, ページ番号は1で初期化し, セッションに保存します. |
||
155 | * |
||
156 | * @Route("/%eccube_admin_route%/order", name="admin_order") |
||
157 | * @Route("/%eccube_admin_route%/order/page/{page_no}", requirements={"page_no" = "\d+"}, name="admin_order_page") |
||
158 | * @Template("@admin/Order/index.twig") |
||
159 | */ |
||
160 | 8 | public function index(Request $request, $page_no = null, PaginatorInterface $paginator) |
|
284 | |||
285 | /** |
||
286 | * @Method("POST") |
||
287 | * @Route("/%eccube_admin_route%/order/bulk_delete", name="admin_order_bulk_delete") |
||
288 | */ |
||
289 | 1 | public function bulkDelete(Request $request) |
|
308 | |||
309 | /** |
||
310 | * 受注CSVの出力. |
||
311 | * |
||
312 | * @Route("/%eccube_admin_route%/order/export/order", name="admin_order_export_order") |
||
313 | * |
||
314 | * @param Request $request |
||
315 | * |
||
316 | * @return StreamedResponse |
||
317 | */ |
||
318 | 1 | public function exportOrder(Request $request) |
|
394 | |||
395 | /** |
||
396 | * Bulk action to order status |
||
397 | * |
||
398 | * @Method("POST") |
||
399 | * @Route("/%eccube_admin_route%/order/bulk/order-status/{id}", requirements={"id" = "\d+"}, name="admin_order_bulk_order_status") |
||
400 | * |
||
401 | * @param Request $request |
||
402 | * @param OrderStatus $OrderStatus |
||
403 | * |
||
404 | * @return RedirectResponse |
||
405 | */ |
||
406 | 2 | public function bulkOrderStatus(Request $request, OrderStatus $OrderStatus) |
|
407 | { |
||
408 | 2 | $this->isTokenValid(); |
|
409 | |||
410 | /** @var Order[] $Orders */ |
||
411 | 2 | $Orders = $this->orderRepository->findBy(['id' => $request->get('ids')]); |
|
412 | |||
413 | 2 | $count = 0; |
|
414 | 2 | foreach ($Orders as $Order) { |
|
415 | try { |
||
416 | // TODO: should support event for plugin customize |
||
417 | // 編集前の受注情報を保持 |
||
418 | 2 | $OriginOrder = clone $Order; |
|
419 | |||
420 | 2 | $Order->setOrderStatus($OrderStatus); |
|
421 | |||
422 | 2 | $purchaseContext = new PurchaseContext($OriginOrder, $OriginOrder->getCustomer()); |
|
423 | |||
424 | 2 | $flowResult = $this->purchaseFlow->validate($Order, $purchaseContext); |
|
425 | 2 | if ($flowResult->hasWarning()) { |
|
426 | 1 | foreach ($flowResult->getWarning() as $warning) { |
|
427 | 1 | $msg = $this->translator->trans('admin.order.index.bulk_warning', [ |
|
428 | 1 | '%orderId%' => $Order->getId(), |
|
429 | 1 | '%message%' => $warning->getMessage(), |
|
430 | ]); |
||
431 | 1 | $this->addWarning($msg, 'admin'); |
|
432 | } |
||
433 | } |
||
434 | |||
435 | 2 | if ($flowResult->hasError()) { |
|
436 | foreach ($flowResult->getErrors() as $error) { |
||
437 | $msg = $this->translator->trans('admin.order.index.bulk_error', [ |
||
438 | '%orderId%' => $Order->getId(), |
||
439 | '%message%' => $error->getMessage(), |
||
440 | ]); |
||
441 | $this->addError($msg, 'admin'); |
||
442 | } |
||
443 | continue; |
||
444 | } |
||
445 | |||
446 | try { |
||
447 | 2 | $this->purchaseFlow->commit($Order, $purchaseContext); |
|
448 | } catch (PurchaseException $e) { |
||
449 | $msg = $this->translator->trans('admin.order.index.bulk_error', [ |
||
450 | '%orderId%' => $Order->getId(), |
||
451 | '%message%' => $e->getMessage(), |
||
452 | ]); |
||
453 | $this->addError($msg, 'admin'); |
||
454 | continue; |
||
455 | } |
||
456 | |||
457 | 2 | $this->orderRepository->save($Order); |
|
458 | |||
459 | 2 | $count++; |
|
460 | } catch (\Exception $e) { |
||
461 | 2 | $this->addError('#'.$Order->getId().': '.$e->getMessage(), 'admin'); |
|
462 | } |
||
463 | } |
||
464 | try { |
||
465 | 2 | View Code Duplication | if ($count) { |
466 | 2 | $this->entityManager->flush(); |
|
467 | 2 | $msg = $this->translator->trans('admin.order.index.bulk_order_status_success_count', [ |
|
468 | 2 | '%count%' => $count, |
|
469 | 2 | '%status%' => $OrderStatus->getName(), |
|
470 | ]); |
||
471 | 2 | $this->addSuccess($msg, 'admin'); |
|
472 | } |
||
473 | } catch (\Exception $e) { |
||
474 | log_error('Bulk order status error', [$e]); |
||
475 | $this->addError('admin.flash.register_failed', 'admin'); |
||
476 | } |
||
477 | |||
478 | 2 | return $this->redirectToRoute('admin_order', ['resume' => Constant::ENABLED]); |
|
479 | } |
||
480 | |||
481 | /** |
||
482 | * Update to Tracking number. |
||
483 | * |
||
484 | * @Method("PUT") |
||
485 | * @Route("/%eccube_admin_route%/shipping/{id}/tracking_number", requirements={"id" = "\d+"}, name="admin_shipping_update_tracking_number") |
||
486 | * |
||
487 | * @param Request $request |
||
488 | * @param Shipping $shipping |
||
489 | * |
||
490 | * @return Response |
||
491 | */ |
||
492 | 2 | public function updateTrackingNumber(Request $request, Shipping $shipping) |
|
534 | } |
||
535 |