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