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 ProductController 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 ProductController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | 1 | class ProductController extends AbstractController |
|
|
|
|||
| 40 | { |
||
| 41 | public function index(Application $app, Request $request, $page_no = null) |
||
| 42 | { |
||
| 43 | |||
| 44 | $session = $app['session']; |
||
| 45 | |||
| 46 | $builder = $app['form.factory'] |
||
| 47 | ->createBuilder('admin_search_product'); |
||
| 48 | 1 | ||
| 49 | $event = new EventArgs( |
||
| 50 | array( |
||
| 51 | 'builder' => $builder, |
||
| 52 | ), |
||
| 53 | 1 | $request |
|
| 54 | 1 | ); |
|
| 55 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_INITIALIZE, $event); |
||
| 56 | |||
| 57 | $searchForm = $builder->getForm(); |
||
| 58 | |||
| 59 | $pagination = array(); |
||
| 60 | |||
| 61 | $disps = $app['eccube.repository.master.disp']->findAll(); |
||
| 62 | $pageMaxis = $app['eccube.repository.master.page_max']->findAll(); |
||
| 63 | $page_count = $app['config']['default_page_count']; |
||
| 64 | $page_status = null; |
||
| 65 | $active = false; |
||
| 66 | |||
| 67 | if ('POST' === $request->getMethod()) { |
||
| 68 | |||
| 69 | $searchForm->handleRequest($request); |
||
| 70 | |||
| 71 | if ($searchForm->isValid()) { |
||
| 72 | $searchData = $searchForm->getData(); |
||
| 73 | |||
| 74 | // paginator |
||
| 75 | $qb = $app['eccube.repository.product']->getQueryBuilderBySearchDataForAdmin($searchData); |
||
| 76 | $page_no = 1; |
||
| 77 | |||
| 78 | $event = new EventArgs( |
||
| 79 | array( |
||
| 80 | 'qb' => $qb, |
||
| 81 | 'searchData' => $searchData, |
||
| 82 | ), |
||
| 83 | $request |
||
| 84 | ); |
||
| 85 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH, $event); |
||
| 86 | $searchData = $event->getArgument('searchData'); |
||
| 87 | |||
| 88 | $pagination = $app['paginator']()->paginate( |
||
| 89 | $qb, |
||
| 90 | $page_no, |
||
| 91 | $page_count, |
||
| 92 | array('wrap-queries' => true) |
||
| 93 | ); |
||
| 94 | |||
| 95 | // sessionのデータ保持 |
||
| 96 | $session->set('eccube.admin.product.search', $searchData); |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | if (is_null($page_no)) { |
||
| 100 | // sessionを削除 |
||
| 101 | $session->remove('eccube.admin.product.search'); |
||
| 102 | } else { |
||
| 103 | // pagingなどの処理 |
||
| 104 | $searchData = $session->get('eccube.admin.product.search'); |
||
| 105 | if (!is_null($searchData)) { |
||
| 106 | |||
| 107 | // 公開ステータス |
||
| 108 | $status = $request->get('status'); |
||
| 109 | if (!empty($status)) { |
||
| 110 | if ($status != $app['config']['admin_product_stock_status']) { |
||
| 111 | $searchData['link_status'] = $app['eccube.repository.master.disp']->find($status); |
||
| 112 | $searchData['status'] = null; |
||
| 113 | $session->set('eccube.admin.product.search', $searchData); |
||
| 114 | } else { |
||
| 115 | $searchData['stock_status'] = Constant::DISABLED; |
||
| 116 | } |
||
| 117 | $page_status = $status; |
||
| 118 | } else { |
||
| 119 | $searchData['link_status'] = null; |
||
| 120 | $searchData['stock_status'] = null; |
||
| 121 | } |
||
| 122 | // 表示件数 |
||
| 123 | $pcount = $request->get('page_count'); |
||
| 124 | |||
| 125 | $page_count = empty($pcount) ? $page_count : $pcount; |
||
| 126 | |||
| 127 | $qb = $app['eccube.repository.product']->getQueryBuilderBySearchDataForAdmin($searchData); |
||
| 128 | |||
| 129 | $event = new EventArgs( |
||
| 130 | 1 | array( |
|
| 131 | 'qb' => $qb, |
||
| 132 | 'searchData' => $searchData, |
||
| 133 | 1 | ), |
|
| 134 | 1 | $request |
|
| 135 | ); |
||
| 136 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_INDEX_SEARCH, $event); |
||
| 137 | $searchData = $event->getArgument('searchData'); |
||
| 138 | |||
| 139 | $pagination = $app['paginator']()->paginate( |
||
| 140 | $qb, |
||
| 141 | $page_no, |
||
| 142 | $page_count, |
||
| 143 | 1 | array('wrap-queries' => true) |
|
| 144 | ); |
||
| 145 | |||
| 146 | // セッションから検索条件を復元 |
||
| 147 | if (!empty($searchData['category_id'])) { |
||
| 148 | $searchData['category_id'] = $app['eccube.repository.category']->find($searchData['category_id']); |
||
| 149 | } |
||
| 150 | if (empty($status)) { |
||
| 151 | View Code Duplication | if (count($searchData['status']) > 0) { |
|
| 152 | $status_ids = array(); |
||
| 153 | foreach ($searchData['status'] as $Status) { |
||
| 154 | $status_ids[] = $Status->getId(); |
||
| 155 | } |
||
| 156 | $searchData['status'] = $app['eccube.repository.master.disp']->findBy(array('id' => $status_ids)); |
||
| 157 | } |
||
| 158 | $searchData['link_status'] = null; |
||
| 159 | $searchData['stock_status'] = null; |
||
| 160 | } |
||
| 161 | $searchForm->setData($searchData); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | 3 | } |
|
| 165 | |||
| 166 | 3 | return $app->render('Product/index.twig', array( |
|
| 167 | 'searchForm' => $searchForm->createView(), |
||
| 168 | 'pagination' => $pagination, |
||
| 169 | 'disps' => $disps, |
||
| 170 | 'pageMaxis' => $pageMaxis, |
||
| 171 | 'page_no' => $page_no, |
||
| 172 | 1 | 'page_status' => $page_status, |
|
| 173 | 1 | 'page_count' => $page_count, |
|
| 174 | 'active' => $active, |
||
| 175 | )); |
||
| 176 | 1 | } |
|
| 177 | 1 | ||
| 178 | public function addImage(Application $app, Request $request) |
||
| 207 | |||
| 208 | public function edit(Application $app, Request $request, $id = null) |
||
| 209 | 3 | { |
|
| 210 | $has_class = false; |
||
| 211 | if (is_null($id)) { |
||
| 212 | $Product = new \Eccube\Entity\Product(); |
||
| 213 | $ProductClass = new \Eccube\Entity\ProductClass(); |
||
| 214 | $Disp = $app['eccube.repository.master.disp']->find(\Eccube\Entity\Master\Disp::DISPLAY_HIDE); |
||
| 215 | 3 | $Product |
|
| 216 | ->setDelFlg(Constant::DISABLED) |
||
| 217 | ->addProductClass($ProductClass) |
||
| 218 | ->setStatus($Disp); |
||
| 219 | 1 | $ProductClass |
|
| 220 | ->setDelFlg(Constant::DISABLED) |
||
| 221 | ->setStockUnlimited(true) |
||
| 222 | 3 | ->setProduct($Product); |
|
| 223 | $ProductStock = new \Eccube\Entity\ProductStock(); |
||
| 224 | $ProductClass->setProductStock($ProductStock); |
||
| 225 | $ProductStock->setProductClass($ProductClass); |
||
| 226 | } else { |
||
| 227 | 1 | $Product = $app['eccube.repository.product']->find($id); |
|
| 228 | if (!$Product) { |
||
| 229 | throw new NotFoundHttpException(); |
||
| 230 | } |
||
| 231 | // 規格あり商品か |
||
| 232 | $has_class = $Product->hasProductClass(); |
||
| 233 | if (!$has_class) { |
||
| 234 | $ProductClasses = $Product->getProductClasses(); |
||
| 235 | 1 | $ProductClass = $ProductClasses[0]; |
|
| 236 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 237 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED && $ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
|
| 238 | $ProductClass->setTaxRate($ProductClass->getTaxRule()->getTaxRate()); |
||
| 239 | } |
||
| 240 | $ProductStock = $ProductClasses[0]->getProductStock(); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | $builder = $app['form.factory'] |
||
| 245 | ->createBuilder('admin_product', $Product); |
||
| 246 | |||
| 247 | // 規格あり商品の場合、規格関連情報をFormから除外 |
||
| 248 | if ($has_class) { |
||
| 249 | $builder->remove('class'); |
||
| 250 | } |
||
| 251 | |||
| 252 | $event = new EventArgs( |
||
| 253 | array( |
||
| 254 | 'builder' => $builder, |
||
| 255 | 'Product' => $Product, |
||
| 256 | ), |
||
| 257 | $request |
||
| 258 | ); |
||
| 259 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_INITIALIZE, $event); |
||
| 260 | |||
| 261 | $form = $builder->getForm(); |
||
| 262 | |||
| 263 | if (!$has_class) { |
||
| 264 | $ProductClass->setStockUnlimited((boolean)$ProductClass->getStockUnlimited()); |
||
| 265 | $form['class']->setData($ProductClass); |
||
| 266 | } |
||
| 267 | |||
| 268 | // ファイルの登録 |
||
| 269 | $images = array(); |
||
| 270 | $ProductImages = $Product->getProductImage(); |
||
| 271 | foreach ($ProductImages as $ProductImage) { |
||
| 272 | $images[] = $ProductImage->getFileName(); |
||
| 273 | 1 | } |
|
| 274 | $form['images']->setData($images); |
||
| 275 | |||
| 276 | $categories = array(); |
||
| 277 | $ProductCategories = $Product->getProductCategories(); |
||
| 278 | foreach ($ProductCategories as $ProductCategory) { |
||
| 279 | /* @var $ProductCategory \Eccube\Entity\ProductCategory */ |
||
| 280 | 1 | $categories[] = $ProductCategory->getCategory(); |
|
| 281 | } |
||
| 282 | $form['Category']->setData($categories); |
||
| 283 | |||
| 284 | if ('POST' === $request->getMethod()) { |
||
| 285 | $form->handleRequest($request); |
||
| 286 | if ($form->isValid()) { |
||
| 287 | $Product = $form->getData(); |
||
| 288 | |||
| 289 | if (!$has_class) { |
||
| 290 | $ProductClass = $form['class']->getData(); |
||
| 291 | |||
| 292 | // 個別消費税 |
||
| 293 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 294 | 1 | View Code Duplication | if ($BaseInfo->getOptionProductTaxRule() == Constant::ENABLED) { |
| 295 | if ($ProductClass->getTaxRate()) { |
||
| 296 | if ($ProductClass->getTaxRule() && !$ProductClass->getTaxRule()->getDelFlg()) { |
||
| 297 | $ProductClass->getTaxRule()->setTaxRate($ProductClass->getTaxRate()); |
||
| 298 | } else { |
||
| 299 | $taxrule = $app['eccube.repository.tax_rule']->newTaxRule(); |
||
| 300 | $taxrule->setTaxRate($ProductClass->getTaxRate()); |
||
| 301 | $taxrule->setApplyDate(new \DateTime()); |
||
| 302 | $taxrule->setProduct($Product); |
||
| 303 | $taxrule->setProductClass($ProductClass); |
||
| 304 | $ProductClass->setTaxRule($taxrule); |
||
| 305 | } |
||
| 306 | } else { |
||
| 307 | if ($ProductClass->getTaxRule()) { |
||
| 308 | $ProductClass->getTaxRule()->setDelFlg(Constant::ENABLED); |
||
| 309 | } |
||
| 310 | 1 | } |
|
| 311 | } |
||
| 312 | $app['orm.em']->persist($ProductClass); |
||
| 313 | |||
| 314 | // 在庫情報を作成 |
||
| 315 | if (!$ProductClass->getStockUnlimited()) { |
||
| 316 | $ProductStock->setStock($ProductClass->getStock()); |
||
| 317 | } else { |
||
| 318 | // 在庫無制限時はnullを設定 |
||
| 319 | $ProductStock->setStock(null); |
||
| 320 | } |
||
| 321 | $app['orm.em']->persist($ProductStock); |
||
| 322 | } |
||
| 323 | |||
| 324 | // カテゴリの登録 |
||
| 325 | // 一度クリア |
||
| 326 | /* @var $Product \Eccube\Entity\Product */ |
||
| 327 | foreach ($Product->getProductCategories() as $ProductCategory) { |
||
| 328 | $Product->removeProductCategory($ProductCategory); |
||
| 329 | 1 | $app['orm.em']->remove($ProductCategory); |
|
| 330 | } |
||
| 331 | $app['orm.em']->persist($Product); |
||
| 332 | $app['orm.em']->flush(); |
||
| 333 | |||
| 334 | $count = 1; |
||
| 335 | 1 | $Categories = $form->get('Category')->getData(); |
|
| 336 | foreach ($Categories as $Category) { |
||
| 337 | $ProductCategory = new \Eccube\Entity\ProductCategory(); |
||
| 338 | $ProductCategory |
||
| 339 | ->setProduct($Product) |
||
| 340 | ->setProductId($Product->getId()) |
||
| 341 | ->setCategory($Category) |
||
| 342 | ->setCategoryId($Category->getId()) |
||
| 343 | ->setRank($count); |
||
| 344 | $app['orm.em']->persist($ProductCategory); |
||
| 345 | $count++; |
||
| 346 | /* @var $Product \Eccube\Entity\Product */ |
||
| 347 | $Product->addProductCategory($ProductCategory); |
||
| 348 | } |
||
| 349 | |||
| 350 | // 画像の登録 |
||
| 351 | $add_images = $form->get('add_images')->getData(); |
||
| 352 | foreach ($add_images as $add_image) { |
||
| 353 | $ProductImage = new \Eccube\Entity\ProductImage(); |
||
| 354 | $ProductImage |
||
| 355 | ->setFileName($add_image) |
||
| 356 | ->setProduct($Product) |
||
| 357 | ->setRank(1); |
||
| 358 | $Product->addProductImage($ProductImage); |
||
| 359 | $app['orm.em']->persist($ProductImage); |
||
| 360 | |||
| 361 | // 移動 |
||
| 362 | $file = new File($app['config']['image_temp_realdir'] . '/' . $add_image); |
||
| 363 | $file->move($app['config']['image_save_realdir']); |
||
| 364 | } |
||
| 365 | 2 | ||
| 366 | // 画像の削除 |
||
| 367 | 2 | $delete_images = $form->get('delete_images')->getData(); |
|
| 368 | 2 | foreach ($delete_images as $delete_image) { |
|
| 369 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 370 | ->findOneBy(array('file_name' => $delete_image)); |
||
| 371 | |||
| 372 | 3 | // 追加してすぐに削除した画像は、Entityに追加されない |
|
| 373 | if ($ProductImage instanceof \Eccube\Entity\ProductImage) { |
||
| 374 | 1 | $Product->removeProductImage($ProductImage); |
|
| 375 | $app['orm.em']->remove($ProductImage); |
||
| 376 | |||
| 377 | } |
||
| 378 | $app['orm.em']->persist($Product); |
||
| 379 | |||
| 380 | // 削除 |
||
| 381 | 1 | $fs = new Filesystem(); |
|
| 382 | $fs->remove($app['config']['image_save_realdir'] . '/' . $delete_image); |
||
| 383 | } |
||
| 384 | $app['orm.em']->persist($Product); |
||
| 385 | $app['orm.em']->flush(); |
||
| 386 | 1 | ||
| 387 | |||
| 388 | $ranks = $request->get('rank_images'); |
||
| 389 | if ($ranks) { |
||
| 390 | 1 | foreach ($ranks as $rank) { |
|
| 391 | list($filename, $rank_val) = explode('//', $rank); |
||
| 392 | $ProductImage = $app['eccube.repository.product_image'] |
||
| 393 | ->findOneBy(array( |
||
| 394 | 'file_name' => $filename, |
||
| 395 | 'Product' => $Product, |
||
| 396 | )); |
||
| 397 | $ProductImage->setRank($rank_val); |
||
| 398 | $app['orm.em']->persist($ProductImage); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | $app['orm.em']->flush(); |
||
| 402 | |||
| 403 | $event = new EventArgs( |
||
| 404 | array( |
||
| 405 | 'form' => $form, |
||
| 406 | 'Product' => $Product, |
||
| 407 | ), |
||
| 408 | $request |
||
| 409 | ); |
||
| 410 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE, $event); |
||
| 411 | |||
| 412 | $app->addSuccess('admin.register.complete', 'admin'); |
||
| 413 | |||
| 414 | return $app->redirect($app->url('admin_product_product_edit', array('id' => $Product->getId()))); |
||
| 415 | } else { |
||
| 416 | $app->addError('admin.register.failed', 'admin'); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | // 検索結果の保持 |
||
| 421 | $builder = $app['form.factory'] |
||
| 422 | ->createBuilder('admin_search_product'); |
||
| 423 | |||
| 424 | $event = new EventArgs( |
||
| 425 | array( |
||
| 426 | 'builder' => $builder, |
||
| 427 | 'Product' => $Product, |
||
| 428 | ), |
||
| 429 | $request |
||
| 430 | ); |
||
| 431 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_EDIT_SEARCH, $event); |
||
| 432 | |||
| 433 | $searchForm = $builder->getForm(); |
||
| 434 | |||
| 435 | if ('POST' === $request->getMethod()) { |
||
| 436 | 1 | $searchForm->handleRequest($request); |
|
| 437 | } |
||
| 438 | |||
| 439 | 1 | return $app->render('Product/product.twig', array( |
|
| 440 | 'Product' => $Product, |
||
| 441 | 'form' => $form->createView(), |
||
| 442 | 1 | 'searchForm' => $searchForm->createView(), |
|
| 443 | 'has_class' => $has_class, |
||
| 444 | 1 | 'id' => $id, |
|
| 445 | )); |
||
| 446 | } |
||
| 447 | |||
| 448 | 1 | public function delete(Application $app, Request $request, $id = null) |
|
| 449 | { |
||
| 450 | $this->isTokenValid($app); |
||
| 451 | |||
| 452 | if (!is_null($id)) { |
||
| 453 | /* @var $Product \Eccube\Entity\Product */ |
||
| 454 | $Product = $app['eccube.repository.product']->find($id); |
||
| 455 | if (!$Product) { |
||
| 456 | $app->deleteMessage(); |
||
| 457 | return $app->redirect($app->url('admin_product')); |
||
| 458 | } |
||
| 459 | |||
| 460 | if ($Product instanceof \Eccube\Entity\Product) { |
||
| 461 | $Product->setDelFlg(Constant::ENABLED); |
||
| 462 | |||
| 463 | $ProductClasses = $Product->getProductClasses(); |
||
| 464 | $deleteImages = array(); |
||
| 465 | foreach ($ProductClasses as $ProductClass) { |
||
| 466 | $ProductClass->setDelFlg(Constant::ENABLED); |
||
| 467 | $Product->removeProductClass($ProductClass); |
||
| 468 | |||
| 469 | $ProductClasses = $Product->getProductClasses(); |
||
| 470 | foreach ($ProductClasses as $ProductClass) { |
||
| 471 | $ProductClass->setDelFlg(Constant::ENABLED); |
||
| 472 | $Product->removeProductClass($ProductClass); |
||
| 473 | |||
| 474 | $ProductStock = $ProductClass->getProductStock(); |
||
| 475 | $app['orm.em']->remove($ProductStock); |
||
| 476 | } |
||
| 477 | |||
| 478 | $ProductImages = $Product->getProductImage(); |
||
| 479 | foreach ($ProductImages as $ProductImage) { |
||
| 480 | $Product->removeProductImage($ProductImage); |
||
| 481 | $deleteImages[] = $ProductImage->getFileName(); |
||
| 482 | $app['orm.em']->remove($ProductImage); |
||
| 483 | } |
||
| 484 | |||
| 485 | $ProductCategories = $Product->getProductCategories(); |
||
| 486 | foreach ($ProductCategories as $ProductCategory) { |
||
| 487 | $Product->removeProductCategory($ProductCategory); |
||
| 488 | $app['orm.em']->remove($ProductCategory); |
||
| 489 | } |
||
| 490 | |||
| 491 | } |
||
| 492 | |||
| 493 | $app['orm.em']->persist($Product); |
||
| 494 | |||
| 495 | $app['orm.em']->flush(); |
||
| 496 | |||
| 497 | $event = new EventArgs( |
||
| 498 | array( |
||
| 499 | 'Product' => $Product, |
||
| 500 | 'ProductClass' => $ProductClasses, |
||
| 501 | 'deleteImages' => $deleteImages, |
||
| 502 | ), |
||
| 503 | $request |
||
| 504 | ); |
||
| 505 | 1 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_PRODUCT_DELETE_COMPLETE, $event); |
|
| 506 | $deleteImages = $event->getArgument('deleteImages'); |
||
| 507 | |||
| 508 | // 画像ファイルの削除(commit後に削除させる) |
||
| 509 | foreach ($deleteImages as $deleteImage) { |
||
| 510 | try { |
||
| 511 | $fs = new Filesystem(); |
||
| 512 | $fs->remove($app['config']['image_save_realdir'] . '/' . $deleteImage); |
||
| 513 | } catch (\Exception $e) { |
||
| 514 | // エラーが発生しても無視する |
||
| 515 | } |
||
| 516 | } |
||
| 517 | |||
| 518 | $app->addSuccess('admin.delete.complete', 'admin'); |
||
| 519 | } else { |
||
| 520 | $app->addError('admin.delete.failed', 'admin'); |
||
| 521 | 1 | } |
|
| 522 | } else { |
||
| 523 | $app->addError('admin.delete.failed', 'admin'); |
||
| 524 | } |
||
| 525 | |||
| 526 | return $app->redirect($app->url('admin_product')); |
||
| 527 | } |
||
| 528 | |||
| 529 | public function copy(Application $app, Request $request, $id = null) |
||
| 621 | |||
| 622 | public function display(Application $app, Request $request, $id = null) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * 商品CSVの出力. |
||
| 639 | * |
||
| 640 | * @param Application $app |
||
| 641 | * @param Request $request |
||
| 642 | * @return StreamedResponse |
||
| 643 | */ |
||
| 644 | public function export(Application $app, Request $request) |
||
| 717 | } |
||
| 718 |