|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProjetNormandie\UserBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use ProjetNormandie\UserBundle\Event\EmailChangedEvent; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
|
9
|
|
|
use Symfony\Component\Mime\Email; |
|
10
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class EmailChangeSubscriber implements EventSubscriberInterface |
|
13
|
|
|
{ |
|
14
|
|
|
public function __construct( |
|
15
|
|
|
private readonly LoggerInterface $logger, |
|
16
|
|
|
private readonly MailerInterface $mailer, |
|
17
|
|
|
private readonly TranslatorInterface $translator |
|
18
|
|
|
) { |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @return array<string, array<int, array<int, int|string>>> |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function getSubscribedEvents(): array |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
EmailChangedEvent::class => [ |
|
28
|
|
|
['logEmailChange', 100], |
|
29
|
|
|
['notifyUser', 90], |
|
30
|
|
|
], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function logEmailChange(EmailChangedEvent $event): void |
|
35
|
|
|
{ |
|
36
|
|
|
$user = $event->getUser(); |
|
37
|
|
|
$oldEmail = $event->getOldEmail(); |
|
38
|
|
|
$newEmail = $event->getNewEmail(); |
|
39
|
|
|
|
|
40
|
|
|
$this->logger->info( |
|
41
|
|
|
sprintf( |
|
42
|
|
|
'##EMAIL_CHANGED##[user=%s/id=%d/old=%s/new=%s]', |
|
43
|
|
|
$user->getUsername(), |
|
44
|
|
|
$user->getId(), |
|
45
|
|
|
$oldEmail, |
|
46
|
|
|
$newEmail |
|
47
|
|
|
), |
|
48
|
|
|
[ |
|
49
|
|
|
'user_id' => $user->getId(), |
|
50
|
|
|
'username' => $user->getUsername(), |
|
51
|
|
|
'old_email' => $oldEmail, |
|
52
|
|
|
'new_email' => $newEmail, |
|
53
|
|
|
'action' => 'email_change' |
|
54
|
|
|
] |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function notifyUser(EmailChangedEvent $event): void |
|
59
|
|
|
{ |
|
60
|
|
|
$user = $event->getUser(); |
|
61
|
|
|
$oldEmail = $event->getOldEmail(); |
|
62
|
|
|
$newEmail = $event->getNewEmail(); |
|
63
|
|
|
|
|
64
|
|
|
$this->logger->debug('Sending email change notifications', [ |
|
65
|
|
|
'user_id' => $user->getId(), |
|
66
|
|
|
'username' => $user->getUsername() |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
try { |
|
70
|
|
|
$locale = $user->getLanguage(); |
|
71
|
|
|
|
|
72
|
|
|
// Email to old email |
|
73
|
|
|
$oldEmailBody = $this->translator->trans( |
|
74
|
|
|
'email_change.notification_old_email', |
|
75
|
|
|
[ |
|
76
|
|
|
'%username%' => $user->getUsername(), |
|
77
|
|
|
'%old_email%' => $oldEmail, |
|
78
|
|
|
'%new_email%' => $newEmail |
|
79
|
|
|
], |
|
80
|
|
|
'email', |
|
81
|
|
|
$locale |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
$emailToOld = (new Email()) |
|
85
|
|
|
->to($oldEmail) |
|
86
|
|
|
->subject($this->translator->trans('email_change.subject', [], 'email', $locale)) |
|
87
|
|
|
->text($oldEmailBody) |
|
88
|
|
|
->html($oldEmailBody); |
|
89
|
|
|
|
|
90
|
|
|
$this->mailer->send($emailToOld); |
|
91
|
|
|
|
|
92
|
|
|
// Email to new email |
|
93
|
|
|
$newEmailBody = $this->translator->trans( |
|
94
|
|
|
'email_change.notification_new_email', |
|
95
|
|
|
[ |
|
96
|
|
|
'%username%' => $user->getUsername(), |
|
97
|
|
|
'%old_email%' => $oldEmail, |
|
98
|
|
|
'%new_email%' => $newEmail |
|
99
|
|
|
], |
|
100
|
|
|
'email', |
|
101
|
|
|
$locale |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
|
|
$emailToNew = (new Email()) |
|
105
|
|
|
->to($newEmail) |
|
106
|
|
|
->subject($this->translator->trans('email_change.subject', [], 'email', $locale)) |
|
107
|
|
|
->text($newEmailBody) |
|
108
|
|
|
->html($newEmailBody); |
|
109
|
|
|
|
|
110
|
|
|
$this->mailer->send($emailToNew); |
|
111
|
|
|
|
|
112
|
|
|
$this->logger->info('Email change notifications sent successfully', [ |
|
113
|
|
|
'user_id' => $user->getId(), |
|
114
|
|
|
'username' => $user->getUsername() |
|
115
|
|
|
]); |
|
116
|
|
|
} catch (\Exception $e) { |
|
117
|
|
|
$this->logger->error('Error sending email change notifications', [ |
|
118
|
|
|
'user_id' => $user->getId(), |
|
119
|
|
|
'username' => $user->getUsername(), |
|
120
|
|
|
'error' => $e->getMessage(), |
|
121
|
|
|
'trace' => $e->getTraceAsString() |
|
122
|
|
|
]); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|