Skip to content

Commit 1505ce1

Browse files
committed
refactor(MailgunEmailSendingStrategy): introduce a value object instead of passing many arguments.
Related to #935 No functional changes.
1 parent abce896 commit 1505ce1

File tree

4 files changed

+75
-50
lines changed

4 files changed

+75
-50
lines changed

src/main/java/ru/mystamps/web/service/MailServiceImpl.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,13 @@ public void sendActivationKeyToUser(SendUsersActivationDto activation) {
7575
Validate.isTrue(activation.getLang() != null, "Language must be non null");
7676
Validate.isTrue(activation.getActivationKey() != null, "Activation key must be non null");
7777

78-
mailer.send(
79-
activation.getEmail(),
80-
robotEmail,
81-
"My Stamps",
82-
getSubjectOfActivationMail(activation),
83-
getTextOfActivationMail(activation),
84-
"activation_key",
85-
testMode
86-
);
78+
MailgunEmail email = prepareEmail()
79+
.recipientAddress(activation.getEmail())
80+
.subject(getSubjectOfActivationMail(activation))
81+
.text(getTextOfActivationMail(activation))
82+
.tag("activation_key");
83+
84+
mailer.send(email);
8785

8886
LOG.info(
8987
"Email with activation code has been sent to {} (lang: {})",
@@ -95,15 +93,14 @@ public void sendActivationKeyToUser(SendUsersActivationDto activation) {
9593
@Override
9694
@Async
9795
public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
98-
mailer.send(
99-
adminEmail,
100-
robotEmail,
101-
"My Stamps",
102-
getSubjectOfDailyStatisticsMail(report),
103-
reportService.prepareDailyStatistics(report),
104-
"daily_statistics",
105-
testMode
106-
);
96+
97+
MailgunEmail email = prepareEmail()
98+
.recipientAddress(adminEmail)
99+
.subject(getSubjectOfDailyStatisticsMail(report))
100+
.text(reportService.prepareDailyStatistics(report))
101+
.tag("daily_statistics");
102+
103+
mailer.send(email);
107104

108105
String date = shortDatePrinter.format(report.getStartDate());
109106

@@ -115,6 +112,13 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
115112
);
116113
}
117114

115+
private MailgunEmail prepareEmail() {
116+
return new MailgunEmail()
117+
.senderAddress(robotEmail)
118+
.senderName("My Stamps")
119+
.testMode(testMode);
120+
}
121+
118122
private String getTextOfActivationMail(SendUsersActivationDto activation) {
119123
String template = messageSource.getMessage("activation.text", null, activation.getLocale());
120124

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (C) 2009-2019 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
import lombok.experimental.Accessors;
23+
24+
@Accessors(fluent = true)
25+
@Getter
26+
@Setter
27+
class MailgunEmail {
28+
private String recipientAddress;
29+
private String senderAddress;
30+
private String senderName;
31+
private String subject;
32+
private String text;
33+
34+
// There fields are fully Mailgun-specific.
35+
//
36+
// a tag for messages categorization
37+
private String tag;
38+
// tells Mailgun to accept a message but don't send it
39+
private boolean testMode;
40+
}

src/main/java/ru/mystamps/web/service/MailgunEmailSendingStrategy.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,11 @@ public interface MailgunEmailSendingStrategy {
2727
/**
2828
* Send an e-mail.
2929
*
30-
* @param toEmail recipient address
31-
* @param fromEmail sender address
32-
* @param fromName sender name
33-
* @param subject mail subject
34-
* @param text mail body
35-
* @param tag a tag for messages categorization in Mailgun
36-
* @param testMode tells Mailgun to accept a message but don't send it
30+
* @param email data and meta-data for sending an e-mail
3731
* @throws ru.mystamps.web.service.exception.EmailSendingException when any error occurs
3832
* @see SmtpMailgunEmailSendingStrategy
3933
* @see <a href="https://documentation.mailgun.com/en/latest/user_manual.html#tagging">Tagging</a>
4034
* @see <a href="https://documentation.mailgun.com/en/latest/user_manual.html#sending-in-test-mode">Sending in Test Mode</a>
4135
*/
42-
@SuppressWarnings("PMD.UseObjectForClearerAPI")
43-
void send(
44-
String toEmail,
45-
String fromEmail,
46-
String fromName,
47-
String subject,
48-
String text,
49-
String tag,
50-
boolean testMode);
36+
void send(MailgunEmail email);
5137
}

src/main/java/ru/mystamps/web/service/SmtpMailgunEmailSendingStrategy.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,31 +39,26 @@ public class SmtpMailgunEmailSendingStrategy implements MailgunEmailSendingStrat
3939
private final JavaMailSender mailer;
4040

4141
@Override
42-
@SuppressWarnings("PMD.UseObjectForClearerAPI")
43-
public void send(
44-
String toEmail,
45-
String fromEmail,
46-
String fromName,
47-
String subject,
48-
String text,
49-
String tag,
50-
boolean testMode) {
42+
public void send(MailgunEmail email) {
5143

5244
try {
5345
// We're using MimeMessagePreparator only because of its capability of adding headers.
5446
// Otherwise we would use SimpleMailMessage class.
5547
MimeMessagePreparator preparator = new MimeMessagePreparator() {
5648
@Override
5749
public void prepare(MimeMessage mimeMessage) throws Exception {
50+
InternetAddress from =
51+
new InternetAddress(email.senderAddress(), email.senderName(), "UTF-8");
52+
5853
MimeMessageHelper message = new MimeMessageHelper(mimeMessage, "UTF-8");
5954
message.setValidateAddresses(true);
60-
message.setTo(toEmail);
61-
message.setFrom(new InternetAddress(fromEmail, fromName, "UTF-8"));
62-
message.setSubject(subject);
63-
message.setText(text);
55+
message.setTo(email.recipientAddress());
56+
message.setFrom(from);
57+
message.setSubject(email.subject());
58+
message.setText(email.text());
6459

65-
message.getMimeMessage().addHeader("X-Mailgun-Tag", tag);
66-
if (testMode) {
60+
message.getMimeMessage().addHeader("X-Mailgun-Tag", email.tag());
61+
if (email.testMode()) {
6762
message.getMimeMessage().addHeader("X-Mailgun-Drop-Message", "yes");
6863
}
6964
}
@@ -72,7 +67,7 @@ public void prepare(MimeMessage mimeMessage) throws Exception {
7267
mailer.send(preparator);
7368

7469
} catch (MailException ex) {
75-
throw new EmailSendingException("Can't send mail to e-mail " + toEmail, ex);
70+
throw new EmailSendingException("Can't send mail to " + email.recipientAddress(), ex);
7671
}
7772
}
7873

0 commit comments

Comments
 (0)