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 |
||
| 34 | class MailController |
||
|
|
|||
| 35 | { |
||
| 36 | 6 | public function index(Application $app, Request $request, $id) |
|
| 168 | |||
| 169 | |||
| 170 | 1 | public function complete(Application $app) |
|
| 174 | |||
| 175 | |||
| 176 | 2 | public function view(Application $app, Request $request) |
|
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | 6 | public function mailAll(Application $app, Request $request) |
|
| 206 | { |
||
| 207 | |||
| 208 | 6 | $builder = $app['form.factory']->createBuilder('mail'); |
|
| 209 | |||
| 210 | 6 | $event = new EventArgs( |
|
| 211 | array( |
||
| 212 | 6 | 'builder' => $builder, |
|
| 213 | ), |
||
| 214 | $request |
||
| 215 | ); |
||
| 216 | 6 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_INITIALIZE, $event); |
|
| 217 | |||
| 218 | 6 | $form = $builder->getForm(); |
|
| 219 | |||
| 220 | 6 | $ids = ''; |
|
| 221 | 6 | if ('POST' === $request->getMethod()) { |
|
| 222 | |||
| 223 | 4 | $form->handleRequest($request); |
|
| 224 | |||
| 225 | 4 | $mode = $request->get('mode'); |
|
| 226 | |||
| 227 | 4 | $ids = $request->get('ids'); |
|
| 228 | |||
| 229 | // テンプレート変更の場合は. バリデーション前に内容差し替え. |
||
| 230 | 4 | if ($mode == 'change') { |
|
| 231 | View Code Duplication | if ($form->get('template')->isValid()) { |
|
| 232 | /** @var $data \Eccube\Entity\MailTemplate */ |
||
| 233 | $MailTemplate = $form->get('template')->getData(); |
||
| 234 | $form = $builder->getForm(); |
||
| 235 | |||
| 236 | $event = new EventArgs( |
||
| 237 | array( |
||
| 238 | 'form' => $form, |
||
| 239 | 'MailTemplate' => $MailTemplate, |
||
| 240 | ), |
||
| 241 | $request |
||
| 242 | ); |
||
| 243 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CHANGE, $event); |
||
| 244 | |||
| 245 | $form->get('template')->setData($MailTemplate); |
||
| 246 | $form->get('subject')->setData($MailTemplate->getSubject()); |
||
| 247 | $form->get('header')->setData($MailTemplate->getHeader()); |
||
| 248 | $form->get('footer')->setData($MailTemplate->getFooter()); |
||
| 249 | } |
||
| 250 | 4 | } else if ($form->isValid()) { |
|
| 251 | switch ($mode) { |
||
| 252 | 4 | case 'confirm': |
|
| 253 | // フォームをFreezeして再生成. |
||
| 254 | |||
| 255 | 2 | $builder->setAttribute('freeze', true); |
|
| 256 | 2 | $builder->setAttribute('freeze_display_text', true); |
|
| 257 | |||
| 258 | 2 | $data = $form->getData(); |
|
| 259 | |||
| 260 | 2 | $tmp = explode(',', $ids); |
|
| 261 | |||
| 262 | 2 | $Order = $app['eccube.repository.order']->find($tmp[0]); |
|
| 263 | |||
| 264 | 2 | if (is_null($Order)) { |
|
| 265 | throw new NotFoundHttpException('order not found.'); |
||
| 266 | } |
||
| 267 | |||
| 268 | 2 | $body = $this->createBody($app, $data['header'], $data['footer'], $Order); |
|
| 269 | |||
| 270 | 2 | $MailTemplate = $form->get('template')->getData(); |
|
| 271 | |||
| 272 | 2 | $form = $builder->getForm(); |
|
| 273 | |||
| 274 | 2 | $event = new EventArgs( |
|
| 275 | array( |
||
| 276 | 2 | 'form' => $form, |
|
| 277 | 2 | 'MailTemplate' => $MailTemplate, |
|
| 278 | 2 | 'Order' => $Order, |
|
| 279 | ), |
||
| 280 | $request |
||
| 281 | ); |
||
| 282 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CONFIRM, $event); |
|
| 283 | |||
| 284 | 2 | $form->setData($data); |
|
| 285 | 2 | $form->get('template')->setData($MailTemplate); |
|
| 286 | |||
| 287 | 2 | return $app->render('Order/mail_all_confirm.twig', array( |
|
| 288 | 2 | 'form' => $form->createView(), |
|
| 289 | 2 | 'body' => $body, |
|
| 290 | 2 | 'ids' => $ids, |
|
| 291 | )); |
||
| 292 | break; |
||
| 293 | |||
| 294 | 2 | case 'complete': |
|
| 295 | |||
| 296 | 2 | $data = $form->getData(); |
|
| 297 | |||
| 298 | 2 | $ids = explode(',', $ids); |
|
| 299 | |||
| 300 | 2 | foreach ($ids as $value) { |
|
| 301 | |||
| 302 | 2 | $Order = $app['eccube.repository.order']->find($value); |
|
| 303 | |||
| 304 | 2 | $body = $this->createBody($app, $data['header'], $data['footer'], $Order); |
|
| 305 | |||
| 306 | // メール送信 |
||
| 307 | 2 | $app['eccube.service.mail']->sendAdminOrderMail($Order, $data); |
|
| 308 | |||
| 309 | // 送信履歴を保存. |
||
| 310 | 2 | $MailTemplate = $form->get('template')->getData(); |
|
| 311 | 2 | $MailHistory = new MailHistory(); |
|
| 312 | $MailHistory |
||
| 313 | 2 | ->setSubject($data['subject']) |
|
| 314 | 2 | ->setMailBody($body) |
|
| 315 | 2 | ->setMailTemplate($MailTemplate) |
|
| 316 | 2 | ->setSendDate(new \DateTime()) |
|
| 317 | 2 | ->setOrder($Order); |
|
| 318 | 2 | $app['orm.em']->persist($MailHistory); |
|
| 319 | } |
||
| 320 | |||
| 321 | 2 | $app['orm.em']->flush($MailHistory); |
|
| 322 | |||
| 323 | 2 | $event = new EventArgs( |
|
| 324 | array( |
||
| 325 | 2 | 'form' => $form, |
|
| 326 | 2 | 'MailHistory' => $MailHistory, |
|
| 327 | ), |
||
| 328 | $request |
||
| 329 | ); |
||
| 330 | 2 | $app['eccube.event.dispatcher']->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_COMPLETE, $event); |
|
| 331 | |||
| 332 | 2 | return $app->redirect($app->url('admin_order_mail_complete')); |
|
| 333 | break; |
||
| 334 | default: |
||
| 335 | break; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | } else { |
||
| 339 | $filter = function ($v) { |
||
| 340 | return preg_match('/^ids\d+$/', $v); |
||
| 341 | 2 | }; |
|
| 342 | 2 | $map = function ($v) { |
|
| 343 | return preg_replace('/[^\d+]/', '', $v); |
||
| 344 | 2 | }; |
|
| 345 | 2 | $keys = array_keys($request->query->all()); |
|
| 346 | 2 | $idArray = array_map($map, array_filter($keys, $filter)); |
|
| 347 | 2 | $ids = implode(',', $idArray); |
|
| 348 | } |
||
| 349 | |||
| 350 | 2 | return $app->render('Order/mail_all.twig', array( |
|
| 351 | 2 | 'form' => $form->createView(), |
|
| 352 | 2 | 'ids' => $ids, |
|
| 353 | )); |
||
| 354 | } |
||
| 355 | |||
| 356 | |||
| 357 | 8 | private function createBody($app, $header, $footer, $Order) |
|
| 365 | } |
||
| 366 |