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 | 10 | 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 | 6 | ), |
|
| 50 | $request |
||
| 51 | 6 | ); |
|
| 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 | 10 | 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 | 3 | ), |
|
| 78 | $request |
||
| 79 | 3 | ); |
|
| 80 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_INDEX_SEARCH, $event); |
|
| 81 | |||
| 82 | 3 | $pagination = $app['paginator']()->paginate( |
|
| 83 | 3 | $qb, |
|
| 84 | 3 | $page_no, |
|
| 85 | $page_count |
||
| 86 | 3 | ); |
|
| 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 | 3 | } |
|
| 92 | 3 | } 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 | 2 | } 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 | 1 | $pcount = $request->get('page_count'); |
|
| 108 | $page_count = empty($pcount) ? $page_count : $pcount; |
||
| 109 | |||
| 110 | 1 | $qb = $app['eccube.repository.customer']->getQueryBuilderBySearchData($searchData); |
|
| 111 | |||
| 112 | $event = new EventArgs( |
||
| 113 | array( |
||
| 114 | 'form' => $searchForm, |
||
| 115 | 'qb' => $qb, |
||
| 116 | ), |
||
| 117 | $request |
||
| 118 | 1 | ); |
|
| 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 | 6 | )); |
|
| 151 | } |
||
| 152 | |||
| 153 | 2 | public function resend(Application $app, Request $request, $id) |
|
| 154 | { |
||
| 155 | 2 | $this->isTokenValid($app); |
|
| 156 | |||
| 157 | 2 | $Customer = $app['orm.em'] |
|
| 158 | 2 | ->getRepository('Eccube\Entity\Customer') |
|
| 159 | 2 | ->find($id); |
|
| 160 | |||
| 161 | 2 | if (is_null($Customer)) { |
|
| 162 | throw new NotFoundHttpException(); |
||
| 163 | } |
||
| 164 | |||
| 165 | 2 | $activateUrl = $app->url('entry_activate', array('secret_key' => $Customer->getSecretKey())); |
|
| 166 | |||
| 167 | // メール送信 |
||
| 168 | 2 | $app['eccube.service.mail']->sendAdminCustomerConfirmMail($Customer, $activateUrl); |
|
| 169 | |||
| 170 | 2 | $event = new EventArgs( |
|
| 171 | array( |
||
| 172 | 2 | 'Customer' => $Customer, |
|
| 173 | 2 | 'activateUrl' => $activateUrl, |
|
| 174 | 2 | ), |
|
| 175 | 1 | $request |
|
| 176 | 2 | ); |
|
| 177 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_RESEND_COMPLETE, $event); |
|
| 178 | |||
| 179 | 2 | $app->addSuccess('admin.customer.resend.complete', 'admin'); |
|
| 180 | |||
| 181 | 2 | return $app->redirect($app->url('admin_customer')); |
|
| 182 | } |
||
| 183 | |||
| 184 | 2 | public function delete(Application $app, Request $request, $id) |
|
| 185 | { |
||
| 186 | 2 | $this->isTokenValid($app); |
|
| 187 | |||
| 188 | 2 | $session = $request->getSession(); |
|
| 189 | 2 | $page_no = intval($session->get('eccube.admin.customer.search.page_no')); |
|
| 190 | 2 | $page_no = $page_no ? $page_no : Constant::ENABLED; |
|
| 191 | |||
| 192 | 2 | $Customer = $app['orm.em'] |
|
| 193 | 2 | ->getRepository('Eccube\Entity\Customer') |
|
| 194 | 2 | ->find($id); |
|
| 195 | |||
| 196 | 2 | if (!$Customer) { |
|
| 197 | $app->deleteMessage(); |
||
| 198 | return $app->redirect($app->url('admin_customer_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED); |
||
| 199 | } |
||
| 200 | |||
| 201 | 2 | $Customer->setDelFlg(Constant::ENABLED); |
|
| 202 | 2 | $app['orm.em']->persist($Customer); |
|
| 203 | 2 | $app['orm.em']->flush(); |
|
| 204 | |||
| 205 | 2 | $event = new EventArgs( |
|
| 206 | array( |
||
| 207 | 2 | 'Customer' => $Customer, |
|
| 208 | 2 | ), |
|
| 209 | $request |
||
| 210 | 2 | ); |
|
| 211 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_CUSTOMER_DELETE_COMPLETE, $event); |
|
| 212 | |||
| 213 | 2 | $app->addSuccess('admin.customer.delete.complete', 'admin'); |
|
| 214 | |||
| 215 | 2 | return $app->redirect($app->url('admin_customer_page', array('page_no' => $page_no)).'?resume='.Constant::ENABLED); |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * 会員CSVの出力. |
||
| 220 | * @param Application $app |
||
| 221 | * @param Request $request |
||
| 222 | * @return StreamedResponse |
||
| 223 | */ |
||
| 224 | 6 | public function export(Application $app, Request $request) |
|
| 278 | } |
||
| 279 |