1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Component Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentBundle\Mailer; |
15
|
|
|
|
16
|
|
|
use Silverback\ApiComponentBundle\Entity\User\AbstractUser; |
17
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidParameterException; |
18
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
19
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
20
|
|
|
use Symfony\Component\Mime\Address; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Daniel West <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class UserMailer |
26
|
|
|
{ |
27
|
|
|
private MailerInterface $mailer; |
28
|
|
|
private string $websiteName; |
29
|
|
|
private bool $sendUserWelcomeEmailEnabled; |
30
|
|
|
private bool $sendUserEnabledEmailEnabled; |
31
|
|
|
private bool $sendUserUsernameChangedEmailEnabled; |
32
|
|
|
private bool $sendUserPasswordChangedEmailEnabled; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
MailerInterface $mailer, |
36
|
|
|
string $websiteName, |
37
|
|
|
bool $sendUserWelcomeEmailEnabled = true, |
38
|
|
|
bool $sendUserEnabledEmailEnabled = true, |
39
|
|
|
bool $sendUserUsernameChangedEmailEnabled = true, |
40
|
|
|
bool $sendUserPasswordChangedEmailEnabled = true) |
41
|
|
|
{ |
42
|
|
|
$this->mailer = $mailer; |
43
|
|
|
$this->websiteName = $websiteName; |
44
|
|
|
$this->sendUserWelcomeEmailEnabled = $sendUserWelcomeEmailEnabled; |
45
|
|
|
$this->sendUserEnabledEmailEnabled = $sendUserEnabledEmailEnabled; |
46
|
|
|
$this->sendUserUsernameChangedEmailEnabled = $sendUserUsernameChangedEmailEnabled; |
47
|
|
|
$this->sendUserPasswordChangedEmailEnabled = $sendUserPasswordChangedEmailEnabled; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function sendPasswordResetEmail(AbstractUser $user, string $resetUrl): void |
51
|
|
|
{ |
52
|
|
|
$userEmail = $this->getUserEmail($user); |
53
|
|
|
$email = (new TemplatedEmail()) |
54
|
|
|
->to(Address::fromString($userEmail)) |
55
|
|
|
->subject('Your password reset request') |
56
|
|
|
->htmlTemplate('@SilverbackApiComponent/emails/forgot_password.html.twig') |
57
|
|
|
->context([ |
58
|
|
|
'user' => $user, |
59
|
|
|
'reset_url' => $resetUrl, |
60
|
|
|
'website_name' => $this->websiteName, |
61
|
|
|
]); |
62
|
|
|
$this->mailer->send($email); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function sendChangeEmailConfirmationEmail(AbstractUser $user): void |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function sendUserWelcomeEmail(AbstractUser $user): void |
70
|
|
|
{ |
71
|
|
|
if (!$this->sendUserWelcomeEmailEnabled) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
$userEmail = $this->getUserEmail($user); |
75
|
|
|
$email = (new TemplatedEmail()) |
76
|
|
|
->to(Address::fromString($userEmail)) |
77
|
|
|
->subject(sprintf('Welcome to %s', $this->websiteName)) |
78
|
|
|
->htmlTemplate('@SilverbackApiComponent/emails/user_welcome.html.twig') |
79
|
|
|
->context([ |
80
|
|
|
'user' => $user, |
81
|
|
|
'website_name' => $this->websiteName, |
82
|
|
|
]); |
83
|
|
|
$this->mailer->send($email); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function sendUserEnabledEmail(AbstractUser $user): void |
87
|
|
|
{ |
88
|
|
|
if (!$this->sendUserEnabledEmailEnabled) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
$userEmail = $this->getUserEmail($user); |
92
|
|
|
$email = (new TemplatedEmail()) |
93
|
|
|
->to(Address::fromString($userEmail)) |
94
|
|
|
->subject('Your account has been enabled') |
95
|
|
|
->htmlTemplate('@SilverbackApiComponent/emails/user_enabled.html.twig') |
96
|
|
|
->context([ |
97
|
|
|
'user' => $user, |
98
|
|
|
'website_name' => $this->websiteName, |
99
|
|
|
]); |
100
|
|
|
$this->mailer->send($email); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function sendUsernameChangedEmail(AbstractUser $user): void |
104
|
|
|
{ |
105
|
|
|
if (!$this->sendUserUsernameChangedEmailEnabled) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
$userEmail = $this->getUserEmail($user); |
109
|
|
|
$email = (new TemplatedEmail()) |
110
|
|
|
->to(Address::fromString($userEmail)) |
111
|
|
|
->subject('Your username has been changed') |
112
|
|
|
->htmlTemplate('@SilverbackApiComponent/emails/username_changed.html.twig') |
113
|
|
|
->context([ |
114
|
|
|
'user' => $user, |
115
|
|
|
'website_name' => $this->websiteName, |
116
|
|
|
]); |
117
|
|
|
$this->mailer->send($email); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function sendPasswordChangedEmail(AbstractUser $user): void |
121
|
|
|
{ |
122
|
|
|
if (!$this->sendUserPasswordChangedEmailEnabled) { |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
$userEmail = $this->getUserEmail($user); |
126
|
|
|
$email = (new TemplatedEmail()) |
127
|
|
|
->to(Address::fromString($userEmail)) |
128
|
|
|
->subject('Your password has been changed') |
129
|
|
|
->htmlTemplate('@SilverbackApiComponent/emails/user_password_changed.html.twig') |
130
|
|
|
->context([ |
131
|
|
|
'user' => $user, |
132
|
|
|
'website_name' => $this->websiteName, |
133
|
|
|
]); |
134
|
|
|
$this->mailer->send($email); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getUserEmail(AbstractUser $user): string |
138
|
|
|
{ |
139
|
|
|
if (!($userEmail = $user->getEmailAddress())) { |
140
|
|
|
throw new InvalidParameterException('The user must have an email address to send a password reset email'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $userEmail; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.