<?php
namespace App\EventSubscriber\User;
use App\Entity\DonneeCompteClient;
use App\Entity\User;
use App\Event\Account\UserAlertFinalizedEvent;
use App\Event\Account\UserCreatedEvent;
use App\Event\Account\UserCreationFinalizedEvent;
use App\Event\Account\UserDeletedEvent;
use App\Event\Account\UserMailChangedEvent;
use App\Event\Account\UserMailChangeFinalizedEvent;
use App\Event\Account\UserPasswordChangedEvent;
use App\Event\Account\UserPasswordWantedEvent;
use App\Event\MyProfile\BankInformationUpdatedEvent;
use App\Event\MyProfile\CustomerDetailsUpdatedEvent;
use App\Event\MyProfile\PersonalInformationEmailUpdatedEvent;
use App\Event\MyProfile\PersonalInformationEmailUpdateEvent;
use App\Event\MyProfile\PersonalInformationPasswordUpdatedEvent;
use App\Event\MyProfile\PersonalInformationUpdatedEvent;
use App\EventSubscriber\AbstractEmailSubscriber;
use App\Manager\DonneeCompteClientManager;
use App\Manager\Mailer;
use App\Manager\MailTypeInterface;
use App\Repository\App\NotificationStatisticsRepository;
use App\ScpExport\ScpExporter;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class UserSubscriber extends AbstractEmailSubscriber
{
private $notificationStatisticsRepository;
private $entityManager;
private DonneeCompteClientManager $donneeCompteClientManager;
public function __construct(
NotificationStatisticsRepository $notificationStatisticsRepository,
EntityManagerInterface $entityManager,
Mailer $mailer,
TranslatorInterface $translator,
Environment $templating,
DonneeCompteClientManager $donneeCompteClientManager
) {
$this->notificationStatisticsRepository = $notificationStatisticsRepository;
$this->entityManager = $entityManager;
parent::__construct($mailer, $templating, $translator);
$this->donneeCompteClientManager = $donneeCompteClientManager;
}
public static function getSubscribedEvents(): array
{
return [
UserCreatedEvent::class => 'onUserCreated',
UserCreationFinalizedEvent::class => 'onUserCreationFinalized',
UserAlertFinalizedEvent::class => 'onAlertUserFinalized',
UserMailChangedEvent::class => 'onUserMailChanged',
UserMailChangeFinalizedEvent::class => 'onUserMailChangeFinalized',
UserPasswordWantedEvent::class => 'onUserPasswordWanted',
UserPasswordChangedEvent::class => 'onUserPasswordChanged',
PersonalInformationEmailUpdateEvent::class => 'onPersonalInformationEmailUpdate',
PersonalInformationEmailUpdatedEvent::class => 'onPersonalInformationEmailUpdated',
PersonalInformationPasswordUpdatedEvent::class => 'onPersonalInformationPasswordUpdated',
PersonalInformationUpdatedEvent::class => 'onPersonalInformationUpdated',
BankInformationUpdatedEvent::class => 'onBankInformationUpdated',
CustomerDetailsUpdatedEvent::class => 'onCustomerDetailsUpdated',
UserDeletedEvent::class => 'onUserDeleted',
];
}
public function onUserCreated(UserCreatedEvent $event): void
{
$user = $event->getUser();
// Update activation status, email and commercial consent in customer data
$this->donneeCompteClientManager->updateDataOnUserEvent(
$user->getCustomerNumber(),
DonneeCompteClient::ACTIVATION_PENDING,
$user->getEmail(),
false
);
$subject = $this->transEmail('account.creation.validation.subject');
$message = $this->templating->render('email/account/creation.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_USER_CREATED);
}
protected function sendEmailAndExport(User $user, string $subject, string $body, string $mailType): void
{
if ($mailType == MailTypeInterface::ACCOUNT_EMAIL_UPDATE) {
$this->mailer->sendMail([$user->getNewEmail()], $subject, $body, $mailType);
} else {
$this->mailer->sendMail([$user->getEmail()], $subject, $body, $mailType);
}
}
public function onUserCreationFinalized(UserCreationFinalizedEvent $event): void
{
$user = $event->getUser();
// Update the user account status in customer data
if ($user?->isActivated()) {
$this->donneeCompteClientManager->updateActivationCompte(
$user->getCustomerNumber(),
DonneeCompteClient::ACTIVATION_ACTIVE
);
}
// Update the user commercial consent in customer data
$this->donneeCompteClientManager->updateComConsent(
$user->getCustomerNumber(),
$user->hasNewsletter()
);
$subject = $this->transEmail('account.creation.validation_confirmation.subject');
$message = $this->templating->render('email/account/validation.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_USER_FINALIZED);
$this->__retrievePendingNotificationsAndNotify($user);
}
private function __retrievePendingNotificationsAndNotify($user): void
{
$statistics = $this->notificationStatisticsRepository
->findOngoingAndFutureNotificationStatisticsByCustomerNumber($user->getCustomerNumber());
// Send email if set in the notification parameters
foreach ($statistics as $statistic) {
$statistic->setUserId($user->getId());
$notification = $statistic->getNotification();
if (
$notification->getSendEmail() === true
&& $notification->isActive()
&& $notification->getPublished() === true
) {
$subject = $this->transEmail('notification.new_alert.subject');
$message = $this->templating->render(
'email/notification/new_notification_alert.html.twig',
['customer' => $user, 'notification' => $notification]
);
$emailAddress = $statistic->getEmail() ?? $user->getEmail();
$this->mailer->sendMail(
[$emailAddress],
$subject,
$message,
MailTypeInterface::NOTIFICATION_NEW_MESSAGE_EMAIL
);
$statistic->setEmailSentDate(new \DateTime());
}
$this->entityManager->flush();
}
}
public function onAlertUserFinalized(UserAlertFinalizedEvent $event): void
{
$user = $event->getUser();
$interval = $event->getInterval();
switch ($interval) {
case UserAlertFinalizedEvent::DAY20:
$subject = 'account.creation.alert_validation_confirmation.d20.subject';
$template = 'email/account/alert_validation_d20.html.twig';
break;
case UserAlertFinalizedEvent::DAY10:
$subject = 'account.creation.alert_validation_confirmation.d10.subject';
$template = 'email/account/alert_validation_d10.html.twig';
break;
case UserAlertFinalizedEvent::DAY3:
default:
$subject = 'account.creation.alert_validation_confirmation.d3.subject';
$template = 'email/account/alert_validation_d3.html.twig';
break;
}
$subject = $this->transEmail($subject);
$message = $this->templating->render($template, [
'customer' => $user,
]);
$this->sendEmailAndExport(
$user,
$subject,
$message,
MailTypeInterface::ACCOUNT_ALERT_USER_NOT_FINALIZED
);
}
public function onUserMailChanged(UserMailChangedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('lost_email.update.subject');
$message = $this->templating->render('email/account/lost_email_update.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport(
$user,
$subject,
$message,
MailTypeInterface::ACCOUNT_MAIL_CHANGE_INITIALIZED
);
}
public function onUserMailChangeFinalized(UserMailChangeFinalizedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('lost_email.confirmation.subject');
$message = $this->templating->render('email/account/lost_email_validation.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_MAIL_CHANGE_FINALIZED);
}
public function onUserPasswordWanted(UserPasswordWantedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('lost_password.recovery.subject');
$message = $this->templating->render('email/account/lost_password_recovery.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_PASSWORD_RECOVERY);
}
public function onUserPasswordChanged(UserPasswordChangedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('lost_password.confirmation.subject');
$message = $this->templating->render('email/account/lost_password_validation.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport(
$user,
$subject,
$message,
MailTypeInterface::ACCOUNT_PASSWORD_RECOVERY_UPDATED
);
}
public function onPersonalInformationEmailUpdate(PersonalInformationEmailUpdateEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('my_profile.update.email.subject');
$message = $this->templating->render('email/account/update_email.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_EMAIL_UPDATE);
}
public function onPersonalInformationEmailUpdated(PersonalInformationEmailUpdatedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('my_profile.update.information.subject');
$message = $this->templating->render('email/account/update_email_confirmed.html.twig', [
'customer' => $user,
]);
// Update the user email address in customer data
if ($user->getEmail() !== null) {
$this->donneeCompteClientManager->updateEmail(
$user->getCustomerNumber(),
$user->getEmail()
);
}
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_EMAIL_UPDATED);
}
public function onPersonalInformationPasswordUpdated(PersonalInformationPasswordUpdatedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('my_profile.update.password.subject');
$message = $this->templating->render('email/account/update_password.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport($user, $subject, $message, MailTypeInterface::ACCOUNT_PASSWORD_UPDATED);
}
public function onPersonalInformationUpdated(PersonalInformationUpdatedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('my_profile.update.information.subject');
$message = $this->templating->render('email/account/update_personal_information.html.twig', [
'customer' => $user,
]);
// Update the user commercial consent in customer data
$this->donneeCompteClientManager->updateComConsent(
$user->getCustomerNumber(),
$user->hasNewsletter()
);
$this->sendEmailAndExport(
$user,
$subject,
$message,
MailTypeInterface::ACCOUNT_PERSONAL_INFORMATION_UPDATED
);
}
public function onCustomerDetailsUpdated( CustomerDetailsUpdatedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('my_profile.update.customer_details.subject');
$message = $this->templating->render('email/account/update_customer_details.html.twig', [
'customer' => $user,
'donneeCompteClient' => $this->donneeCompteClientManager->getDonneeCompteClientByNumeroClient(
$user->getCustomerNumber()
)
]);
$this->sendEmailAndExport(
$user,
$subject,
$message,
MailTypeInterface::ACCOUNT_CUSTOMER_DETAIL_UPDATED
);
}
public function onBankInformationUpdated(BankInformationUpdatedEvent $event): void
{
$user = $event->getUser();
$subject = $this->transEmail('my_profile.update.bank_information.subject');
$message = $this->templating->render('email/account/update_bank_information.html.twig', [
'customer' => $user,
]);
$this->sendEmailAndExport(
$user,
$subject,
$message,
MailTypeInterface::ACCOUNT_BANK_INFORMATION_UPDATED
);
}
public function onUserDeleted(UserDeletedEvent $event): void
{
$user = $event->getUser();
if (null === $user) {
return;
}
// Update the user account status in customer data
$this->donneeCompteClientManager->updateDataOnUserEvent(
$user->getCustomerNumber(),
DonneeCompteClient::ACTIVATION_INACTIVE,
null,
false
);
// Send emails
$notifyMain = $event->getOptions()['notifyMain'] ?? false;
$notifyGuests = $event->getOptions()['notifyGuests'] ?? false;
if ($user->isMainAccount() && 'true' == $notifyMain) {
$this->mailer->sendMail(
[$user->getEmail()],
$this->transEmail('account.removal.main.subject'),
$this->templating->render('email/account/removal/main.html.twig', ['customer' => $user]),
MailTypeInterface::ACCOUNT_USER_DELETED
);
} elseif ($user->isGuest() && 'true' == $notifyGuests) {
$this->mailer->sendMail(
[$user->getEmail()],
$this->transEmail('account.removal.guest.subject'),
$this->templating->render('email/account/removal/guest.html.twig', ['customer' => $user]),
MailTypeInterface::ACCOUNT_GUEST_DELETED
);
}
}
}