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