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 |
||
| 41 | class MailController extends AbstractController |
||
|
|
|||
| 42 | { |
||
| 43 | /** |
||
| 44 | * @var MailService |
||
| 45 | */ |
||
| 46 | protected $mailService; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var MailHistoryRepository |
||
| 50 | */ |
||
| 51 | protected $mailHistoryRepository; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @Inject(OrderRepository::class) |
||
| 55 | * |
||
| 56 | * @var OrderRepository |
||
| 57 | */ |
||
| 58 | protected $orderRepository; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * MailController constructor. |
||
| 62 | * |
||
| 63 | * @param MailService $mailService |
||
| 64 | * @param MailHistoryRepository $mailHistoryRepository |
||
| 65 | * @param OrderRepository $orderRepository |
||
| 66 | */ |
||
| 67 | 5 | public function __construct( |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @Route("/%eccube_admin_route%/order/{id}/mail", requirements={"id" = "\d+"}, name="admin_order_mail") |
||
| 79 | * @Template("@admin/Order/mail.twig") |
||
| 80 | */ |
||
| 81 | 2 | public function index(Request $request, Order $Order) |
|
| 82 | { |
||
| 83 | 2 | $MailHistories = $this->mailHistoryRepository->findBy(['Order' => $Order]); |
|
| 84 | |||
| 85 | 2 | $builder = $this->formFactory->createBuilder(MailType::class); |
|
| 86 | |||
| 87 | 2 | $event = new EventArgs( |
|
| 88 | [ |
||
| 89 | 2 | 'builder' => $builder, |
|
| 90 | 2 | 'Order' => $Order, |
|
| 91 | 2 | 'MailHistories' => $MailHistories, |
|
| 92 | ], |
||
| 93 | 2 | $request |
|
| 94 | ); |
||
| 95 | 2 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_INITIALIZE, $event); |
|
| 96 | |||
| 97 | 2 | $form = $builder->getForm(); |
|
| 98 | |||
| 99 | 2 | if ('POST' === $request->getMethod()) { |
|
| 100 | 1 | $form->handleRequest($request); |
|
| 101 | |||
| 102 | 1 | $mode = $request->get('mode'); |
|
| 103 | |||
| 104 | // テンプレート変更の場合は. バリデーション前に内容差し替え. |
||
| 105 | 1 | if ($mode == 'change') { |
|
| 106 | View Code Duplication | if ($form->get('template')->isValid()) { |
|
| 107 | /** @var $data \Eccube\Entity\MailTemplate */ |
||
| 108 | $MailTemplate = $form->get('template')->getData(); |
||
| 109 | $form = $builder->getForm(); |
||
| 110 | $event = new EventArgs( |
||
| 111 | [ |
||
| 112 | 'form' => $form, |
||
| 113 | 'Order' => $Order, |
||
| 114 | 'MailTemplate' => $MailTemplate, |
||
| 115 | ], |
||
| 116 | $request |
||
| 117 | ); |
||
| 118 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_CHANGE, $event); |
||
| 119 | $form->get('template')->setData($MailTemplate); |
||
| 120 | $form->get('mail_subject')->setData($MailTemplate->getMailSubject()); |
||
| 121 | $form->get('mail_header')->setData($MailTemplate->getMailHeader()); |
||
| 122 | $form->get('mail_footer')->setData($MailTemplate->getMailFooter()); |
||
| 123 | } |
||
| 124 | } else { |
||
| 125 | 1 | if ($form->isValid()) { |
|
| 126 | 1 | $data = $form->getData(); |
|
| 127 | 1 | $body = $this->createBody($data['mail_header'], $data['mail_footer'], $Order); |
|
| 128 | |||
| 129 | // メール送信 |
||
| 130 | 1 | $this->mailService->sendAdminOrderMail($Order, $data); |
|
| 131 | |||
| 132 | // 送信履歴を保存. |
||
| 133 | 1 | $MailTemplate = $form->get('template')->getData(); |
|
| 134 | 1 | $MailHistory = new MailHistory(); |
|
| 135 | $MailHistory |
||
| 136 | 1 | ->setMailSubject($data['mail_subject']) |
|
| 137 | 1 | ->setMailBody($body) |
|
| 138 | 1 | ->setSendDate(new \DateTime()) |
|
| 139 | 1 | ->setOrder($Order); |
|
| 140 | |||
| 141 | 1 | $this->entityManager->persist($MailHistory); |
|
| 142 | 1 | $this->entityManager->flush($MailHistory); |
|
| 143 | |||
| 144 | 1 | $event = new EventArgs( |
|
| 145 | [ |
||
| 146 | 1 | 'form' => $form, |
|
| 147 | 1 | 'Order' => $Order, |
|
| 148 | 1 | 'MailTemplate' => $MailTemplate, |
|
| 149 | 1 | 'MailHistory' => $MailHistory, |
|
| 150 | ], |
||
| 151 | 1 | $request |
|
| 152 | ); |
||
| 153 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_INDEX_COMPLETE, $event); |
|
| 154 | |||
| 155 | 1 | return $this->redirectToRoute('admin_order_page', ['page_no' => $this->session->get('eccube.admin.order.search.page_no', 1)]); |
|
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | // 本文確認用 |
||
| 161 | 1 | $body = $this->createBody('', '', $Order); |
|
| 162 | |||
| 163 | return [ |
||
| 164 | 1 | 'form' => $form->createView(), |
|
| 165 | 1 | 'Order' => $Order, |
|
| 166 | 1 | 'MailHistories' => $MailHistories, |
|
| 167 | 1 | 'body' => $body, |
|
| 168 | ]; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @Route("/%eccube_admin_route%/order/mail/view", name="admin_order_mail_view") |
||
| 173 | * @Template("@admin/Order/mail_view.twig") |
||
| 174 | */ |
||
| 175 | 1 | public function view(Request $request) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @Route("/%eccube_admin_route%/order/mail/mail_all", name="admin_order_mail_all") |
||
| 204 | * @Template("@admin/Order/mail_all.twig") |
||
| 205 | */ |
||
| 206 | 2 | public function mailAll(Request $request) |
|
| 207 | { |
||
| 208 | 2 | $builder = $this->formFactory->createBuilder(MailType::class); |
|
| 209 | |||
| 210 | 2 | $event = new EventArgs( |
|
| 211 | [ |
||
| 212 | 2 | 'builder' => $builder, |
|
| 213 | ], |
||
| 214 | 2 | $request |
|
| 215 | ); |
||
| 216 | 2 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_INITIALIZE, $event); |
|
| 217 | |||
| 218 | 2 | $form = $builder->getForm(); |
|
| 219 | |||
| 220 | 2 | $ids = ''; |
|
| 221 | 2 | if ('POST' === $request->getMethod()) { |
|
| 222 | 1 | $form->handleRequest($request); |
|
| 223 | |||
| 224 | 1 | $mode = $request->get('mode'); |
|
| 225 | |||
| 226 | 1 | $ids = $request->get('ids'); |
|
| 227 | |||
| 228 | // テンプレート変更の場合は. バリデーション前に内容差し替え. |
||
| 229 | 1 | if ($mode == 'change') { |
|
| 230 | View Code Duplication | if ($form->get('template')->isValid()) { |
|
| 231 | /** @var $data \Eccube\Entity\MailTemplate */ |
||
| 232 | $MailTemplate = $form->get('template')->getData(); |
||
| 233 | $form = $builder->getForm(); |
||
| 234 | |||
| 235 | $event = new EventArgs( |
||
| 236 | [ |
||
| 237 | 'form' => $form, |
||
| 238 | 'MailTemplate' => $MailTemplate, |
||
| 239 | ], |
||
| 240 | $request |
||
| 241 | ); |
||
| 242 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_CHANGE, $event); |
||
| 243 | |||
| 244 | $form->get('template')->setData($MailTemplate); |
||
| 245 | $form->get('mail_subject')->setData($MailTemplate->getMailSubject()); |
||
| 246 | $form->get('mail_header')->setData($MailTemplate->getMailHeader()); |
||
| 247 | $form->get('mail_footer')->setData($MailTemplate->getMailFooter()); |
||
| 248 | } |
||
| 249 | } else { |
||
| 250 | 1 | if ($form->isValid()) { |
|
| 251 | 1 | $data = $form->getData(); |
|
| 252 | |||
| 253 | 1 | $ids = explode(',', $ids); |
|
| 254 | |||
| 255 | 1 | foreach ($ids as $value) { |
|
| 256 | 1 | $Order = $this->orderRepository->find($value); |
|
| 257 | |||
| 258 | 1 | $body = $this->createBody($data['mail_header'], $data['mail_footer'], $Order); |
|
| 259 | |||
| 260 | // メール送信 |
||
| 261 | 1 | $this->mailService->sendAdminOrderMail($Order, $data); |
|
| 262 | |||
| 263 | // 送信履歴を保存. |
||
| 264 | 1 | $MailHistory = new MailHistory(); |
|
| 265 | $MailHistory |
||
| 266 | 1 | ->setMailSubject($data['mail_subject']) |
|
| 267 | 1 | ->setMailBody($body) |
|
| 268 | 1 | ->setSendDate(new \DateTime()) |
|
| 269 | 1 | ->setOrder($Order); |
|
| 270 | 1 | $this->entityManager->persist($MailHistory); |
|
| 271 | } |
||
| 272 | |||
| 273 | 1 | $this->entityManager->flush($MailHistory); |
|
| 274 | |||
| 275 | 1 | $event = new EventArgs( |
|
| 276 | [ |
||
| 277 | 1 | 'form' => $form, |
|
| 278 | 1 | 'MailHistory' => $MailHistory, |
|
| 279 | ], |
||
| 280 | 1 | $request |
|
| 281 | ); |
||
| 282 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::ADMIN_ORDER_MAIL_MAIL_ALL_COMPLETE, $event); |
|
| 283 | |||
| 284 | 1 | return $this->redirectToRoute('admin_order_page', ['page_no' => $this->session->get('eccube.admin.order.search.page_no', 1)]); |
|
| 285 | } |
||
| 286 | } |
||
| 287 | } else { |
||
| 288 | 1 | $ids = implode(',', (array) $request->get('ids')); |
|
| 289 | } |
||
| 290 | |||
| 291 | // 本文確認用 |
||
| 292 | 1 | $body = ''; |
|
| 293 | 1 | if ($ids != '') { |
|
| 294 | $idArray = explode(',', $ids); |
||
| 295 | $Order = $this->orderRepository->find($idArray[0]); |
||
| 296 | $body = $this->createBody('', '', $Order); |
||
| 297 | } |
||
| 298 | |||
| 299 | return [ |
||
| 300 | 1 | 'form' => $form->createView(), |
|
| 301 | 1 | 'ids' => $ids, |
|
| 302 | 1 | 'body' => $body, |
|
| 303 | ]; |
||
| 304 | } |
||
| 305 | |||
| 306 | 3 | private function createBody($header, $footer, $Order) |
|
| 314 | } |
||
| 315 |