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 ProductClassController 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 ProductClassController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class ProductClassController |
||
|
|
|||
| 45 | { |
||
| 46 | /** |
||
| 47 | * 商品規格が登録されていなければ新規登録、登録されていれば更新画面を表示する |
||
| 48 | */ |
||
| 49 | 11 | public function index(Application $app, Request $request, $id) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * 商品規格の登録、更新、削除を行う |
||
| 264 | * |
||
| 265 | * @param Application $app |
||
| 266 | * @param Request $request |
||
| 267 | * @param int $id |
||
| 268 | * @return RedirectResponse |
||
| 269 | */ |
||
| 270 | 12 | public function edit(Application $app, Request $request, $id) |
|
| 271 | { |
||
| 272 | |||
| 273 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 274 | 12 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
|
| 275 | 12 | $softDeleteFilter->setExcludes(array( |
|
| 276 | 12 | 'Eccube\Entity\TaxRule', |
|
| 277 | )); |
||
| 278 | |||
| 279 | /** @var $Product \Eccube\Entity\Product */ |
||
| 280 | 12 | $Product = $app['eccube.repository.product']->find($id); |
|
| 281 | |||
| 282 | 12 | if (!$Product) { |
|
| 283 | throw new NotFoundHttpException('商品が存在しません'); |
||
| 284 | } |
||
| 285 | |||
| 286 | /* @var FormBuilder $builder */ |
||
| 287 | 12 | $builder = $app['form.factory']->createBuilder(); |
|
| 288 | 12 | $builder->add('product_classes', 'collection', array( |
|
| 289 | 12 | 'type' => 'admin_product_class', |
|
| 290 | 'allow_add' => true, |
||
| 291 | 'allow_delete' => true, |
||
| 292 | )); |
||
| 293 | |||
| 294 | 12 | $event = new EventArgs( |
|
| 295 | array( |
||
| 296 | 12 | 'builder' => $builder, |
|
| 297 | 12 | 'Product' => $Product, |
|
| 298 | ), |
||
| 299 | $request |
||
| 300 | ); |
||
| 301 | 12 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_INITIALIZE, $event); |
|
| 302 | |||
| 303 | 12 | $form = $builder->getForm(); |
|
| 304 | |||
| 305 | 12 | $ProductClasses = $this->getProductClassesExcludeNonClass($Product); |
|
| 306 | |||
| 307 | 12 | $form->handleRequest($request); |
|
| 308 | 12 | if ($form->isSubmitted()) { |
|
| 309 | 10 | switch ($request->get('mode')) { |
|
| 310 | 10 | case 'edit': |
|
| 311 | // 新規登録 |
||
| 312 | 4 | log_info('商品規格新規登録開始', array($id)); |
|
| 313 | |||
| 314 | 4 | View Code Duplication | if (count($ProductClasses) > 0) { |
| 315 | // 既に登録されていれば最初の画面に戻す |
||
| 316 | log_info('商品規格登録済', array($id)); |
||
| 317 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 318 | } |
||
| 319 | |||
| 320 | 4 | $addProductClasses = array(); |
|
| 321 | |||
| 322 | 4 | $tmpProductClass = null; |
|
| 323 | 4 | View Code Duplication | foreach ($form->get('product_classes') as $formData) { |
| 324 | // 追加対象の行をvalidate |
||
| 325 | 4 | $ProductClass = $formData->getData(); |
|
| 326 | |||
| 327 | 4 | if ($ProductClass->getAdd()) { |
|
| 328 | 4 | if ($formData->isValid()) { |
|
| 329 | 3 | $addProductClasses[] = $ProductClass; |
|
| 330 | } else { |
||
| 331 | // 対象行のエラー |
||
| 332 | 1 | return $this->render($app, $Product, $ProductClass, true, $form); |
|
| 333 | } |
||
| 334 | } |
||
| 335 | 3 | $tmpProductClass = $ProductClass; |
|
| 336 | } |
||
| 337 | |||
| 338 | 3 | View Code Duplication | if (count($addProductClasses) == 0) { |
| 339 | // 対象がなければエラー |
||
| 340 | log_info('商品規格が未選択', array($id)); |
||
| 341 | $error = array('message' => '商品規格が選択されていません。'); |
||
| 342 | return $this->render($app, $Product, $tmpProductClass, true, $form, $error); |
||
| 343 | } |
||
| 344 | |||
| 345 | // 選択された商品規格を登録 |
||
| 346 | 3 | $this->insertProductClass($app, $Product, $addProductClasses); |
|
| 347 | |||
| 348 | // デフォルトの商品規格を更新 |
||
| 349 | 3 | $defaultProductClass = $app['eccube.repository.product_class'] |
|
| 350 | 3 | ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null)); |
|
| 351 | |||
| 352 | 3 | $defaultProductClass->setDelFlg(Constant::ENABLED); |
|
| 353 | |||
| 354 | 3 | $app['orm.em']->flush(); |
|
| 355 | |||
| 356 | 3 | log_info('商品規格新規登録完了', array($id)); |
|
| 357 | |||
| 358 | 3 | $event = new EventArgs( |
|
| 359 | array( |
||
| 360 | 3 | 'form' => $form, |
|
| 361 | 3 | 'Product' => $Product, |
|
| 362 | 3 | 'defaultProductClass' => $defaultProductClass, |
|
| 363 | ), |
||
| 364 | $request |
||
| 365 | ); |
||
| 366 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_COMPLETE, $event); |
|
| 367 | |||
| 368 | 3 | $app->addSuccess('admin.product.product_class.save.complete', 'admin'); |
|
| 369 | |||
| 370 | 3 | break; |
|
| 371 | 6 | case 'update': |
|
| 372 | // 更新 |
||
| 373 | 5 | log_info('商品規格更新開始', array($id)); |
|
| 374 | |||
| 375 | 5 | View Code Duplication | if (count($ProductClasses) == 0) { |
| 376 | // 商品規格が0件であれば最初の画面に戻す |
||
| 377 | log_info('商品規格が存在しません', array($id)); |
||
| 378 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 379 | } |
||
| 380 | |||
| 381 | 5 | $checkProductClasses = array(); |
|
| 382 | 5 | $removeProductClasses = array(); |
|
| 383 | |||
| 384 | 5 | $tempProductClass = null; |
|
| 385 | 5 | View Code Duplication | foreach ($form->get('product_classes') as $formData) { |
| 386 | // 追加対象の行をvalidate |
||
| 387 | 5 | $ProductClass = $formData->getData(); |
|
| 388 | |||
| 389 | 5 | if ($ProductClass->getAdd()) { |
|
| 390 | 5 | if ($formData->isValid()) { |
|
| 391 | 4 | $checkProductClasses[] = $ProductClass; |
|
| 392 | } else { |
||
| 393 | 5 | return $this->render($app, $Product, $ProductClass, false, $form); |
|
| 394 | } |
||
| 395 | } else { |
||
| 396 | // 削除対象の行 |
||
| 397 | 1 | $removeProductClasses[] = $ProductClass; |
|
| 398 | } |
||
| 399 | 4 | $tempProductClass = $ProductClass; |
|
| 400 | } |
||
| 401 | |||
| 402 | 4 | View Code Duplication | if (count($checkProductClasses) == 0) { |
| 403 | // 対象がなければエラー |
||
| 404 | log_info('商品規格が存在しません', array($id)); |
||
| 405 | $error = array('message' => '商品規格が選択されていません。'); |
||
| 406 | return $this->render($app, $Product, $tempProductClass, false, $form, $error); |
||
| 407 | } |
||
| 408 | |||
| 409 | |||
| 410 | // 登録対象と更新対象の行か判断する |
||
| 411 | 4 | $addProductClasses = array(); |
|
| 412 | 4 | $updateProductClasses = array(); |
|
| 413 | 4 | foreach ($checkProductClasses as $cp) { |
|
| 414 | 4 | $flag = false; |
|
| 415 | |||
| 416 | // 既に登録済みの商品規格か確認 |
||
| 417 | 4 | foreach ($ProductClasses as $productClass) { |
|
| 418 | 4 | if ($productClass->getProduct()->getId() == $id && |
|
| 419 | 4 | $productClass->getClassCategory1() == $cp->getClassCategory1() && |
|
| 420 | 4 | $productClass->getClassCategory2() == $cp->getClassCategory2()) { |
|
| 421 | 4 | $updateProductClasses[] = $cp; |
|
| 422 | |||
| 423 | // 商品情報 |
||
| 424 | 4 | $cp->setProduct($Product); |
|
| 425 | // 商品在庫 |
||
| 426 | 4 | $productStock = $productClass->getProductStock(); |
|
| 427 | 4 | if (!$cp->getStockUnlimited()) { |
|
| 428 | 1 | $productStock->setStock($cp->getStock()); |
|
| 429 | } else { |
||
| 430 | 3 | $productStock->setStock(null); |
|
| 431 | } |
||
| 432 | 4 | $this->setDefualtProductClass($app, $productClass, $cp); |
|
| 433 | 4 | $flag = true; |
|
| 434 | 4 | break; |
|
| 435 | } |
||
| 436 | } |
||
| 437 | 4 | if (!$flag) { |
|
| 438 | 4 | $addProductClasses[] = $cp; |
|
| 439 | } |
||
| 440 | } |
||
| 441 | |||
| 442 | 4 | foreach ($removeProductClasses as $rc) { |
|
| 443 | // 登録されている商品規格に削除フラグをセット |
||
| 444 | 1 | foreach ($ProductClasses as $productClass) { |
|
| 445 | 1 | if ($productClass->getProduct()->getId() == $id && |
|
| 446 | 1 | $productClass->getClassCategory1() == $rc->getClassCategory1() && |
|
| 447 | 1 | $productClass->getClassCategory2() == $rc->getClassCategory2()) { |
|
| 448 | |||
| 449 | 1 | $productClass->setDelFlg(Constant::ENABLED); |
|
| 450 | 4 | break; |
|
| 451 | } |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | // 選択された商品規格を登録 |
||
| 456 | 4 | $this->insertProductClass($app, $Product, $addProductClasses); |
|
| 457 | |||
| 458 | 4 | $app['orm.em']->flush(); |
|
| 459 | |||
| 460 | 4 | log_info('商品規格更新完了', array($id)); |
|
| 461 | |||
| 462 | 4 | $event = new EventArgs( |
|
| 463 | array( |
||
| 464 | 4 | 'form' => $form, |
|
| 465 | 4 | 'Product' => $Product, |
|
| 466 | 4 | 'updateProductClasses' => $updateProductClasses, |
|
| 467 | 4 | 'addProductClasses' => $addProductClasses, |
|
| 468 | ), |
||
| 469 | $request |
||
| 470 | ); |
||
| 471 | 4 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE, $event); |
|
| 472 | |||
| 473 | 4 | $app->addSuccess('admin.product.product_class.update.complete', 'admin'); |
|
| 474 | |||
| 475 | 4 | break; |
|
| 476 | |||
| 477 | 1 | case 'delete': |
|
| 478 | // 削除 |
||
| 479 | 1 | log_info('商品規格削除開始', array($id)); |
|
| 480 | |||
| 481 | 1 | View Code Duplication | if (count($ProductClasses) == 0) { |
| 482 | // 既に商品が削除されていれば元の画面に戻す |
||
| 483 | log_info('商品規格が存在しません', array($id)); |
||
| 484 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 485 | } |
||
| 486 | |||
| 487 | 1 | foreach ($ProductClasses as $ProductClass) { |
|
| 488 | // 登録されている商品規格に削除フラグをセット |
||
| 489 | 1 | $ProductClass->setDelFlg(Constant::ENABLED); |
|
| 490 | } |
||
| 491 | |||
| 492 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 493 | 1 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
|
| 494 | 1 | $softDeleteFilter->setExcludes(array( |
|
| 495 | 'Eccube\Entity\ProductClass' |
||
| 496 | 1 | )); |
|
| 497 | |||
| 498 | // デフォルトの商品規格を更新 |
||
| 499 | 1 | $defaultProductClass = $app['eccube.repository.product_class'] |
|
| 500 | 1 | ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null, 'del_flg' => Constant::ENABLED)); |
|
| 501 | |||
| 502 | 1 | $defaultProductClass->setDelFlg(Constant::DISABLED); |
|
| 503 | |||
| 504 | 1 | $app['orm.em']->flush(); |
|
| 505 | 1 | log_info('商品規格削除完了', array($id)); |
|
| 506 | |||
| 507 | 1 | $event = new EventArgs( |
|
| 508 | array( |
||
| 509 | 1 | 'form' => $form, |
|
| 510 | 1 | 'Product' => $Product, |
|
| 511 | 1 | 'defaultProductClass' => $defaultProductClass, |
|
| 512 | ), |
||
| 513 | $request |
||
| 514 | ); |
||
| 515 | 1 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_DELETE, $event); |
|
| 516 | |||
| 517 | 1 | $app->addSuccess('admin.product.product_class.delete.complete', 'admin'); |
|
| 518 | |||
| 519 | 1 | break; |
|
| 520 | default: |
||
| 521 | 8 | break; |
|
| 522 | } |
||
| 523 | |||
| 524 | } |
||
| 525 | |||
| 526 | 10 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
|
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * 登録、更新時のエラー画面表示 |
||
| 531 | * |
||
| 532 | */ |
||
| 533 | 2 | protected function render($app, $Product, $ProductClass, $not_product_class, $classForm, $error = null) |
|
| 534 | { |
||
| 535 | |||
| 536 | 2 | $ClassName1 = null; |
|
| 537 | 2 | $ClassName2 = null; |
|
| 538 | // 規格を取得 |
||
| 539 | 2 | if (isset($ProductClass)) { |
|
| 540 | 2 | $ClassCategory1 = $ProductClass->getClassCategory1(); |
|
| 541 | 2 | if ($ClassCategory1) { |
|
| 542 | 2 | $ClassName1 = $ClassCategory1->getClassName(); |
|
| 543 | } |
||
| 544 | 2 | $ClassCategory2 = $ProductClass->getClassCategory2(); |
|
| 545 | 2 | if ($ClassCategory2) { |
|
| 546 | 1 | $ClassName2 = $ClassCategory2->getClassName(); |
|
| 547 | } |
||
| 548 | } |
||
| 549 | |||
| 550 | 2 | $form = $app->form() |
|
| 551 | 2 | ->add('class_name1', 'entity', array( |
|
| 552 | 2 | 'class' => 'Eccube\Entity\ClassName', |
|
| 553 | 2 | 'property' => 'name', |
|
| 554 | 2 | 'empty_value' => '規格1を選択', |
|
| 555 | 2 | 'data' => $ClassName1, |
|
| 556 | )) |
||
| 557 | 2 | ->add('class_name2', 'entity', array( |
|
| 558 | 2 | 'class' => 'Eccube\Entity\ClassName', |
|
| 559 | 2 | 'property' => 'name', |
|
| 560 | 2 | 'empty_value' => '規格2を選択', |
|
| 561 | 2 | 'data' => $ClassName2, |
|
| 562 | )) |
||
| 563 | 2 | ->getForm(); |
|
| 564 | |||
| 565 | 2 | log_info('商品規格登録エラー'); |
|
| 566 | |||
| 567 | |||
| 568 | 2 | return $app->render('Product/product_class.twig', array( |
|
| 569 | 2 | 'form' => $form->createView(), |
|
| 570 | 2 | 'classForm' => $classForm->createView(), |
|
| 571 | 2 | 'Product' => $Product, |
|
| 572 | 2 | 'class_name1' => $ClassName1, |
|
| 573 | 2 | 'class_name2' => $ClassName2, |
|
| 574 | 2 | 'not_product_class' => $not_product_class, |
|
| 575 | 2 | 'error' => $error, |
|
| 576 | 'has_class_category_flg' => true, |
||
| 577 | )); |
||
| 578 | } |
||
| 579 | |||
| 580 | |||
| 581 | /** |
||
| 582 | * 規格1と規格2を組み合わせた商品規格を作成 |
||
| 583 | */ |
||
| 584 | 10 | private function createProductClasses($app, Product $Product, ClassName $ClassName1 = null, ClassName $ClassName2 = null) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * 新しい商品規格を作成 |
||
| 624 | */ |
||
| 625 | 10 | private function newProductClass(Application $app) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * 商品規格のコピーを取得. |
||
| 636 | * |
||
| 637 | * @see http://symfony.com/doc/current/cookbook/form/form_collections.html |
||
| 638 | * @param Product $Product |
||
| 639 | * @return \Eccube\Entity\ProductClass[] |
||
| 640 | */ |
||
| 641 | private function getProductClassesOriginal(Product $Product) |
||
| 648 | |||
| 649 | /** |
||
| 650 | * 規格なし商品を除いて商品規格を取得. |
||
| 651 | * |
||
| 652 | * @param Product $Product |
||
| 653 | * @return \Eccube\Entity\ProductClass[] |
||
| 654 | */ |
||
| 655 | 12 | private function getProductClassesExcludeNonClass(Product $Product) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * デフォルトとなる商品規格を設定 |
||
| 667 | * |
||
| 668 | * @param $productClassDest コピー先となる商品規格 |
||
| 669 | * @param $productClassOrig コピー元となる商品規格 |
||
| 670 | */ |
||
| 671 | 8 | private function setDefualtProductClass($app, $productClassDest, $productClassOrig) { |
|
| 706 | |||
| 707 | |||
| 708 | /** |
||
| 709 | * 商品規格を登録 |
||
| 710 | * |
||
| 711 | * @param Application $app |
||
| 712 | * @param Product $Product |
||
| 713 | * @param ArrayCollection $ProductClasses 登録される商品規格 |
||
| 714 | */ |
||
| 715 | 7 | private function insertProductClass($app, $Product, $ProductClasses) { |
|
| 716 | |||
| 717 | 7 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 718 | |||
| 719 | // 選択された商品を登録 |
||
| 720 | 7 | foreach ($ProductClasses as $ProductClass) { |
|
| 721 | |||
| 722 | 3 | $ProductClass->setDelFlg(Constant::DISABLED); |
|
| 723 | 3 | $ProductClass->setProduct($Product); |
|
| 724 | 3 | $app['orm.em']->persist($ProductClass); |
|
| 725 | |||
| 726 | // 在庫情報を作成 |
||
| 727 | 3 | $ProductStock = new ProductStock(); |
|
| 728 | 3 | $ProductClass->setProductStock($ProductStock); |
|
| 729 | 3 | $ProductStock->setProductClass($ProductClass); |
|
| 730 | 3 | if (!$ProductClass->getStockUnlimited()) { |
|
| 731 | 3 | $ProductStock->setStock($ProductClass->getStock()); |
|
| 732 | } else { |
||
| 733 | // 在庫無制限時はnullを設定 |
||
| 734 | $ProductStock->setStock(null); |
||
| 735 | } |
||
| 736 | 7 | $app['orm.em']->persist($ProductStock); |
|
| 737 | |||
| 738 | } |
||
| 739 | |||
| 740 | // 商品税率が設定されている場合、商品税率をセット |
||
| 741 | 7 | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { |
|
| 742 | // 初期設定の税設定. |
||
| 743 | 7 | $TaxRule = $app['eccube.repository.tax_rule']->find(TaxRule::DEFAULT_TAX_RULE_ID); |
|
| 744 | // 初期税率設定の計算方法を設定する |
||
| 745 | 7 | $CalcRule = $TaxRule->getCalcRule(); |
|
| 746 | 7 | foreach ($ProductClasses as $ProductClass) { |
|
| 747 | 3 | if ($ProductClass && is_numeric($taxRate = $ProductClass->getTaxRate())) { |
|
| 748 | 2 | $TaxRule = new TaxRule(); |
|
| 749 | 2 | $TaxRule->setProduct($Product); |
|
| 750 | 2 | $TaxRule->setProductClass($ProductClass); |
|
| 751 | 2 | $TaxRule->setCalcRule($CalcRule); |
|
| 752 | 2 | $TaxRule->setTaxRate($taxRate); |
|
| 753 | 2 | $TaxRule->setTaxAdjust(0); |
|
| 754 | 2 | $TaxRule->setApplyDate(new \DateTime()); |
|
| 755 | 2 | $TaxRule->setDelFlg(Constant::DISABLED); |
|
| 756 | 7 | $app['orm.em']->persist($TaxRule); |
|
| 757 | } |
||
| 758 | } |
||
| 759 | } |
||
| 760 | |||
| 761 | } |
||
| 762 | |||
| 763 | /** |
||
| 764 | * 規格の分類判定 |
||
| 765 | * |
||
| 766 | * @param $class_name |
||
| 767 | * @return boolean |
||
| 768 | */ |
||
| 769 | 4 | private function isValiedCategory($class_name) |
|
| 779 | } |
||
| 780 |