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 |
||
| 31 | class MailService |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var \Swift_Mailer |
||
| 35 | */ |
||
| 36 | protected $mailer; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var MailTemplateRepository |
||
| 40 | */ |
||
| 41 | protected $mailTemplateRepository; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var MailHistoryRepository |
||
| 45 | */ |
||
| 46 | private $mailHistoryRepository; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var EventDispatcher |
||
| 50 | */ |
||
| 51 | protected $eventDispatcher; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var BaseInfo |
||
| 55 | */ |
||
| 56 | protected $BaseInfo; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var EccubeConfig |
||
| 60 | */ |
||
| 61 | protected $eccubeConfig; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \Twig_Environment |
||
| 65 | */ |
||
| 66 | protected $twig; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * MailService constructor. |
||
| 70 | * |
||
| 71 | * @param \Swift_Mailer $mailer |
||
| 72 | * @param MailTemplateRepository $mailTemplateRepository |
||
| 73 | * @param MailHistoryRepository $mailHistoryRepository |
||
| 74 | * @param BaseInfo $baseInfo |
||
| 75 | * @param EventDispatcherInterface $eventDispatcher |
||
| 76 | * @param \Twig_Environment $twig |
||
| 77 | * @param EccubeConfig $eccubeConfig |
||
| 78 | */ |
||
| 79 | 138 | public function __construct( |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Send customer confirm mail. |
||
| 99 | * |
||
| 100 | * @param $Customer 会員情報 |
||
| 101 | * @param string $activateUrl アクティベート用url |
||
| 102 | */ |
||
| 103 | 2 | public function sendCustomerConfirmMail(\Eccube\Entity\Customer $Customer, $activateUrl) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * Send customer complete mail. |
||
| 146 | * |
||
| 147 | * @param $Customer 会員情報 |
||
| 148 | */ |
||
| 149 | 2 | View Code Duplication | public function sendCustomerCompleteMail(\Eccube\Entity\Customer $Customer) |
| 187 | |||
| 188 | /** |
||
| 189 | * Send withdraw mail. |
||
| 190 | * |
||
| 191 | * @param $Customer Customer |
||
| 192 | * @param $email string |
||
| 193 | */ |
||
| 194 | 2 | View Code Duplication | public function sendCustomerWithdrawMail(Customer $Customer, string $email) |
| 233 | |||
| 234 | /** |
||
| 235 | * Send contact mail. |
||
| 236 | * |
||
| 237 | * @param $formData お問い合わせ内容 |
||
| 238 | */ |
||
| 239 | 4 | View Code Duplication | public function sendContactMail($formData) |
| 278 | |||
| 279 | /** |
||
| 280 | * Alias of sendContactMail(). |
||
| 281 | * |
||
| 282 | * @param $formData お問い合わせ内容 |
||
| 283 | * |
||
| 284 | * @see sendContactMail() |
||
| 285 | * @deprecated since 3.0.0, to be removed in 3.1 |
||
| 286 | * @see https://github.com/EC-CUBE/ec-cube/issues/1315 |
||
| 287 | */ |
||
| 288 | public function sendrContactMail($formData) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Send order mail. |
||
| 295 | * |
||
| 296 | * @param \Eccube\Entity\Order $Order 受注情報 |
||
| 297 | * |
||
| 298 | * @return \Swift_Message |
||
| 299 | */ |
||
| 300 | 7 | View Code Duplication | public function sendOrderMail(\Eccube\Entity\Order $Order) |
| 338 | |||
| 339 | /** |
||
| 340 | * Send admin customer confirm mail. |
||
| 341 | * |
||
| 342 | * @param $Customer 会員情報 |
||
| 343 | * @param string $activateUrl アクティベート用url |
||
| 344 | */ |
||
| 345 | 2 | public function sendAdminCustomerConfirmMail(\Eccube\Entity\Customer $Customer, $activateUrl) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Send admin order mail. |
||
| 389 | * |
||
| 390 | * @param Order $Order 受注情報 |
||
| 391 | * @param $formData 入力内容 |
||
| 392 | * @param string $twig テンプレートファイル名 |
||
| 393 | * |
||
| 394 | * @return \Swift_Message |
||
| 395 | * |
||
| 396 | * @throws \Twig_Error_Loader |
||
| 397 | * @throws \Twig_Error_Runtime |
||
| 398 | * @throws \Twig_Error_Syntax |
||
| 399 | */ |
||
| 400 | 3 | public function sendAdminOrderMail(Order $Order, $formData, $twig = '@admin/Mail/order.twig') |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Send password reset notification mail. |
||
| 439 | * |
||
| 440 | * @param $Customer 会員情報 |
||
| 441 | * @param string $reset_url |
||
| 442 | */ |
||
| 443 | 1 | public function sendPasswordResetNotificationMail(\Eccube\Entity\Customer $Customer, $reset_url) |
|
| 444 | { |
||
| 445 | 1 | log_info('パスワード再発行メール送信開始'); |
|
| 446 | |||
| 447 | 1 | $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_forgot_mail_template_id']); |
|
| 448 | 1 | $body = $this->twig->render($MailTemplate->getFileName(), [ |
|
| 449 | 1 | 'BaseInfo' => $this->BaseInfo, |
|
| 450 | 1 | 'header' => $MailTemplate->getMailHeader(), |
|
| 451 | 1 | 'footer' => $MailTemplate->getMailFooter(), |
|
| 452 | 1 | 'Customer' => $Customer, |
|
| 453 | 1 | 'expire' => $this->eccubeConfig['eccube_customer_reset_expire'], |
|
| 454 | 1 | 'reset_url' => $reset_url, |
|
| 455 | ]); |
||
| 456 | |||
| 457 | 1 | $message = (new \Swift_Message()) |
|
| 458 | 1 | ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject()) |
|
| 459 | 1 | ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()]) |
|
| 460 | 1 | ->setTo([$Customer->getEmail()]) |
|
| 461 | 1 | ->setReplyTo($this->BaseInfo->getEmail03()) |
|
| 462 | 1 | ->setReturnPath($this->BaseInfo->getEmail04()) |
|
| 463 | 1 | ->setBody($body); |
|
| 464 | |||
| 465 | 1 | $event = new EventArgs( |
|
| 466 | [ |
||
| 467 | 1 | 'message' => $message, |
|
| 468 | 1 | 'Customer' => $Customer, |
|
| 469 | 1 | 'BaseInfo' => $this->BaseInfo, |
|
| 470 | 1 | 'resetUrl' => $reset_url, |
|
| 471 | ], |
||
| 472 | 1 | null |
|
| 473 | ); |
||
| 474 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::MAIL_PASSWORD_RESET, $event); |
|
| 475 | |||
| 476 | 1 | $count = $this->mailer->send($message); |
|
| 477 | |||
| 478 | 1 | log_info('パスワード再発行メール送信完了', ['count' => $count]); |
|
| 479 | |||
| 480 | 1 | return $count; |
|
| 481 | } |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Send password reset notification mail. |
||
| 485 | * |
||
| 486 | * @param $Customer 会員情報 |
||
| 487 | * @param string $password |
||
| 488 | */ |
||
| 489 | 1 | public function sendPasswordResetCompleteMail(\Eccube\Entity\Customer $Customer, $password) |
|
| 490 | { |
||
| 491 | 1 | log_info('パスワード変更完了メール送信開始'); |
|
| 492 | |||
| 493 | 1 | $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_reset_complete_mail_template_id']); |
|
| 494 | |||
| 495 | 1 | $body = $this->twig->render($MailTemplate->getFileName(), [ |
|
| 496 | 1 | 'BaseInfo' => $this->BaseInfo, |
|
| 497 | 1 | 'header' => $MailTemplate->getMailHeader(), |
|
| 498 | 1 | 'footer' => $MailTemplate->getMailFooter(), |
|
| 499 | 1 | 'Customer' => $Customer, |
|
| 500 | 1 | 'password' => $password, |
|
| 501 | ]); |
||
| 502 | |||
| 503 | 1 | $message = (new \Swift_Message()) |
|
| 504 | 1 | ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject()) |
|
| 505 | 1 | ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()]) |
|
| 506 | 1 | ->setTo([$Customer->getEmail()]) |
|
| 507 | 1 | ->setBcc($this->BaseInfo->getEmail01()) |
|
| 508 | 1 | ->setReplyTo($this->BaseInfo->getEmail03()) |
|
| 509 | 1 | ->setReturnPath($this->BaseInfo->getEmail04()) |
|
| 510 | 1 | ->setBody($body); |
|
| 511 | |||
| 512 | 1 | $event = new EventArgs( |
|
| 513 | [ |
||
| 514 | 1 | 'message' => $message, |
|
| 515 | 1 | 'Customer' => $Customer, |
|
| 516 | 1 | 'BaseInfo' => $this->BaseInfo, |
|
| 517 | 1 | 'password' => $password, |
|
| 518 | ], |
||
| 519 | 1 | null |
|
| 520 | ); |
||
| 521 | 1 | $this->eventDispatcher->dispatch(EccubeEvents::MAIL_PASSWORD_RESET_COMPLETE, $event); |
|
| 522 | |||
| 523 | 1 | $count = $this->mailer->send($message); |
|
| 524 | |||
| 525 | 1 | log_info('パスワード変更完了メール送信完了', ['count' => $count]); |
|
| 526 | |||
| 527 | 1 | return $count; |
|
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * ポイントでマイナス発生時にメール通知する。 |
||
| 532 | * |
||
| 533 | * @param Order $Order |
||
| 534 | * @param int $currentPoint |
||
| 535 | * @param int $changePoint |
||
| 536 | */ |
||
| 537 | public function sendPointNotifyMail(\Eccube\Entity\Order $Order, $currentPoint = 0, $changePoint = 0) |
||
| 538 | { |
||
| 539 | $body = $this->twig->render('Mail/point_notify.twig', [ |
||
| 540 | 'Order' => $Order, |
||
| 541 | 'currentPoint' => $currentPoint, |
||
| 542 | 'changePoint' => $changePoint, |
||
| 543 | ]); |
||
| 544 | |||
| 545 | $message = (new \Swift_Message()) |
||
| 546 | ->setSubject('['.$this->BaseInfo->getShopName().'] ポイント通知') |
||
| 547 | ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()]) |
||
| 548 | ->setTo([$this->BaseInfo->getEmail01()]) |
||
| 549 | ->setBcc($this->BaseInfo->getEmail01()) |
||
| 550 | ->setReplyTo($this->BaseInfo->getEmail03()) |
||
| 551 | ->setReturnPath($this->BaseInfo->getEmail04()) |
||
| 552 | ->setBody($body); |
||
| 553 | |||
| 554 | $this->mailer->send($message); |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * 発送通知メールを送信する. |
||
| 559 | * 発送通知メールは受注ごとに送られる |
||
| 560 | * |
||
| 561 | * @param Shipping $Shipping |
||
| 562 | * |
||
| 563 | * @throws \Twig_Error |
||
| 564 | */ |
||
| 565 | 4 | public function sendShippingNotifyMail(Shipping $Shipping) |
|
| 566 | { |
||
| 567 | 4 | log_info('出荷通知メール送信処理開始', ['id' => $Shipping->getId()]); |
|
| 568 | |||
| 569 | 4 | $MailTemplate = $this->mailTemplateRepository->find($this->eccubeConfig['eccube_shipping_notify_mail_template_id']); |
|
| 570 | |||
| 571 | /** @var Order $Order */ |
||
| 572 | 4 | $Order = $Shipping->getOrder(); |
|
| 573 | 4 | $message = (new \Swift_Message()) |
|
| 574 | 4 | ->setSubject('['.$this->BaseInfo->getShopName().'] '.$MailTemplate->getMailSubject()) |
|
| 575 | 4 | ->setFrom([$this->BaseInfo->getEmail01() => $this->BaseInfo->getShopName()]) |
|
| 576 | 4 | ->setTo($Order->getEmail()) |
|
| 577 | 4 | ->setBcc($this->BaseInfo->getEmail01()) |
|
| 578 | 4 | ->setReplyTo($this->BaseInfo->getEmail03()) |
|
| 579 | 4 | ->setReturnPath($this->BaseInfo->getEmail04()) |
|
| 580 | 4 | ->setBody($this->getShippingNotifyMailBody($Shipping, $Order, $MailTemplate)); |
|
| 581 | |||
| 582 | 4 | $this->mailer->send($message); |
|
| 583 | |||
| 584 | 4 | $MailHistory = new MailHistory(); |
|
| 585 | 4 | $MailHistory->setMailSubject($message->getSubject()) |
|
| 586 | 4 | ->setMailBody($message->getBody()) |
|
| 587 | 4 | ->setOrder($Order) |
|
| 588 | 4 | ->setSendDate(new \DateTime()); |
|
| 589 | |||
| 590 | 4 | $this->mailHistoryRepository->save($MailHistory); |
|
| 591 | |||
| 592 | 4 | log_info('出荷通知メール送信処理完了', ['id' => $Shipping->getId()]); |
|
| 593 | } |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @param Shipping $Shipping |
||
| 597 | * @param Order $Order |
||
| 598 | * @param MailTemplate|null $MailTemplate |
||
| 599 | * |
||
| 600 | * @return string |
||
| 601 | * |
||
| 602 | * @throws \Twig_Error |
||
| 603 | */ |
||
| 604 | public function getShippingNotifyMailBody(Shipping $Shipping, Order $Order, MailTemplate $MailTemplate = null) |
||
| 620 | } |
||
| 621 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.