| Conditions | 30 |
| Paths | 13 |
| Total Lines | 258 |
| Lines | 57 |
| Ratio | 22.09 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 272 | public function edit(Application $app, Request $request, $id) |
||
| 273 | { |
||
| 274 | |||
| 275 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 276 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
||
| 277 | $softDeleteFilter->setExcludes(array( |
||
| 278 | 'Eccube\Entity\TaxRule', |
||
| 279 | )); |
||
| 280 | |||
| 281 | /** @var $Product \Eccube\Entity\Product */ |
||
| 282 | $Product = $app['eccube.repository.product']->find($id); |
||
| 283 | |||
| 284 | if (!$Product) { |
||
| 285 | throw new NotFoundHttpException('商品が存在しません'); |
||
| 286 | } |
||
| 287 | |||
| 288 | /* @var FormBuilder $builder */ |
||
| 289 | $builder = $app['form.factory']->createBuilder(); |
||
| 290 | $builder->add('product_classes', 'collection', array( |
||
| 291 | 'type' => 'admin_product_class', |
||
| 292 | 'allow_add' => true, |
||
| 293 | 'allow_delete' => true, |
||
| 294 | )); |
||
| 295 | |||
| 296 | $event = new EventArgs( |
||
| 297 | array( |
||
| 298 | 'builder' => $builder, |
||
| 299 | 'Product' => $Product, |
||
| 300 | ), |
||
| 301 | $request |
||
| 302 | ); |
||
| 303 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_INITIALIZE, $event); |
||
| 304 | |||
| 305 | $form = $builder->getForm(); |
||
| 306 | |||
| 307 | $ProductClasses = $this->getProductClassesExcludeNonClass($Product); |
||
| 308 | |||
| 309 | $form->handleRequest($request); |
||
| 310 | if ($form->isSubmitted()) { |
||
| 311 | switch ($request->get('mode')) { |
||
| 312 | case 'edit': |
||
| 313 | // 新規登録 |
||
| 314 | log_info('商品規格新規登録開始', array($id)); |
||
| 315 | |||
| 316 | View Code Duplication | if (count($ProductClasses) > 0) { |
|
| 317 | // 既に登録されていれば最初の画面に戻す |
||
| 318 | log_info('商品規格登録済', array($id)); |
||
| 319 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 320 | } |
||
| 321 | |||
| 322 | $addProductClasses = array(); |
||
| 323 | |||
| 324 | $tmpProductClass = null; |
||
| 325 | View Code Duplication | foreach ($form->get('product_classes') as $formData) { |
|
| 326 | // 追加対象の行をvalidate |
||
| 327 | $ProductClass = $formData->getData(); |
||
| 328 | |||
| 329 | if ($ProductClass->getAdd()) { |
||
| 330 | if ($formData->isValid()) { |
||
| 331 | $addProductClasses[] = $ProductClass; |
||
| 332 | } else { |
||
| 333 | // 対象行のエラー |
||
| 334 | return $this->render($app, $Product, $ProductClass, true, $form); |
||
| 335 | } |
||
| 336 | } |
||
| 337 | $tmpProductClass = $ProductClass; |
||
| 338 | } |
||
| 339 | |||
| 340 | View Code Duplication | if (count($addProductClasses) == 0) { |
|
| 341 | // 対象がなければエラー |
||
| 342 | log_info('商品規格が未選択', array($id)); |
||
| 343 | $error = array('message' => '商品規格が選択されていません。'); |
||
| 344 | return $this->render($app, $Product, $tmpProductClass, true, $form, $error); |
||
| 345 | } |
||
| 346 | |||
| 347 | // 選択された商品規格を登録 |
||
| 348 | $this->insertProductClass($app, $Product, $addProductClasses); |
||
| 349 | |||
| 350 | // デフォルトの商品規格を更新 |
||
| 351 | $defaultProductClass = $app['eccube.repository.product_class'] |
||
| 352 | ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null)); |
||
| 353 | |||
| 354 | $defaultProductClass->setDelFlg(Constant::ENABLED); |
||
| 355 | |||
| 356 | $app['orm.em']->flush(); |
||
| 357 | |||
| 358 | log_info('商品規格新規登録完了', array($id)); |
||
| 359 | |||
| 360 | $event = new EventArgs( |
||
| 361 | array( |
||
| 362 | 'form' => $form, |
||
| 363 | 'Product' => $Product, |
||
| 364 | 'defaultProductClass' => $defaultProductClass, |
||
| 365 | ), |
||
| 366 | $request |
||
| 367 | ); |
||
| 368 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_COMPLETE, $event); |
||
| 369 | |||
| 370 | $app->addSuccess('admin.product.product_class.save.complete', 'admin'); |
||
| 371 | |||
| 372 | break; |
||
| 373 | case 'update': |
||
| 374 | // 更新 |
||
| 375 | log_info('商品規格更新開始', array($id)); |
||
| 376 | |||
| 377 | View Code Duplication | if (count($ProductClasses) == 0) { |
|
| 378 | // 商品規格が0件であれば最初の画面に戻す |
||
| 379 | log_info('商品規格が存在しません', array($id)); |
||
| 380 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 381 | } |
||
| 382 | |||
| 383 | $checkProductClasses = array(); |
||
| 384 | $removeProductClasses = array(); |
||
| 385 | |||
| 386 | $tempProductClass = null; |
||
| 387 | View Code Duplication | foreach ($form->get('product_classes') as $formData) { |
|
| 388 | // 追加対象の行をvalidate |
||
| 389 | $ProductClass = $formData->getData(); |
||
| 390 | |||
| 391 | if ($ProductClass->getAdd()) { |
||
| 392 | if ($formData->isValid()) { |
||
| 393 | $checkProductClasses[] = $ProductClass; |
||
| 394 | } else { |
||
| 395 | return $this->render($app, $Product, $ProductClass, false, $form); |
||
| 396 | } |
||
| 397 | } else { |
||
| 398 | // 削除対象の行 |
||
| 399 | $removeProductClasses[] = $ProductClass; |
||
| 400 | } |
||
| 401 | $tempProductClass = $ProductClass; |
||
| 402 | } |
||
| 403 | |||
| 404 | View Code Duplication | if (count($checkProductClasses) == 0) { |
|
| 405 | // 対象がなければエラー |
||
| 406 | log_info('商品規格が存在しません', array($id)); |
||
| 407 | $error = array('message' => '商品規格が選択されていません。'); |
||
| 408 | return $this->render($app, $Product, $tempProductClass, false, $form, $error); |
||
| 409 | } |
||
| 410 | |||
| 411 | |||
| 412 | // 登録対象と更新対象の行か判断する |
||
| 413 | $addProductClasses = array(); |
||
| 414 | $updateProductClasses = array(); |
||
| 415 | foreach ($checkProductClasses as $cp) { |
||
| 416 | $flag = false; |
||
| 417 | |||
| 418 | // 既に登録済みの商品規格か確認 |
||
| 419 | foreach ($ProductClasses as $productClass) { |
||
| 420 | if ($productClass->getProduct()->getId() == $id && |
||
| 421 | $productClass->getClassCategory1() == $cp->getClassCategory1() && |
||
| 422 | $productClass->getClassCategory2() == $cp->getClassCategory2()) { |
||
| 423 | $updateProductClasses[] = $cp; |
||
| 424 | |||
| 425 | // 商品情報 |
||
| 426 | $cp->setProduct($Product); |
||
| 427 | // 商品在庫 |
||
| 428 | $productStock = $productClass->getProductStock(); |
||
| 429 | if (!$cp->getStockUnlimited()) { |
||
| 430 | $productStock->setStock($cp->getStock()); |
||
| 431 | } else { |
||
| 432 | $productStock->setStock(null); |
||
| 433 | } |
||
| 434 | $this->setDefualtProductClass($app, $productClass, $cp); |
||
| 435 | $flag = true; |
||
| 436 | break; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | if (!$flag) { |
||
| 440 | $addProductClasses[] = $cp; |
||
| 441 | } |
||
| 442 | } |
||
| 443 | |||
| 444 | foreach ($removeProductClasses as $rc) { |
||
| 445 | // 登録されている商品規格に削除フラグをセット |
||
| 446 | foreach ($ProductClasses as $productClass) { |
||
| 447 | if ($productClass->getProduct()->getId() == $id && |
||
| 448 | $productClass->getClassCategory1() == $rc->getClassCategory1() && |
||
| 449 | $productClass->getClassCategory2() == $rc->getClassCategory2()) { |
||
| 450 | |||
| 451 | $productClass->setDelFlg(Constant::ENABLED); |
||
| 452 | break; |
||
| 453 | } |
||
| 454 | } |
||
| 455 | } |
||
| 456 | |||
| 457 | // 選択された商品規格を登録 |
||
| 458 | $this->insertProductClass($app, $Product, $addProductClasses); |
||
| 459 | |||
| 460 | $app['orm.em']->flush(); |
||
| 461 | |||
| 462 | log_info('商品規格更新完了', array($id)); |
||
| 463 | |||
| 464 | $event = new EventArgs( |
||
| 465 | array( |
||
| 466 | 'form' => $form, |
||
| 467 | 'Product' => $Product, |
||
| 468 | 'updateProductClasses' => $updateProductClasses, |
||
| 469 | 'addProductClasses' => $addProductClasses, |
||
| 470 | ), |
||
| 471 | $request |
||
| 472 | ); |
||
| 473 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE, $event); |
||
| 474 | |||
| 475 | $app->addSuccess('admin.product.product_class.update.complete', 'admin'); |
||
| 476 | |||
| 477 | break; |
||
| 478 | |||
| 479 | case 'delete': |
||
| 480 | // 削除 |
||
| 481 | log_info('商品規格削除開始', array($id)); |
||
| 482 | |||
| 483 | View Code Duplication | if (count($ProductClasses) == 0) { |
|
| 484 | // 既に商品が削除されていれば元の画面に戻す |
||
| 485 | log_info('商品規格が存在しません', array($id)); |
||
| 486 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 487 | } |
||
| 488 | |||
| 489 | foreach ($ProductClasses as $ProductClass) { |
||
| 490 | // 登録されている商品規格に削除フラグをセット |
||
| 491 | $ProductClass->setDelFlg(Constant::ENABLED); |
||
| 492 | } |
||
| 493 | |||
| 494 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 495 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
||
| 496 | $softDeleteFilter->setExcludes(array( |
||
| 497 | 'Eccube\Entity\ProductClass' |
||
| 498 | )); |
||
| 499 | |||
| 500 | // デフォルトの商品規格を更新 |
||
| 501 | $defaultProductClass = $app['eccube.repository.product_class'] |
||
| 502 | ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null, 'del_flg' => Constant::ENABLED)); |
||
| 503 | |||
| 504 | $defaultProductClass->setDelFlg(Constant::DISABLED); |
||
| 505 | |||
| 506 | $app['orm.em']->flush(); |
||
| 507 | log_info('商品規格削除完了', array($id)); |
||
| 508 | |||
| 509 | $event = new EventArgs( |
||
| 510 | array( |
||
| 511 | 'form' => $form, |
||
| 512 | 'Product' => $Product, |
||
| 513 | 'defaultProductClass' => $defaultProductClass, |
||
| 514 | ), |
||
| 515 | $request |
||
| 516 | ); |
||
| 517 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_DELETE, $event); |
||
| 518 | |||
| 519 | $app->addSuccess('admin.product.product_class.delete.complete', 'admin'); |
||
| 520 | |||
| 521 | break; |
||
| 522 | default: |
||
| 523 | break; |
||
| 524 | } |
||
| 525 | |||
| 526 | } |
||
| 527 | |||
| 528 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 529 | } |
||
| 530 | |||
| 781 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.