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 ShoppingController 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 ShoppingController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 61 | class ShoppingController extends AbstractShoppingController |
||
| 62 | { |
||
| 63 | /** |
||
| 64 | * @Inject(BaseInfoRepository::class) |
||
| 65 | * @var BaseInfoRepository |
||
| 66 | */ |
||
| 67 | protected $baseInfoRepository; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @Inject(OrderHelper::class) |
||
| 71 | * @var OrderHelper |
||
| 72 | */ |
||
| 73 | protected $orderHelper; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @Inject(CartService::class) |
||
| 77 | * @var CartService |
||
| 78 | */ |
||
| 79 | protected $cartService; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @Inject("form.factory") |
||
| 83 | * @var FormFactory |
||
| 84 | */ |
||
| 85 | protected $formFactory; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @Inject("orm.em") |
||
| 89 | * @var EntityManager |
||
| 90 | */ |
||
| 91 | protected $entityManager; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @Inject("config") |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $appConfig; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @Inject(ShoppingService::class) |
||
| 101 | * @var ShoppingService |
||
| 102 | */ |
||
| 103 | protected $shoppingService; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @Inject(CustomerAddressRepository::class) |
||
| 107 | * @var CustomerAddressRepository |
||
| 108 | */ |
||
| 109 | protected $customerAddressRepository; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @Inject("eccube.event.dispatcher") |
||
| 113 | * @var EventDispatcher |
||
| 114 | */ |
||
| 115 | protected $eventDispatcher; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @Inject("session") |
||
| 119 | * @var Session |
||
| 120 | */ |
||
| 121 | protected $session; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @Inject("request_scope") |
||
| 125 | * @var ParameterBag |
||
| 126 | */ |
||
| 127 | protected $parameterBag; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * 購入画面表示 |
||
| 131 | * |
||
| 132 | * @Route("/shopping", name="shopping") |
||
| 133 | * @Template("Shopping/index.twig") |
||
| 134 | */ |
||
| 135 | 13 | View Code Duplication | public function index(Application $app, Request $request) |
| 171 | |||
| 172 | /** |
||
| 173 | * 購入確認画面から, 他の画面へのリダイレクト. |
||
| 174 | * 配送業者や支払方法、お問い合わせ情報をDBに保持してから遷移する. |
||
| 175 | * |
||
| 176 | * @Route("/shopping/redirect", name="shopping_redirect_to") |
||
| 177 | * @Template("Shopping/index.twig") |
||
| 178 | */ |
||
| 179 | 5 | View Code Duplication | public function redirectTo(Application $app, Request $request) |
| 211 | |||
| 212 | /** |
||
| 213 | * 購入処理 |
||
| 214 | * |
||
| 215 | * @Route("/shopping/confirm", name="shopping_confirm") |
||
| 216 | * @Method("POST") |
||
| 217 | * @Template("Shopping/index.twig") |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function confirm(Application $app, Request $request) |
|
| 220 | { |
||
| 221 | // カートチェック |
||
| 222 | $response = $app->forward($app->path("shopping_check_to_cart")); |
||
| 223 | if ($response->isRedirection() || $response->getContent()) { |
||
| 224 | return $response; |
||
| 225 | } |
||
| 226 | |||
| 227 | // 受注の存在チェック |
||
| 228 | $response = $app->forward($app->path("shopping_exists_order")); |
||
| 229 | if ($response->isRedirection() || $response->getContent()) { |
||
| 230 | return $response; |
||
| 231 | } |
||
| 232 | |||
| 233 | // form作成 |
||
| 234 | // FIXME イベントハンドラを外から渡したい |
||
| 235 | $app->forward($app->path("shopping_create_form")); |
||
| 236 | |||
| 237 | $form = $this->parameterBag->get(OrderType::class); |
||
| 238 | $Order = $this->parameterBag->get('Order'); |
||
| 239 | |||
| 240 | $form->handleRequest($request); |
||
| 241 | |||
| 242 | // 受注処理 |
||
| 243 | $response = $app->forward($app->path("shopping_complete_order")); |
||
| 244 | if ($response->isRedirection() || $response->getContent()) { |
||
| 245 | return $response; |
||
| 246 | } |
||
| 247 | |||
| 248 | log_info('購入チェックエラー', array($Order->getId())); |
||
| 249 | |||
| 250 | return [ |
||
| 251 | 'form' => $form->createView(), |
||
| 252 | 'Order' => $Order, |
||
| 253 | ]; |
||
| 254 | } |
||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * 購入完了画面表示 |
||
| 259 | * |
||
| 260 | * @Route("/shopping/complete", name="shopping_complete") |
||
| 261 | * @Template("Shopping/complete.twig") |
||
| 262 | */ |
||
| 263 | 1 | public function complete(Application $app, Request $request) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * お届け先の設定一覧からの選択 |
||
| 297 | * |
||
| 298 | * @Route("/shopping/shipping/{id}", name="shopping_shipping", requirements={"id" = "\d+"}) |
||
| 299 | * @Template("Shopping/shipping.twig") |
||
| 300 | */ |
||
| 301 | public function shipping(Application $app, Request $request, $id) |
||
| 302 | { |
||
| 303 | // カートチェック |
||
| 304 | $response = $app->forward($app->path("shopping_check_to_cart")); |
||
| 305 | if ($response->isRedirection() || $response->getContent()) { |
||
| 306 | return $response; |
||
| 307 | } |
||
| 308 | |||
| 309 | if ('POST' === $request->getMethod()) { |
||
| 310 | $address = $request->get('address'); |
||
| 311 | |||
| 312 | if (is_null($address)) { |
||
| 313 | // 選択されていなければエラー |
||
| 314 | log_info('お届け先入力チェックエラー'); |
||
| 315 | |||
| 316 | return [ |
||
| 317 | 'Customer' => $app->user(), |
||
| 318 | 'shippingId' => $id, |
||
| 319 | 'error' => true, |
||
| 320 | ]; |
||
| 321 | } |
||
| 322 | |||
| 323 | // 選択されたお届け先情報を取得 |
||
| 324 | $CustomerAddress = $this->customerAddressRepository->findOneBy( |
||
| 325 | array( |
||
| 326 | 'Customer' => $app->user(), |
||
| 327 | 'id' => $address, |
||
| 328 | ) |
||
| 329 | ); |
||
| 330 | if (is_null($CustomerAddress)) { |
||
| 331 | throw new NotFoundHttpException('選択されたお届け先住所が存在しない'); |
||
| 332 | } |
||
| 333 | |||
| 334 | /** @var Order $Order */ |
||
| 335 | $Order = $this->shoppingService->getOrder($this->appConfig['order_processing']); |
||
| 336 | if (!$Order) { |
||
| 337 | log_info('購入処理中の受注情報がないため購入エラー'); |
||
| 338 | $app->addError('front.shopping.order.error'); |
||
| 339 | |||
| 340 | return $app->redirect($app->url('shopping_error')); |
||
| 341 | } |
||
| 342 | |||
| 343 | $Shipping = $Order->findShipping($id); |
||
| 344 | if (!$Shipping) { |
||
| 345 | throw new NotFoundHttpException('お届け先情報が存在しない'); |
||
| 346 | } |
||
| 347 | |||
| 348 | log_info('お届先情報更新開始', array($Shipping->getId())); |
||
| 349 | |||
| 350 | // お届け先情報を更新 |
||
| 351 | $Shipping->setFromCustomerAddress($CustomerAddress); |
||
| 352 | |||
| 353 | // 配送料金の設定 |
||
| 354 | $this->shoppingService->setShippingDeliveryFee($Shipping); |
||
| 355 | |||
| 356 | |||
| 357 | // 合計金額の再計算 |
||
| 358 | $flowResult = $this->executePurchaseFlow($app, $Order); |
||
| 359 | if ($flowResult->hasWarning() || $flowResult->hasError()) { |
||
| 360 | return $app->redirect($app->url('shopping_error')); |
||
| 361 | } |
||
| 362 | |||
| 363 | // 配送先を更新 |
||
| 364 | $this->entityManager->flush(); |
||
| 365 | |||
| 366 | $event = new EventArgs( |
||
| 367 | array( |
||
| 368 | 'Order' => $Order, |
||
| 369 | 'shippingId' => $id, |
||
| 370 | ), |
||
| 371 | $request |
||
| 372 | ); |
||
| 373 | $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE, $event); |
||
| 374 | |||
| 375 | log_info('お届先情報更新完了', array($Shipping->getId())); |
||
| 376 | |||
| 377 | return $app->redirect($app->url('shopping')); |
||
| 378 | } |
||
| 379 | |||
| 380 | return [ |
||
| 381 | 'Customer' => $app->user(), |
||
| 382 | 'shippingId' => $id, |
||
| 383 | 'error' => false, |
||
| 384 | ]; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * お届け先の設定(非会員でも使用する) |
||
| 389 | * |
||
| 390 | * @Route("/shopping/shipping_edit/{id}", name="shopping_shipping_edit", requirements={"id" = "\d+"}) |
||
| 391 | * @Template("Shopping/shipping_edit.twig") |
||
| 392 | */ |
||
| 393 | public function shippingEdit(Application $app, Request $request, $id) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * ログイン |
||
| 506 | * |
||
| 507 | * @Route("/shopping/login", name="shopping_login") |
||
| 508 | * @Template("Shopping/login.twig") |
||
| 509 | */ |
||
| 510 | 2 | public function login(Application $app, Request $request) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * 購入エラー画面表示 |
||
| 548 | * |
||
| 549 | * @Route("/shopping/error", name="shopping_error") |
||
| 550 | * @Template("Shopping/shopping_error.twig") |
||
| 551 | */ |
||
| 552 | 1 | public function shoppingError(Application $app, Request $request) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * カート画面のチェック |
||
| 569 | * |
||
| 570 | * @Route("/shopping/check_to_cart", name="shopping_check_to_cart") |
||
| 571 | */ |
||
| 572 | 17 | public function checkToCart(Application $app, Request $request) |
|
| 592 | |||
| 593 | /** |
||
| 594 | * 受注情報を初期化する. |
||
| 595 | * |
||
| 596 | * @Route("/shopping/initialize_order", name="shopping_initialize_order") |
||
| 597 | */ |
||
| 598 | 10 | public function initializeOrder(Application $app, Request $request) |
|
| 648 | |||
| 649 | /** |
||
| 650 | * フォームを作成し, イベントハンドラを設定する |
||
| 651 | * |
||
| 652 | * @Route("/shopping/create_form", name="shopping_create_form") |
||
| 653 | */ |
||
| 654 | 10 | public function createForm(Application $app, Request $request) |
|
| 675 | |||
| 676 | /** |
||
| 677 | * mode に応じて各変更ページへリダイレクトする. |
||
| 678 | * |
||
| 679 | * @Route("/shopping/redirect_to_change", name="shopping_redirect_to_change") |
||
| 680 | */ |
||
| 681 | 5 | public function redirectToChange(Application $app, Request $request) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * 複数配送時のエラーを表示する |
||
| 720 | * |
||
| 721 | * @Route("/shopping/handle_multiple_errors", name="shopping_handle_multiple_errors") |
||
| 722 | */ |
||
| 723 | 10 | public function handleMultipleErrors(Application $app, Request $request) |
|
| 747 | |||
| 748 | /** |
||
| 749 | * 受注の存在チェック |
||
| 750 | * |
||
| 751 | * @Route("/shopping/exists_order", name="shopping_exists_order") |
||
| 752 | */ |
||
| 753 | 5 | public function existsOrder(Application $app, Request $request) |
|
| 766 | |||
| 767 | /** |
||
| 768 | * 受注完了処理 |
||
| 769 | * |
||
| 770 | * @Route("/shopping/complete_order", name="shopping_complete_order") |
||
| 771 | */ |
||
| 772 | public function completeOrder(Application $app, Request $request) |
||
| 862 | |||
| 863 | /** |
||
| 864 | * 受注完了の後処理 |
||
| 865 | * |
||
| 866 | * @Route("/shopping/after_complete", name="shopping_after_complete") |
||
| 867 | */ |
||
| 868 | public function afterComplete(Application $app, Request $request) |
||
| 916 | } |
||
| 917 |