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 |
||
| 36 | class AdminController extends AbstractController |
||
|
|
|||
| 37 | { |
||
| 38 | 6 | public function login(Application $app, Request $request) |
|
| 63 | |||
| 64 | 9 | public function index(Application $app, Request $request) |
|
| 65 | { |
||
| 66 | // install.phpのチェック. |
||
| 67 | 3 | if (isset($app['config']['eccube_install']) && $app['config']['eccube_install'] == 1) { |
|
| 68 | 3 | $file = $app['config']['root_dir'] . '/html/install.php'; |
|
| 69 | 3 | View Code Duplication | if (file_exists($file)) { |
| 70 | 3 | $message = $app->trans('admin.install.warning', array('installphpPath' => 'html/install.php')); |
|
| 71 | 3 | $app->addWarning($message, 'admin'); |
|
| 72 | 3 | } |
|
| 73 | 3 | $fileOnRoot = $app['config']['root_dir'] . '/install.php'; |
|
| 74 | 3 | View Code Duplication | if (file_exists($fileOnRoot)) { |
| 75 | $message = $app->trans('admin.install.warning', array('installphpPath' => 'install.php')); |
||
| 76 | $app->addWarning($message, 'admin'); |
||
| 77 | } |
||
| 78 | 3 | } |
|
| 79 | |||
| 80 | // 受注マスター検索用フォーム |
||
| 81 | 3 | $searchOrderBuilder = $app['form.factory'] |
|
| 82 | 3 | ->createBuilder('admin_search_order'); |
|
| 83 | // 商品マスター検索用フォーム |
||
| 84 | 3 | $searchProductBuilder = $app['form.factory'] |
|
| 85 | 3 | ->createBuilder('admin_search_product'); |
|
| 86 | // 会員マスター検索用フォーム |
||
| 87 | 3 | $searchCustomerBuilder = $app['form.factory'] |
|
| 88 | 3 | ->createBuilder('admin_search_customer'); |
|
| 89 | |||
| 90 | 3 | $event = new EventArgs( |
|
| 91 | array( |
||
| 92 | 3 | 'searchOrderBuilder' => $searchOrderBuilder, |
|
| 93 | 3 | 'searchProductBuilder' => $searchProductBuilder, |
|
| 94 | 3 | 'searchCustomerBuilder' => $searchCustomerBuilder, |
|
| 95 | 3 | ), |
|
| 96 | $request |
||
| 97 | 3 | ); |
|
| 98 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_INITIALIZE, $event); |
|
| 99 | |||
| 100 | // 受注マスター検索用フォーム |
||
| 101 | 3 | $searchOrderForm = $searchOrderBuilder->getForm(); |
|
| 102 | |||
| 103 | // 商品マスター検索用フォーム |
||
| 104 | 3 | $searchProductForm = $searchProductBuilder->getForm(); |
|
| 105 | |||
| 106 | // 会員マスター検索用フォーム |
||
| 107 | 3 | $searchCustomerForm = $searchCustomerBuilder->getForm(); |
|
| 108 | |||
| 109 | /** |
||
| 110 | * 受注状況. |
||
| 111 | */ |
||
| 112 | 3 | $excludes = array(); |
|
| 113 | 3 | $excludes[] = $app['config']['order_pending']; |
|
| 114 | 3 | $excludes[] = $app['config']['order_processing']; |
|
| 115 | 3 | $excludes[] = $app['config']['order_cancel']; |
|
| 116 | 3 | $excludes[] = $app['config']['order_deliv']; |
|
| 117 | |||
| 118 | 3 | $event = new EventArgs( |
|
| 119 | array( |
||
| 120 | 3 | 'excludes' => $excludes, |
|
| 121 | 3 | ), |
|
| 122 | $request |
||
| 123 | 3 | ); |
|
| 124 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_ORDER, $event); |
|
| 125 | |||
| 126 | // 受注ステータスごとの受注件数. |
||
| 127 | 3 | $Orders = $this->getOrderEachStatus($app['orm.em'], $excludes); |
|
| 128 | // 受注ステータスの一覧. |
||
| 129 | 3 | $OrderStatuses = $this->findOrderStatus($app['orm.em'], $excludes); |
|
| 130 | |||
| 131 | /** |
||
| 132 | * 売り上げ状況 |
||
| 133 | */ |
||
| 134 | 3 | $excludes = array(); |
|
| 135 | 3 | $excludes[] = $app['config']['order_processing']; |
|
| 136 | 3 | $excludes[] = $app['config']['order_cancel']; |
|
| 137 | 3 | $excludes[] = $app['config']['order_pending']; |
|
| 138 | |||
| 139 | 3 | $event = new EventArgs( |
|
| 140 | array( |
||
| 141 | 3 | 'excludes' => $excludes, |
|
| 142 | 3 | ), |
|
| 143 | $request |
||
| 144 | 3 | ); |
|
| 145 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_SALES, $event); |
|
| 146 | |||
| 147 | // 今日の売上/件数 |
||
| 148 | 3 | $salesToday = $this->getSalesByDay($app['orm.em'], new \DateTime(), $excludes); |
|
| 149 | // 昨日の売上/件数 |
||
| 150 | 3 | $salesYesterday = $this->getSalesByDay($app['orm.em'], new \DateTime('-1 day'), $excludes); |
|
| 151 | // 今月の売上/件数 |
||
| 152 | 3 | $salesThisMonth = $this->getSalesByMonth($app['orm.em'], new \DateTime(), $excludes); |
|
| 153 | |||
| 154 | /** |
||
| 155 | * ショップ状況 |
||
| 156 | */ |
||
| 157 | // 在庫切れ商品数 |
||
| 158 | 3 | $countNonStockProducts = $this->countNonStockProducts($app['orm.em']); |
|
| 159 | // 本会員数 |
||
| 160 | 3 | $countCustomers = $this->countCustomers($app['orm.em']); |
|
| 161 | |||
| 162 | 9 | $event = new EventArgs( |
|
| 163 | array( |
||
| 164 | 3 | 'Orders' => $Orders, |
|
| 165 | 3 | 'OrderStatuses' => $OrderStatuses, |
|
| 166 | 3 | 'salesThisMonth' => $salesThisMonth, |
|
| 167 | 3 | 'salesToday' => $salesToday, |
|
| 168 | 3 | 'salesYesterday' => $salesYesterday, |
|
| 169 | 3 | 'countNonStockProducts' => $countNonStockProducts, |
|
| 170 | 3 | 'countCustomers' => $countCustomers, |
|
| 171 | 3 | ), |
|
| 172 | $request |
||
| 173 | 3 | ); |
|
| 174 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_INDEX_COMPLETE, $event); |
|
| 175 | |||
| 176 | 3 | return $app->render('index.twig', array( |
|
| 177 | 3 | 'searchOrderForm' => $searchOrderForm->createView(), |
|
| 178 | 3 | 'searchProductForm' => $searchProductForm->createView(), |
|
| 179 | 3 | 'searchCustomerForm' => $searchCustomerForm->createView(), |
|
| 180 | 3 | 'Orders' => $Orders, |
|
| 181 | 3 | 'OrderStatuses' => $OrderStatuses, |
|
| 182 | 3 | 'salesThisMonth' => $salesThisMonth, |
|
| 183 | 3 | 'salesToday' => $salesToday, |
|
| 184 | 3 | 'salesYesterday' => $salesYesterday, |
|
| 185 | 3 | 'countNonStockProducts' => $countNonStockProducts, |
|
| 186 | 3 | 'countCustomers' => $countCustomers, |
|
| 187 | 3 | )); |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * パスワード変更画面 |
||
| 192 | * |
||
| 193 | * @param Application $app |
||
| 194 | * @param Request $request |
||
| 195 | * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
||
| 196 | */ |
||
| 197 | 12 | public function changePassword(Application $app, Request $request) |
|
| 198 | { |
||
| 199 | 3 | $builder = $app['form.factory'] |
|
| 200 | 3 | ->createBuilder('admin_change_password'); |
|
| 201 | |||
| 202 | 3 | $event = new EventArgs( |
|
| 203 | array( |
||
| 204 | 3 | 'builder' => $builder, |
|
| 205 | 3 | ), |
|
| 206 | $request |
||
| 207 | 3 | ); |
|
| 208 | 3 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIM_CHANGE_PASSWORD_INITIALIZE, $event); |
|
| 209 | |||
| 210 | 3 | $form = $builder->getForm(); |
|
| 211 | 3 | $form->handleRequest($request); |
|
| 212 | |||
| 213 | 3 | if ($form->isSubmitted() && $form->isValid()) { |
|
| 214 | 1 | $password = $form->get('change_password')->getData(); |
|
| 215 | |||
| 216 | 1 | $Member = $app->user(); |
|
| 217 | |||
| 218 | 1 | $dummyMember = clone $Member; |
|
| 219 | 1 | $dummyMember->setPassword($password); |
|
| 220 | 1 | $salt = $dummyMember->getSalt(); |
|
| 221 | 1 | if (!isset($salt)) { |
|
| 222 | $salt = $app['eccube.repository.member']->createSalt(5); |
||
| 223 | $dummyMember->setSalt($salt); |
||
| 224 | } |
||
| 225 | |||
| 226 | 1 | $encryptPassword = $app['eccube.repository.member']->encryptPassword($dummyMember); |
|
| 227 | |||
| 228 | $Member |
||
| 229 | 1 | ->setPassword($encryptPassword) |
|
| 230 | 1 | ->setSalt($salt); |
|
| 231 | |||
| 232 | 1 | $status = $app['eccube.repository.member']->save($Member); |
|
| 233 | 1 | if ($status) { |
|
| 234 | 1 | $event = new EventArgs( |
|
| 235 | array( |
||
| 236 | 1 | 'form' => $form, |
|
| 237 | 1 | ), |
|
| 238 | $request |
||
| 239 | 12 | ); |
|
| 240 | 1 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ADMIN_CHANGE_PASSWORD_COMPLETE, $event); |
|
| 241 | |||
| 242 | 1 | $app->addSuccess('admin.change_password.save.complete', 'admin'); |
|
| 243 | |||
| 244 | 1 | return $app->redirect($app->url('admin_change_password')); |
|
| 245 | } |
||
| 246 | |||
| 247 | $app->addError('admin.change_password.save.error', 'admin'); |
||
| 248 | } |
||
| 249 | |||
| 250 | 2 | return $app->render('change_password.twig', array( |
|
| 251 | 2 | 'form' => $form->createView(), |
|
| 252 | 2 | )); |
|
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * 在庫なし商品の検索結果を表示する. |
||
| 257 | * |
||
| 258 | * @param Application $app |
||
| 259 | * @param Request $request |
||
| 260 | * @return \Symfony\Component\HttpFoundation\Response |
||
| 261 | */ |
||
| 262 | 2 | public function searchNonStockProducts(Application $app, Request $request) |
|
| 263 | { |
||
| 264 | // 商品マスター検索用フォーム |
||
| 265 | 2 | $form = $app['form.factory'] |
|
| 266 | 2 | ->createBuilder('admin_search_product') |
|
| 267 | 2 | ->getForm(); |
|
| 268 | |||
| 269 | 2 | if ('POST' === $request->getMethod()) { |
|
| 270 | 2 | $form->handleRequest($request); |
|
| 271 | |||
| 272 | 2 | if ($form->isValid()) { |
|
| 273 | // 在庫なし商品の検索条件をセッションに付与し, 商品マスタへリダイレクトする. |
||
| 274 | $searchData = array(); |
||
| 275 | $searchData['stock_status'] = Constant::DISABLED; |
||
| 276 | $session = $request->getSession(); |
||
| 277 | $session->set('eccube.admin.product.search', $searchData); |
||
| 278 | |||
| 279 | return $app->redirect($app->url('admin_product_page', array( |
||
| 280 | 'page_no' => 1, |
||
| 281 | 'status' => $app['config']['admin_product_stock_status']))); |
||
| 282 | } |
||
| 283 | 2 | } |
|
| 284 | |||
| 285 | 2 | return $app->redirect($app->url('admin_homepage')); |
|
| 286 | } |
||
| 287 | |||
| 288 | 3 | protected function findOrderStatus($em, array $excludes) |
|
| 289 | { |
||
| 290 | $qb = $em |
||
| 291 | 3 | ->getRepository('Eccube\Entity\Master\OrderStatus') |
|
| 292 | 3 | ->createQueryBuilder('os'); |
|
| 293 | |||
| 294 | return $qb |
||
| 295 | 3 | ->where($qb->expr()->notIn('os.id', $excludes)) |
|
| 296 | 3 | ->getQuery() |
|
| 297 | 3 | ->getResult(); |
|
| 298 | } |
||
| 299 | |||
| 300 | 3 | protected function getOrderEachStatus($em, array $excludes) |
|
| 301 | { |
||
| 302 | $sql = 'SELECT |
||
| 303 | t1.status as status, |
||
| 304 | COUNT(t1.order_id) as count |
||
| 305 | FROM |
||
| 306 | dtb_order t1 |
||
| 307 | WHERE |
||
| 308 | t1.del_flg = 0 |
||
| 309 | AND t1.status NOT IN (:excludes) |
||
| 310 | GROUP BY |
||
| 311 | t1.status |
||
| 312 | ORDER BY |
||
| 313 | 3 | t1.status'; |
|
| 314 | 3 | $rsm = new ResultSetMapping();; |
|
| 315 | 3 | $rsm->addScalarResult('status', 'status'); |
|
| 316 | 3 | $rsm->addScalarResult('count', 'count'); |
|
| 317 | 3 | $query = $em->createNativeQuery($sql, $rsm); |
|
| 318 | 3 | $query->setParameters(array(':excludes' => $excludes)); |
|
| 319 | 3 | $result = $query->getResult(); |
|
| 320 | 3 | $orderArray = array(); |
|
| 321 | 3 | foreach ($result as $row) { |
|
| 322 | 1 | $orderArray[$row['status']] = $row['count']; |
|
| 323 | 3 | } |
|
| 324 | |||
| 325 | 3 | return $orderArray; |
|
| 326 | } |
||
| 327 | |||
| 328 | 3 | View Code Duplication | protected function getSalesByMonth($em, $dateTime, array $excludes) |
| 358 | |||
| 359 | 3 | View Code Duplication | protected function getSalesByDay($em, $dateTime, array $excludes) |
| 360 | { |
||
| 361 | // concat... for pgsql |
||
| 362 | // http://stackoverflow.com/questions/1091924/substr-does-not-work-with-datatype-timestamp-in-postgres-8-3 |
||
| 363 | $dql = 'SELECT |
||
| 389 | |||
| 390 | 3 | protected function countNonStockProducts($em) |
|
| 404 | |||
| 405 | 3 | protected function countCustomers($em) |
|
| 422 | } |