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 |
||
| 57 | class OrderController extends AbstractController |
||
|
|
|||
| 58 | { |
||
| 59 | /** |
||
| 60 | * @var PurchaseFlow |
||
| 61 | */ |
||
| 62 | protected $purchaseFlow; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var CsvExportService |
||
| 66 | */ |
||
| 67 | protected $csvExportService; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var CustomerRepository |
||
| 71 | */ |
||
| 72 | protected $customerRepository; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var PaymentRepository |
||
| 76 | */ |
||
| 77 | protected $paymentRepository; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var SexRepository |
||
| 81 | */ |
||
| 82 | protected $sexRepository; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var OrderStatusRepository |
||
| 86 | */ |
||
| 87 | protected $orderStatusRepository; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var PageMaxRepository |
||
| 91 | */ |
||
| 92 | protected $pageMaxRepository; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var ProductStatusRepository |
||
| 96 | */ |
||
| 97 | protected $productStatusRepository; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var OrderRepository |
||
| 101 | */ |
||
| 102 | protected $orderRepository; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * OrderController constructor. |
||
| 106 | * |
||
| 107 | * @param PurchaseFlow $orderPurchaseFlow |
||
| 108 | * @param CsvExportService $csvExportService |
||
| 109 | * @param CustomerRepository $customerRepository |
||
| 110 | * @param PaymentRepository $paymentRepository |
||
| 111 | * @param SexRepository $sexRepository |
||
| 112 | * @param OrderStatusRepository $orderStatusRepository |
||
| 113 | * @param PageMaxRepository $pageMaxRepository |
||
| 114 | * @param ProductStatusRepository $productStatusRepository |
||
| 115 | * @param OrderRepository $orderRepository |
||
| 116 | */ |
||
| 117 | 13 | 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 | 9 | public function index(Request $request, $page_no = null, PaginatorInterface $paginator) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @Method("POST") |
||
| 281 | * @Route("/%eccube_admin_route%/order/bulk_delete", name="admin_order_bulk_delete") |
||
| 282 | */ |
||
| 283 | 1 | public function bulkDelete(Request $request) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * 受注CSVの出力. |
||
| 305 | * |
||
| 306 | * @Route("/%eccube_admin_route%/order/export/order", name="admin_order_export_order") |
||
| 307 | * |
||
| 308 | * @param Request $request |
||
| 309 | * |
||
| 310 | * @return StreamedResponse |
||
| 311 | */ |
||
| 312 | 1 | public function exportOrder(Request $request) |
|
| 313 | { |
||
| 314 | // タイムアウトを無効にする. |
||
| 315 | 1 | set_time_limit(0); |
|
| 316 | |||
| 317 | // sql loggerを無効にする. |
||
| 318 | 1 | $em = $this->entityManager; |
|
| 319 | 1 | $em->getConfiguration()->setSQLLogger(null); |
|
| 320 | |||
| 321 | 1 | $response = new StreamedResponse(); |
|
| 322 | 1 | $response->setCallback(function () use ($request) { |
|
| 323 | // CSV種別を元に初期化. |
||
| 324 | 1 | $this->csvExportService->initCsvType(CsvType::CSV_TYPE_ORDER); |
|
| 325 | |||
| 326 | // ヘッダ行の出力. |
||
| 327 | 1 | $this->csvExportService->exportHeader(); |
|
| 328 | |||
| 329 | // 受注データ検索用のクエリビルダを取得. |
||
| 330 | 1 | $qb = $this->csvExportService |
|
| 331 | 1 | ->getOrderQueryBuilder($request); |
|
| 332 | |||
| 333 | // データ行の出力. |
||
| 334 | 1 | $this->csvExportService->setExportQueryBuilder($qb); |
|
| 335 | 1 | View Code Duplication | $this->csvExportService->exportData(function ($entity, $csvService) use ($request) { |
| 336 | 1 | $Csvs = $csvService->getCsvs(); |
|
| 337 | |||
| 338 | 1 | $Order = $entity; |
|
| 339 | 1 | $OrderItems = $Order->getOrderItems(); |
|
| 340 | |||
| 341 | 1 | foreach ($OrderItems as $OrderItem) { |
|
| 342 | 1 | $ExportCsvRow = new \Eccube\Entity\ExportCsvRow(); |
|
| 343 | |||
| 344 | // CSV出力項目と合致するデータを取得. |
||
| 345 | 1 | foreach ($Csvs as $Csv) { |
|
| 346 | // 受注データを検索. |
||
| 347 | 1 | $ExportCsvRow->setData($csvService->getData($Csv, $Order)); |
|
| 348 | 1 | if ($ExportCsvRow->isDataNull()) { |
|
| 349 | // 受注データにない場合は, 受注明細を検索. |
||
| 350 | 1 | $ExportCsvRow->setData($csvService->getData($Csv, $OrderItem)); |
|
| 351 | } |
||
| 352 | |||
| 353 | 1 | $event = new EventArgs( |
|
| 354 | [ |
||
| 355 | 1 | 'csvService' => $csvService, |
|
| 356 | 1 | 'Csv' => $Csv, |
|
| 357 | 1 | 'OrderItem' => $OrderItem, |
|
| 358 | 1 | 'ExportCsvRow' => $ExportCsvRow, |
|
| 359 | ], |
||
| 360 | 1 | $request |
|
| 361 | ); |
||
| 362 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_CSV_EXPORT_ORDER, $event); |
|
| 363 | |||
| 364 | 1 | $ExportCsvRow->pushData(); |
|
| 365 | } |
||
| 366 | |||
| 367 | //$row[] = number_format(memory_get_usage(true)); |
||
| 368 | // 出力. |
||
| 369 | 1 | $csvService->fputcsv($ExportCsvRow->getRow()); |
|
| 370 | } |
||
| 371 | 1 | }); |
|
| 372 | 1 | }); |
|
| 373 | |||
| 374 | 1 | $now = new \DateTime(); |
|
| 375 | 1 | $filename = 'order_'.$now->format('YmdHis').'.csv'; |
|
| 376 | 1 | $response->headers->set('Content-Type', 'application/octet-stream'); |
|
| 377 | 1 | $response->headers->set('Content-Disposition', 'attachment; filename='.$filename); |
|
| 378 | 1 | $response->send(); |
|
| 379 | |||
| 380 | 1 | log_info('受注CSV出力ファイル名', [$filename]); |
|
| 381 | |||
| 382 | 1 | return $response; |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * 配送CSVの出力. |
||
| 387 | * |
||
| 388 | * @Route("/%eccube_admin_route%/order/export/shipping", name="admin_order_export_shipping") |
||
| 389 | * |
||
| 390 | * @param Request $request |
||
| 391 | * |
||
| 392 | * @return StreamedResponse |
||
| 393 | */ |
||
| 394 | 1 | public function exportShipping(Request $request) |
|
| 395 | { |
||
| 396 | // タイムアウトを無効にする. |
||
| 397 | 1 | set_time_limit(0); |
|
| 398 | |||
| 399 | // sql loggerを無効にする. |
||
| 400 | 1 | $em = $this->entityManager; |
|
| 401 | 1 | $em->getConfiguration()->setSQLLogger(null); |
|
| 402 | |||
| 403 | 1 | $response = new StreamedResponse(); |
|
| 404 | 1 | $response->setCallback(function () use ($request) { |
|
| 405 | // CSV種別を元に初期化. |
||
| 406 | 1 | $this->csvExportService->initCsvType(CsvType::CSV_TYPE_SHIPPING); |
|
| 407 | |||
| 408 | // ヘッダ行の出力. |
||
| 409 | 1 | $this->csvExportService->exportHeader(); |
|
| 410 | |||
| 411 | // 受注データ検索用のクエリビルダを取得. |
||
| 412 | 1 | $qb = $this->csvExportService |
|
| 413 | 1 | ->getShippingQueryBuilder($request); |
|
| 414 | |||
| 415 | // データ行の出力. |
||
| 416 | 1 | $this->csvExportService->setExportQueryBuilder($qb); |
|
| 417 | 1 | $this->csvExportService->exportData(function ($entity, $csvService) use ($request) { |
|
| 418 | /** @var Csv[] $Csvs */ |
||
| 419 | 1 | $Csvs = $csvService->getCsvs(); |
|
| 420 | |||
| 421 | /** @var Shipping $Shipping */ |
||
| 422 | 1 | $Shipping = $entity; |
|
| 423 | /** @var OrderItem[] $OrderItems */ |
||
| 424 | 1 | $OrderItems = $Shipping->getOrderItems(); |
|
| 425 | |||
| 426 | 1 | foreach ($OrderItems as $OrderItem) { |
|
| 427 | 1 | $ExportCsvRow = new \Eccube\Entity\ExportCsvRow(); |
|
| 428 | |||
| 429 | 1 | $Order = $OrderItem->getOrder(); |
|
| 430 | // CSV出力項目と合致するデータを取得. |
||
| 431 | 1 | foreach ($Csvs as $Csv) { |
|
| 432 | // 受注データを検索. |
||
| 433 | 1 | $ExportCsvRow->setData($csvService->getData($Csv, $Order)); |
|
| 434 | |||
| 435 | 1 | if ($ExportCsvRow->isDataNull()) { |
|
| 436 | // 受注データにない場合は, 出荷データを検索. |
||
| 437 | 1 | $ExportCsvRow->setData($csvService->getData($Csv, $Shipping)); |
|
| 438 | } |
||
| 439 | 1 | if ($ExportCsvRow->isDataNull()) { |
|
| 440 | // 出荷データにない場合は, 出荷明細を検索. |
||
| 441 | 1 | $ExportCsvRow->setData($csvService->getData($Csv, $OrderItem)); |
|
| 442 | } |
||
| 443 | |||
| 444 | 1 | $event = new EventArgs( |
|
| 445 | [ |
||
| 446 | 1 | 'csvService' => $csvService, |
|
| 447 | 1 | 'Csv' => $Csv, |
|
| 448 | 1 | 'OrderItem' => $OrderItem, |
|
| 449 | 1 | 'ExportCsvRow' => $ExportCsvRow, |
|
| 450 | ], |
||
| 451 | 1 | $request |
|
| 452 | ); |
||
| 453 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_CSV_EXPORT_SHIPPING, $event); |
|
| 454 | |||
| 455 | 1 | $ExportCsvRow->pushData(); |
|
| 456 | } |
||
| 457 | //$row[] = number_format(memory_get_usage(true)); |
||
| 458 | // 出力. |
||
| 459 | 1 | $csvService->fputcsv($ExportCsvRow->getRow()); |
|
| 460 | } |
||
| 461 | 1 | }); |
|
| 462 | 1 | }); |
|
| 463 | |||
| 464 | 1 | $now = new \DateTime(); |
|
| 465 | 1 | $filename = 'shipping_'.$now->format('YmdHis').'.csv'; |
|
| 466 | 1 | $response->headers->set('Content-Type', 'application/octet-stream'); |
|
| 467 | 1 | $response->headers->set('Content-Disposition', 'attachment; filename='.$filename); |
|
| 468 | 1 | $response->send(); |
|
| 469 | |||
| 470 | 1 | log_info('出荷CSV出力ファイル名', [$filename]); |
|
| 471 | |||
| 472 | 1 | return $response; |
|
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Bulk action to order status |
||
| 477 | * |
||
| 478 | * @Method("POST") |
||
| 479 | * @Route("/%eccube_admin_route%/order/bulk/order-status/{id}", requirements={"id" = "\d+"}, name="admin_order_bulk_order_status") |
||
| 480 | * |
||
| 481 | * @param Request $request |
||
| 482 | * @param OrderStatus $OrderStatus |
||
| 483 | * |
||
| 484 | * @return RedirectResponse |
||
| 485 | */ |
||
| 486 | 2 | public function bulkOrderStatus(Request $request, OrderStatus $OrderStatus) |
|
| 560 | } |
||
| 561 |