1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Users\Service; |
6
|
|
|
|
7
|
|
|
use App\Movies\DTO\ReleaseDateNotificationDTO; |
8
|
|
|
use App\Users\Entity\User; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
12
|
|
|
|
13
|
|
|
class SendEmailService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var TranslatorInterface |
17
|
|
|
*/ |
18
|
|
|
private $translator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Swift_Mailer |
22
|
|
|
*/ |
23
|
|
|
private $mailer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Twig_Environment |
27
|
|
|
*/ |
28
|
|
|
private $twig; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ConfirmationTokenService |
32
|
|
|
*/ |
33
|
|
|
private $confirmationTokenService; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var LoggerInterface|NullLogger |
37
|
|
|
*/ |
38
|
|
|
private $logger; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* From email. |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $supportEmail; |
46
|
|
|
|
47
|
5 |
|
public function __construct(TranslatorInterface $translator, \Swift_Mailer $mailer, \Twig_Environment $twig, ConfirmationTokenService $confirmationTokenService, LoggerInterface $logger) |
48
|
|
|
{ |
49
|
5 |
|
$this->translator = $translator; |
50
|
5 |
|
$this->mailer = $mailer; |
51
|
5 |
|
$this->twig = $twig; |
52
|
5 |
|
$this->confirmationTokenService = $confirmationTokenService; |
53
|
5 |
|
$this->supportEmail = \getenv('MAILER_SUPPORT_EMAIL') ?: '[email protected]'; |
54
|
5 |
|
$this->logger = $logger ?? new NullLogger(); |
55
|
5 |
|
} |
56
|
|
|
|
57
|
|
|
// todo translations for movie + for email text |
58
|
|
|
public function sendReleaseDateNotification(string $email, ReleaseDateNotificationDTO $data) |
59
|
|
|
{ |
60
|
|
|
$body = $this->twig->render( |
61
|
|
|
'emails/releaseDateNotification.html.twig', |
62
|
|
|
(array) $data |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$subject = $this->translator->trans('release_date_notification_email_subject', [ |
66
|
|
|
'{movieTitle}' => $data->movieTitle, |
67
|
|
|
'{countryName}' => $data->countryName, |
68
|
|
|
], 'users'); |
69
|
|
|
|
70
|
|
|
$this->sendEmail($email, $subject, $body); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function sendEmailConfirmation(User $user) |
74
|
|
|
{ |
75
|
2 |
|
$emailConfirmationToken = $this->confirmationTokenService->getEmailConfirmationToken($user)->getToken(); |
76
|
|
|
|
77
|
2 |
|
$body = $this->twig->render( |
78
|
2 |
|
'emails/confirmEmail.html.twig', |
79
|
2 |
|
['token' => $emailConfirmationToken] |
80
|
|
|
); |
81
|
|
|
|
82
|
2 |
|
$subject = $this->translator->trans('user_registration_email_subject', [ |
83
|
2 |
|
'{appName}' => \getenv('APP_NAME'), |
84
|
2 |
|
], 'users'); |
85
|
|
|
|
86
|
2 |
|
$this->sendEmail($user->getEmail(), $subject, $body); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
1 |
|
public function sendPasswordRecoveryConfirmation(User $user) |
90
|
|
|
{ |
91
|
1 |
|
$passwordRecoveryToken = $this->confirmationTokenService->getPasswordRecoveryToken($user)->getToken(); |
92
|
|
|
|
93
|
1 |
|
$body = $this->twig->render( |
94
|
1 |
|
'emails/passwordRecovery.html.twig', |
95
|
1 |
|
['token' => $passwordRecoveryToken] |
96
|
|
|
); |
97
|
|
|
|
98
|
1 |
|
$subject = $this->translator->trans('user_password_recovery_email_subject', [], 'users'); |
99
|
|
|
|
100
|
1 |
|
$this->sendEmail($user->getEmail(), $subject, $body); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
3 |
|
private function sendEmail($recipientEmail, string $subject, string $body) |
104
|
|
|
{ |
105
|
3 |
|
$this->logger->info("[MAILER] Trying to send email to {$recipientEmail} with next params:", [ |
106
|
3 |
|
'subject' => $subject, |
107
|
3 |
|
'body' => $body, |
108
|
|
|
]); |
109
|
3 |
|
$message = (new \Swift_Message($subject)) |
110
|
3 |
|
->setFrom($this->supportEmail) |
111
|
3 |
|
->setTo($recipientEmail) |
112
|
3 |
|
->setBody($body, 'text/html'); |
113
|
|
|
|
114
|
|
|
// todo need to retry if any failed recipients found ($failedRecipients is array of emails) |
115
|
3 |
|
$failedRecipients = []; |
116
|
3 |
|
$this->mailer->send($message, $failedRecipients); |
117
|
|
|
|
118
|
3 |
|
if (\count($failedRecipients)) { |
119
|
|
|
$this->logger->warning('[MAILER] Some of mails not sent, list of all recipients: ', $failedRecipients); |
|
|
|
|
120
|
|
|
} |
121
|
3 |
|
} |
122
|
|
|
} |
123
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.