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:
| 1 | <?php |
||
| 37 | class CustomerController extends AbstractController |
||
|
|
|||
| 38 | { |
||
| 39 | 6 | public function index(Application $app, Request $request, $page_no = null) |
|
| 40 | { |
||
| 41 | 6 | $session = $request->getSession(); |
|
| 42 | 6 | $pagination = array(); |
|
| 43 | 6 | $builder = $app['form.factory'] |
|
| 44 | 6 | ->createBuilder('admin_search_customer'); |
|
| 45 | |||
| 46 | 6 | $event = new EventArgs( |
|
| 47 | array( |
||
| 48 | 6 | 'builder' => $builder, |
|
| 49 | ), |
||
| 50 | $request |
||
| 51 | ); |
||
| 52 | 6 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_INITIALIZE, $event); |
|
| 53 | |||
| 54 | 6 | $searchForm = $builder->getForm(); |
|
| 55 | |||
| 56 | //アコーディオンの制御初期化( デフォルトでは閉じる ) |
||
| 57 | 6 | $active = false; |
|
| 58 | |||
| 59 | 6 | $pageMaxis = $app['eccube.repository.master.page_max']->findAll(); |
|
| 60 | 6 | $page_count = $app['config']['default_page_count']; |
|
| 61 | |||
| 62 | 6 | if ('POST' === $request->getMethod()) { |
|
| 63 | |||
| 64 | 3 | $searchForm->handleRequest($request); |
|
| 65 | |||
| 66 | 3 | View Code Duplication | if ($searchForm->isValid()) { |
| 67 | 3 | $searchData = $searchForm->getData(); |
|
| 68 | |||
| 69 | // paginator |
||
| 70 | 3 | $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData); |
|
| 71 | 3 | $page_no = 1; |
|
| 72 | |||
| 73 | 3 | $event = new EventArgs( |
|
| 74 | array( |
||
| 75 | 3 | 'form' => $searchForm, |
|
| 76 | 3 | 'qb' => $qb, |
|
| 77 | ), |
||
| 78 | $request |
||
| 79 | ); |
||
| 80 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event); |
|
| 81 | |||
| 82 | 3 | $pagination = $app['paginator']()->paginate( |
|
| 83 | $qb, |
||
| 84 | $page_no, |
||
| 85 | $page_count |
||
| 86 | ); |
||
| 87 | |||
| 88 | // sessionのデータ保持 |
||
| 89 | 3 | $session->set('eccube.admin.customer.search', $searchData); |
|
| 90 | 3 | $session->set('eccube.admin.customer.search.page_no', $page_no); |
|
| 91 | } |
||
| 92 | } else { |
||
| 93 | 3 | if (is_null($page_no) && $request->get('resume') != Constant::ENABLED) { |
|
| 94 | // sessionを削除 |
||
| 95 | 2 | $session->remove('eccube.admin.customer.search'); |
|
| 96 | 2 | $session->remove('eccube.admin.customer.search.page_no'); |
|
| 97 | } else { |
||
| 98 | // pagingなどの処理 |
||
| 99 | 1 | $searchData = $session->get('eccube.admin.customer.search'); |
|
| 100 | 1 | if (is_null($page_no)) { |
|
| 101 | $page_no = intval($session->get('eccube.admin.customer.search.page_no')); |
||
| 102 | } else { |
||
| 103 | 1 | $session->set('eccube.admin.customer.search.page_no', $page_no); |
|
| 104 | } |
||
| 105 | 1 | if (!is_null($searchData)) { |
|
| 106 | // 表示件数 |
||
| 107 | $pcount = $request->get('page_count'); |
||
| 108 | $page_count = empty($pcount) ? $page_count : $pcount; |
||
| 109 | |||
| 110 | $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData); |
||
| 111 | |||
| 112 | $event = new EventArgs( |
||
| 113 | array( |
||
| 114 | 'form' => $searchForm, |
||
| 115 | 'qb' => $qb, |
||
| 116 | ), |
||
| 117 | $request |
||
| 118 | ); |
||
| 119 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event); |
||
| 120 | |||
| 121 | $pagination = $app['paginator']()->paginate( |
||
| 122 | $qb, |
||
| 123 | $page_no, |
||
| 124 | $page_count |
||
| 125 | ); |
||
| 126 | |||
| 127 | // セッションから検索条件を復元 |
||
| 128 | View Code Duplication | if (count($searchData['sex']) > 0) { |
|
| 129 | $sex_ids = array(); |
||
| 130 | foreach ($searchData['sex'] as $Sex) { |
||
| 131 | $sex_ids[] = $Sex->getId(); |
||
| 132 | } |
||
| 133 | $searchData['sex'] = $app['eccube.repository.master.sex']->findBy(array('id' => $sex_ids)); |
||
| 134 | } |
||
| 135 | |||
| 136 | if (!is_null($searchData['pref'])) { |
||
| 137 | $searchData['pref'] = $app['eccube.repository.master.pref']->find($searchData['pref']->getId()); |
||
| 138 | } |
||
| 139 | $searchForm->setData($searchData); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | 6 | return $app->render('Customer/index.twig', array( |
|
| 144 | 6 | 'searchForm' => $searchForm->createView(), |
|
| 145 | 6 | 'pagination' => $pagination, |
|
| 146 | 6 | 'pageMaxis' => $pageMaxis, |
|
| 147 | 6 | 'page_no' => $page_no, |
|
| 148 | 6 | 'page_count' => $page_count, |
|
| 149 | 6 | 'active' => $active, |
|
| 150 | )); |
||
| 151 | } |
||
| 152 | |||
| 153 | 2 | public function resend(Application $app, Request $request, $id) |
|
| 183 | |||
| 184 | 2 | public function delete(Application $app, Request $request, $id) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * 会員CSVの出力. |
||
| 224 | * @param Application $app |
||
| 225 | * @param Request $request |
||
| 226 | * @return StreamedResponse |
||
| 227 | */ |
||
| 228 | public function export(Application $app, Request $request) |
||
| 284 | } |
||
| 285 |