| Conditions | 30 |
| Paths | 13 |
| Total Lines | 239 |
| Code Lines | 132 |
| Lines | 52 |
| Ratio | 21.76 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 268 | public function edit(Application $app, Request $request, $id) |
||
| 269 | { |
||
| 270 | |||
| 271 | /** @var $Product \Eccube\Entity\Product */ |
||
| 272 | $Product = $app['eccube.repository.product']->find($id); |
||
| 273 | |||
| 274 | if (!$Product) { |
||
| 275 | throw new NotFoundHttpException(); |
||
| 276 | } |
||
| 277 | |||
| 278 | $builder = $app['form.factory']->createBuilder(); |
||
| 279 | $builder |
||
| 280 | ->add('product_classes', 'collection', array( |
||
| 281 | 'type' => 'admin_product_class', |
||
| 282 | 'allow_add' => true, |
||
| 283 | 'allow_delete' => true, |
||
| 284 | )); |
||
| 285 | |||
| 286 | $event = new EventArgs( |
||
| 287 | array( |
||
| 288 | 'builder' => $builder, |
||
| 289 | 'Product' => $Product, |
||
| 290 | ), |
||
| 291 | $request |
||
| 292 | ); |
||
| 293 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_INITIALIZE, $event); |
||
| 294 | |||
| 295 | $form = $builder->getForm(); |
||
| 296 | |||
| 297 | $ProductClasses = $this->getProductClassesExcludeNonClass($Product); |
||
| 298 | |||
| 299 | if ('POST' === $request->getMethod()) { |
||
| 300 | |||
| 301 | $form->handleRequest($request); |
||
| 302 | |||
| 303 | switch ($request->get('mode')) { |
||
| 304 | case 'edit': |
||
| 305 | // 新規登録 |
||
| 306 | |||
| 307 | View Code Duplication | if (count($ProductClasses) > 0) { |
|
| 308 | // 既に登録されていれば最初の画面に戻す |
||
| 309 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 310 | } |
||
| 311 | |||
| 312 | $addProductClasses = array(); |
||
| 313 | |||
| 314 | $tmpProductClass = null; |
||
| 315 | View Code Duplication | foreach ($form->get('product_classes') as $formData) { |
|
| 316 | // 追加対象の行をvalidate |
||
| 317 | $ProductClass = $formData->getData(); |
||
| 318 | |||
| 319 | if ($ProductClass->getAdd()) { |
||
| 320 | if ($formData->isValid()) { |
||
| 321 | $addProductClasses[] = $ProductClass; |
||
| 322 | } else { |
||
| 323 | // 対象行のエラー |
||
| 324 | return $this->render($app, $Product, $ProductClass, true, $form); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | $tmpProductClass = $ProductClass; |
||
| 328 | } |
||
| 329 | |||
| 330 | View Code Duplication | if (count($addProductClasses) == 0) { |
|
| 331 | // 対象がなければエラー |
||
| 332 | $error = array('message' => '商品規格が選択されていません。'); |
||
| 333 | return $this->render($app, $Product, $tmpProductClass, true, $form, $error); |
||
| 334 | } |
||
| 335 | |||
| 336 | // 選択された商品規格を登録 |
||
| 337 | $this->insertProductClass($app, $Product, $addProductClasses); |
||
| 338 | |||
| 339 | // デフォルトの商品規格を更新 |
||
| 340 | $defaultProductClass = $app['eccube.repository.product_class'] |
||
| 341 | ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null)); |
||
| 342 | |||
| 343 | $defaultProductClass->setDelFlg(Constant::ENABLED); |
||
| 344 | |||
| 345 | $event = new EventArgs( |
||
| 346 | array( |
||
| 347 | 'form' => $form, |
||
| 348 | 'Product' => $Product, |
||
| 349 | 'defaultProductClass' => $defaultProductClass, |
||
| 350 | ), |
||
| 351 | $request |
||
| 352 | ); |
||
| 353 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_COMPLETE, $event); |
||
| 354 | |||
| 355 | $app['orm.em']->flush(); |
||
| 356 | |||
| 357 | $app->addSuccess('admin.product.product_class.save.complete', 'admin'); |
||
| 358 | |||
| 359 | break; |
||
| 360 | case 'update': |
||
| 361 | // 更新 |
||
| 362 | |||
| 363 | View Code Duplication | if (count($ProductClasses) == 0) { |
|
| 364 | // 商品規格が0件であれば最初の画面に戻す |
||
| 365 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 366 | } |
||
| 367 | |||
| 368 | $checkProductClasses = array(); |
||
| 369 | $removeProductClasses = array(); |
||
| 370 | |||
| 371 | $tempProductClass = null; |
||
| 372 | View Code Duplication | foreach ($form->get('product_classes') as $formData) { |
|
| 373 | // 追加対象の行をvalidate |
||
| 374 | $ProductClass = $formData->getData(); |
||
| 375 | |||
| 376 | if ($ProductClass->getAdd()) { |
||
| 377 | if ($formData->isValid()) { |
||
| 378 | $checkProductClasses[] = $ProductClass; |
||
| 379 | } else { |
||
| 380 | return $this->render($app, $Product, $ProductClass, false, $form); |
||
| 381 | } |
||
| 382 | } else { |
||
| 383 | // 削除対象の行 |
||
| 384 | $removeProductClasses[] = $ProductClass; |
||
| 385 | } |
||
| 386 | $tempProductClass = $ProductClass; |
||
| 387 | } |
||
| 388 | |||
| 389 | View Code Duplication | if (count($checkProductClasses) == 0) { |
|
| 390 | // 対象がなければエラー |
||
| 391 | $error = array('message' => '商品規格が選択されていません。'); |
||
| 392 | return $this->render($app, $Product, $tempProductClass, false, $form, $error); |
||
| 393 | } |
||
| 394 | |||
| 395 | |||
| 396 | // 登録対象と更新対象の行か判断する |
||
| 397 | $addProductClasses = array(); |
||
| 398 | $updateProductClasses = array(); |
||
| 399 | foreach ($checkProductClasses as $cp) { |
||
| 400 | $flag = false; |
||
| 401 | |||
| 402 | // 既に登録済みの商品規格か確認 |
||
| 403 | foreach ($ProductClasses as $productClass) { |
||
| 404 | if ($productClass->getProduct()->getId() == $id && |
||
| 405 | $productClass->getClassCategory1() == $cp->getClassCategory1() && |
||
| 406 | $productClass->getClassCategory2() == $cp->getClassCategory2()) { |
||
| 407 | $updateProductClasses[] = $cp; |
||
| 408 | |||
| 409 | // 商品情報 |
||
| 410 | $cp->setProduct($Product); |
||
| 411 | // 商品在庫 |
||
| 412 | $productStock = $productClass->getProductStock(); |
||
| 413 | if (!$cp->getStockUnlimited()) { |
||
| 414 | $productStock->setStock($cp->getStock()); |
||
| 415 | } else { |
||
| 416 | $productStock->setStock(null); |
||
| 417 | } |
||
| 418 | $this->setDefualtProductClass($app, $productClass, $cp); |
||
| 419 | $flag = true; |
||
| 420 | break; |
||
| 421 | } |
||
| 422 | } |
||
| 423 | if (!$flag) { |
||
| 424 | $addProductClasses[] = $cp; |
||
| 425 | } |
||
| 426 | } |
||
| 427 | |||
| 428 | foreach ($removeProductClasses as $rc) { |
||
| 429 | // 登録されている商品規格に削除フラグをセット |
||
| 430 | foreach ($ProductClasses as $productClass) { |
||
| 431 | if ($productClass->getProduct()->getId() == $id && |
||
| 432 | $productClass->getClassCategory1() == $rc->getClassCategory1() && |
||
| 433 | $productClass->getClassCategory2() == $rc->getClassCategory2()) { |
||
| 434 | |||
| 435 | $productClass->setDelFlg(Constant::ENABLED); |
||
| 436 | break; |
||
| 437 | } |
||
| 438 | } |
||
| 439 | } |
||
| 440 | |||
| 441 | // 選択された商品規格を登録 |
||
| 442 | $this->insertProductClass($app, $Product, $addProductClasses); |
||
| 443 | |||
| 444 | $event = new EventArgs( |
||
| 445 | array( |
||
| 446 | 'form' => $form, |
||
| 447 | 'Product' => $Product, |
||
| 448 | 'updateProductClasses' => $updateProductClasses, |
||
| 449 | ), |
||
| 450 | $request |
||
| 451 | ); |
||
| 452 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE, $event); |
||
| 453 | |||
| 454 | $app['orm.em']->flush(); |
||
| 455 | |||
| 456 | $app->addSuccess('admin.product.product_class.update.complete', 'admin'); |
||
| 457 | |||
| 458 | break; |
||
| 459 | case 'delete': |
||
| 460 | // 削除 |
||
| 461 | |||
| 462 | View Code Duplication | if (count($ProductClasses) == 0) { |
|
| 463 | // 既に商品が削除されていれば元の画面に戻す |
||
| 464 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 465 | } |
||
| 466 | |||
| 467 | foreach ($ProductClasses as $ProductClass) { |
||
| 468 | // 登録されている商品規格に削除フラグをセット |
||
| 469 | $ProductClass->setDelFlg(Constant::ENABLED); |
||
| 470 | } |
||
| 471 | |||
| 472 | /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */ |
||
| 473 | $softDeleteFilter = $app['orm.em']->getFilters()->getFilter('soft_delete'); |
||
| 474 | $softDeleteFilter->setExcludes(array( |
||
| 475 | 'Eccube\Entity\ProductClass' |
||
| 476 | )); |
||
| 477 | |||
| 478 | // デフォルトの商品規格を更新 |
||
| 479 | $defaultProductClass = $app['eccube.repository.product_class'] |
||
| 480 | ->findOneBy(array('Product' => $Product, 'ClassCategory1' => null, 'ClassCategory2' => null, 'del_flg' => Constant::ENABLED)); |
||
| 481 | |||
| 482 | $defaultProductClass->setDelFlg(Constant::DISABLED); |
||
| 483 | |||
| 484 | $event = new EventArgs( |
||
| 485 | array( |
||
| 486 | 'form' => $form, |
||
| 487 | 'Product' => $Product, |
||
| 488 | 'defaultProductClass' => $defaultProductClass, |
||
| 489 | ), |
||
| 490 | $request |
||
| 491 | ); |
||
| 492 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_DELETE, $event); |
||
| 493 | |||
| 494 | $app['orm.em']->flush(); |
||
| 495 | |||
| 496 | $app->addSuccess('admin.product.product_class.delete.complete', 'admin'); |
||
| 497 | |||
| 498 | break; |
||
| 499 | default: |
||
| 500 | break; |
||
| 501 | } |
||
| 502 | |||
| 503 | } |
||
| 504 | |||
| 505 | return $app->redirect($app->url('admin_product_product_class', array('id' => $id))); |
||
| 506 | } |
||
| 507 | |||
| 756 |