| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 286 |
| Lines | 31 |
| Ratio | 10.84 % |
| 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 |
||
| 250 | public function edit(Application $app, Request $request, $id = null) |
||
| 251 | { |
||
| 252 | $has_class = false; |
||
| 253 | if (is_null($id)) { |
||
| 254 | $Product = new \Eccube\Entity\Product(); |
||
| 255 | $ProductClass = new \Eccube\Entity\ProductClass(); |
||
| 256 | $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE); |
||
| 257 | $Product |
||
| 258 | ->setDelFlg(Constant::DISABLED) |
||
| 259 | ->addProductClass($ProductClass) |
||
| 260 | ->setStatus($Disp); |
||
| 261 | $ProductClass |
||
| 262 | ->setDelFlg(Constant::DISABLED) |
||
| 263 | ->setStockUnlimited(true) |
||
| 264 | ->setProduct($Product); |
||
| 265 | $ProductStock = new \Eccube\Entity\ProductStock(); |
||
| 266 | $ProductClass->setProductStock($ProductStock); |
||
| 267 | $ProductStock->setProductClass($ProductClass); |
||
| 268 | } else { |
||
| 269 | $Product = $app['eccube.repository.product']->find($id); |
||
| 270 | if (!$Product) { |
||
| 271 | throw new NotFoundHttpException(); |
||
| 272 | } |
||
| 273 | // 規格あり商品か |
||
| 274 | $has_class = $Product->hasProductClass(); |
||
| 275 | if (!$has_class) { |
||
| 276 | $ProductClasses = $Product->getProductClasses(); |
||
| 277 | $ProductClass = $ProductClasses[0]; |
||
| 278 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 279 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
|
| 280 | $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate()); |
||
| 281 | } |
||
| 282 | $ProductStock = $ProductClasses[0]->getProductStock(); |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | $builder = $app['form.factory'] |
||
| 287 | ->createBuilder('admin_product', $Product); |
||
| 288 | |||
| 289 | // 規格あり商品の場合、規格関連情報をFormから除外 |
||
| 290 | if ($has_class) { |
||
| 291 | $builder->remove('class'); |
||
| 292 | } |
||
| 293 | |||
| 294 | $event = new EventArgs( |
||
| 295 | array( |
||
| 296 | 'builder' => $builder, |
||
| 297 | 'Product' => $Product, |
||
| 298 | ), |
||
| 299 | $request |
||
| 300 | ); |
||
| 301 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE, $event); |
||
| 302 | |||
| 303 | $form = $builder->getForm(); |
||
| 304 | |||
| 305 | if (!$has_class) { |
||
| 306 | $ProductClass->setStockUnlimited((boolean)$ProductClass->getStockUnlimited()); |
||
| 307 | $form['class']->setData($ProductClass); |
||
| 308 | } |
||
| 309 | |||
| 310 | // ファイルの登録 |
||
| 311 | $images = array(); |
||
| 312 | $ProductImages = $Product->getProductImage(); |
||
| 313 | foreach ($ProductImages as $ProductImage) { |
||
| 314 | $images[] = $ProductImage->getFileName(); |
||
| 315 | } |
||
| 316 | $form['images']->setData($images); |
||
| 317 | |||
| 318 | $categories = array(); |
||
| 319 | $ProductCategories = $Product->getProductCategories(); |
||
| 320 | foreach ($ProductCategories as $ProductCategory) { |
||
| 321 | /* @var $ProductCategory \Eccube\Entity\ProductCategory */ |
||
| 322 | $categories[] = $ProductCategory->getCategory(); |
||
| 323 | } |
||
| 324 | $form['Category']->setData($categories); |
||
| 325 | |||
| 326 | $Tags = array(); |
||
| 327 | $ProductTags = $Product->getProductTag(); |
||
| 328 | foreach ($ProductTags as $ProductTag) { |
||
| 329 | $Tags[] = $ProductTag->getTag(); |
||
| 330 | } |
||
| 331 | $form['Tag']->setData($Tags); |
||
| 332 | |||
| 333 | if ('POST' === $request->getMethod()) { |
||
| 334 | $form->handleRequest($request); |
||
| 335 | if ($form->isValid()) { |
||
| 336 | log_info('商品登録開始', array($id)); |
||
| 337 | $Product = $form->getData(); |
||
| 338 | |||
| 339 | if (!$has_class) { |
||
| 340 | $ProductClass = $form['class']->getData(); |
||
| 341 | |||
| 342 | // 個別消費税 |
||
| 343 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 344 | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { |
||
| 345 | if ($ProductClass->getTaxRate() !== null) { |
||
| 346 | View Code Duplication | if ($ProductClass->getTaxRule()) { |
|
| 347 | if ($ProductClass->getTaxRule()->getDelFlg() == Constant::ENABLED) { |
||
| 348 | $ProductClass->getTaxRule()->setDelFlg(Constant::DISABLED); |
||
| 349 | } |
||
| 350 | |||
| 351 | $ProductClass->getTaxRule()->setTaxRate($ProductClass->getTaxRate()); |
||
| 352 | } else { |
||
| 353 | $taxrule = $app['eccube.repository.tax_rule']->newTaxRule(); |
||
| 354 | $taxrule->setTaxRate($ProductClass->getTaxRate()); |
||
| 355 | $taxrule->setApplyDate(new \DateTime()); |
||
| 356 | $taxrule->setProduct($Product); |
||
| 357 | $taxrule->setProductClass($ProductClass); |
||
| 358 | $ProductClass->setTaxRule($taxrule); |
||
| 359 | } |
||
| 360 | } else { |
||
| 361 | if ($ProductClass->getTaxRule()) { |
||
| 362 | $ProductClass->getTaxRule()->setDelFlg(Constant::ENABLED); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | $app['orm.em']->persist($ProductClass); |
||
| 367 | |||
| 368 | // 在庫情報を作成 |
||
| 369 | if (!$ProductClass->getStockUnlimited()) { |
||
| 370 | $ProductStock->setStock($ProductClass->getStock()); |
||
| 371 | } else { |
||
| 372 | // 在庫無制限時はnullを設定 |
||
| 373 | $ProductStock->setStock(null); |
||
| 374 | } |
||
| 375 | $app['orm.em']->persist($ProductStock); |
||
| 376 | } |
||
| 377 | |||
| 378 | // カテゴリの登録 |
||
| 379 | // 一度クリア |
||
| 380 | /* @var $Product \Eccube\Entity\Product */ |
||
| 381 | foreach ($Product->getProductCategories() as $ProductCategory) { |
||
| 382 | $Product->removeProductCategory($ProductCategory); |
||
| 383 | $app['orm.em']->remove($ProductCategory); |
||
| 384 | } |
||
| 385 | $app['orm.em']->persist($Product); |
||
| 386 | $app['orm.em']->flush(); |
||
| 387 | |||
| 388 | $count = 1; |
||
| 389 | $Categories = $form->get('Category')->getData(); |
||
| 390 | $categoriesIdList = array(); |
||
| 391 | foreach ($Categories as $Category) { |
||
| 392 | View Code Duplication | foreach ($Category->getPath() as $ParentCategory) { |
|
| 393 | if (!isset($categoriesIdList[$ParentCategory->getId()])) { |
||
| 394 | $ProductCategory = $this->createProductCategory($Product, $ParentCategory, $count); |
||
| 395 | $app['orm.em']->persist($ProductCategory); |
||
| 396 | $count++; |
||
| 397 | /* @var $Product \Eccube\Entity\Product */ |
||
| 398 | $Product->addProductCategory($ProductCategory); |
||
| 399 | $categoriesIdList[$ParentCategory->getId()] = true; |
||
| 400 | } |
||
| 401 | } |
||
| 402 | if (!isset($categoriesIdList[$Category->getId()])) { |
||
| 403 | $ProductCategory = $this->createProductCategory($Product, $Category, $count); |
||
| 404 | $app['orm.em']->persist($ProductCategory); |
||
| 405 | $count++; |
||
| 406 | /* @var $Product \Eccube\Entity\Product */ |
||
| 407 | $Product->addProductCategory($ProductCategory); |
||
| 408 | $categoriesIdList[$Category->getId()] = true; |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | // 画像の登録 |
||
| 413 | $add_images = $form->get('add_images')->getData(); |
||
| 414 | foreach ($add_images as $add_image) { |
||
| 415 | $ProductImage = new \Eccube\Entity\ProductImage(); |
||
| 416 | $ProductImage |
||
| 417 | ->setFileName($add_image) |
||
| 418 | ->setProduct($Product) |
||
| 419 | ->setRank(1); |
||
| 420 | $Product->addProductImage($ProductImage); |
||
| 421 | $app['orm.em']->persist($ProductImage); |
||
| 422 | |||
| 423 | // 移動 |
||
| 424 | $file = new File($app['config']['image_temp_realdir'].'/'.$add_image); |
||
| 425 | $file->move($app['config']['image_save_realdir']); |
||
| 426 | } |
||
| 427 | |||
| 428 | // 画像の削除 |
||
| 429 | $delete_images = $form->get('delete_images')->getData(); |
||
| 430 | foreach ($delete_images as $delete_image) { |
||
| 431 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 432 | ->findOneBy(array('file_name' => $delete_image)); |
||
| 433 | |||
| 434 | // 追加してすぐに削除した画像は、Entityに追加されない |
||
| 435 | if ($ProductImage instanceof \Eccube\Entity\ProductImage) { |
||
| 436 | $Product->removeProductImage($ProductImage); |
||
| 437 | $app['orm.em']->remove($ProductImage); |
||
| 438 | |||
| 439 | } |
||
| 440 | $app['orm.em']->persist($Product); |
||
| 441 | |||
| 442 | // 削除 |
||
| 443 | View Code Duplication | if (!empty($delete_image)) { |
|
| 444 | $fs = new Filesystem(); |
||
| 445 | $fs->remove($app['config']['image_save_realdir'].'/'.$delete_image); |
||
| 446 | } |
||
| 447 | } |
||
| 448 | $app['orm.em']->persist($Product); |
||
| 449 | $app['orm.em']->flush(); |
||
| 450 | |||
| 451 | |||
| 452 | $ranks = $request->get('rank_images'); |
||
| 453 | if ($ranks) { |
||
| 454 | foreach ($ranks as $rank) { |
||
| 455 | list($filename, $rank_val) = explode('//', $rank); |
||
| 456 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 457 | ->findOneBy(array( |
||
| 458 | 'file_name' => $filename, |
||
| 459 | 'Product' => $Product, |
||
| 460 | )); |
||
| 461 | $ProductImage->setRank($rank_val); |
||
| 462 | $app['orm.em']->persist($ProductImage); |
||
| 463 | } |
||
| 464 | } |
||
| 465 | $app['orm.em']->flush(); |
||
| 466 | |||
| 467 | // 商品タグの登録 |
||
| 468 | // 商品タグを一度クリア |
||
| 469 | $ProductTags = $Product->getProductTag(); |
||
| 470 | foreach ($ProductTags as $ProductTag) { |
||
| 471 | $Product->removeProductTag($ProductTag); |
||
| 472 | $app['orm.em']->remove($ProductTag); |
||
| 473 | } |
||
| 474 | |||
| 475 | // 商品タグの登録 |
||
| 476 | $Tags = $form->get('Tag')->getData(); |
||
| 477 | foreach ($Tags as $Tag) { |
||
| 478 | $ProductTag = new ProductTag(); |
||
| 479 | $ProductTag |
||
| 480 | ->setProduct($Product) |
||
| 481 | ->setTag($Tag); |
||
| 482 | $Product->addProductTag($ProductTag); |
||
| 483 | $app['orm.em']->persist($ProductTag); |
||
| 484 | } |
||
| 485 | |||
| 486 | $Product->setUpdateDate(new \DateTime()); |
||
| 487 | $app['orm.em']->flush(); |
||
| 488 | |||
| 489 | log_info('商品登録完了', array($id)); |
||
| 490 | |||
| 491 | $event = new EventArgs( |
||
| 492 | array( |
||
| 493 | 'form' => $form, |
||
| 494 | 'Product' => $Product, |
||
| 495 | ), |
||
| 496 | $request |
||
| 497 | ); |
||
| 498 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event); |
||
| 499 | |||
| 500 | $app->addSuccess('admin.register.complete', 'admin'); |
||
| 501 | |||
| 502 | return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId()))); |
||
| 503 | } else { |
||
| 504 | log_info('商品登録チェックエラー', array($id)); |
||
| 505 | $app->addError('admin.register.failed', 'admin'); |
||
| 506 | } |
||
| 507 | } |
||
| 508 | |||
| 509 | // 検索結果の保持 |
||
| 510 | $builder = $app['form.factory'] |
||
| 511 | ->createBuilder('admin_search_product'); |
||
| 512 | |||
| 513 | $event = new EventArgs( |
||
| 514 | array( |
||
| 515 | 'builder' => $builder, |
||
| 516 | 'Product' => $Product, |
||
| 517 | ), |
||
| 518 | $request |
||
| 519 | ); |
||
| 520 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event); |
||
| 521 | |||
| 522 | $searchForm = $builder->getForm(); |
||
| 523 | |||
| 524 | if ('POST' === $request->getMethod()) { |
||
| 525 | $searchForm->handleRequest($request); |
||
| 526 | } |
||
| 527 | |||
| 528 | return $app->render('Product/product.twig', array( |
||
| 529 | 'Product' => $Product, |
||
| 530 | 'form' => $form->createView(), |
||
| 531 | 'searchForm' => $searchForm->createView(), |
||
| 532 | 'has_class' => $has_class, |
||
| 533 | 'id' => $id, |
||
| 534 | )); |
||
| 535 | } |
||
| 536 | |||
| 868 |
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.