Skip to content

Commit d918e76

Browse files
SRP => BadPractice
1 parent 2db6900 commit d918e76

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class TemplatingEngineInterface {
2+
render(template : string , params : Array<string>) : string {
3+
return `
4+
<div>
5+
${params.join(" </br> ")}
6+
</div>
7+
`
8+
}
9+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { MailerInterface } from "./mailerInterface";
2+
import { TemplatingEngineInterface } from "./TemplatingEngineInterface";
3+
import { TranslatorInterface } from "./translatorInterface";
4+
5+
export class User {
6+
public getConfirmCode() : string {
7+
return Math.floor(Math.random() * 10).toString()
8+
}
9+
10+
public getEmailAddress() : string {
11+
return "Mahdi@gmail.com"
12+
}
13+
}
14+
export class Message {
15+
private subject:string;
16+
private body:string;
17+
private emailAddress:string;
18+
19+
constructor(
20+
subject,
21+
body,
22+
emailAddress
23+
){
24+
this.subject = subject;
25+
this.body = body;
26+
this.emailAddress = emailAddress;
27+
}
28+
}
29+
30+
class ConfirmationMailMailer {
31+
private templating : TemplatingEngineInterface;
32+
private translator : TranslatorInterface;
33+
private mailer : MailerInterface;
34+
35+
constructor(
36+
templating : TemplatingEngineInterface,
37+
translator : TranslatorInterface,
38+
mailer : MailerInterface
39+
){
40+
this.templating = templating;
41+
this.translator = translator;
42+
this.mailer = mailer;
43+
}
44+
45+
private createMesssageFor(user : User) : Message {
46+
const subject = this.translator.translate("Please Confirm Your Email Address");
47+
const body = this.templating.render("mail.confirm" , [
48+
"Hello Sir." ,
49+
"Thanks For Your Signup !",
50+
`Your Code is ${user.getConfirmCode()}`,
51+
"Regards♥"]);
52+
return new Message(subject,body,user.getEmailAddress())
53+
}
54+
55+
public sendTo(user : User){
56+
const message = this.createMesssageFor(user);
57+
this.sendMessage(message);
58+
}
59+
60+
public sendMessage(message : Message) {
61+
this.mailer.send(message)
62+
}
63+
64+
}

solid/srp/mailerInterface.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Message } from "./confirmationMailMailer";
2+
3+
export class MailerInterface {
4+
send(message : Message){
5+
6+
}
7+
}

solid/srp/translatorInterface.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export class TranslatorInterface {
2+
private translate(text : string) {
3+
return text;
4+
};
5+
}

0 commit comments

Comments
 (0)