src/EventSubscriber/User/UserSubscriber.php line 250

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\User;
  3. use App\Entity\DonneeCompteClient;
  4. use App\Entity\User;
  5. use App\Event\Account\UserAlertFinalizedEvent;
  6. use App\Event\Account\UserCreatedEvent;
  7. use App\Event\Account\UserCreationFinalizedEvent;
  8. use App\Event\Account\UserDeletedEvent;
  9. use App\Event\Account\UserMailChangedEvent;
  10. use App\Event\Account\UserMailChangeFinalizedEvent;
  11. use App\Event\Account\UserPasswordChangedEvent;
  12. use App\Event\Account\UserPasswordWantedEvent;
  13. use App\Event\MyProfile\BankInformationUpdatedEvent;
  14. use App\Event\MyProfile\CustomerDetailsUpdatedEvent;
  15. use App\Event\MyProfile\PersonalInformationEmailUpdatedEvent;
  16. use App\Event\MyProfile\PersonalInformationEmailUpdateEvent;
  17. use App\Event\MyProfile\PersonalInformationPasswordUpdatedEvent;
  18. use App\Event\MyProfile\PersonalInformationUpdatedEvent;
  19. use App\EventSubscriber\AbstractEmailSubscriber;
  20. use App\Manager\DonneeCompteClientManager;
  21. use App\Manager\Mailer;
  22. use App\Manager\MailTypeInterface;
  23. use App\Repository\App\NotificationStatisticsRepository;
  24. use App\ScpExport\ScpExporter;
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use Symfony\Contracts\Translation\TranslatorInterface;
  27. use Twig\Environment;
  28. class UserSubscriber extends AbstractEmailSubscriber
  29. {
  30.     private $notificationStatisticsRepository;
  31.     private $entityManager;
  32.     private DonneeCompteClientManager $donneeCompteClientManager;
  33.     public function __construct(
  34.         NotificationStatisticsRepository $notificationStatisticsRepository,
  35.         EntityManagerInterface           $entityManager,
  36.         Mailer                           $mailer,
  37.         TranslatorInterface              $translator,
  38.         Environment                      $templating,
  39.         DonneeCompteClientManager        $donneeCompteClientManager
  40.     ) {
  41.         $this->notificationStatisticsRepository $notificationStatisticsRepository;
  42.         $this->entityManager $entityManager;
  43.         parent::__construct($mailer$templating$translator);
  44.         $this->donneeCompteClientManager $donneeCompteClientManager;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             UserCreatedEvent::class => 'onUserCreated',
  50.             UserCreationFinalizedEvent::class => 'onUserCreationFinalized',
  51.             UserAlertFinalizedEvent::class => 'onAlertUserFinalized',
  52.             UserMailChangedEvent::class => 'onUserMailChanged',
  53.             UserMailChangeFinalizedEvent::class => 'onUserMailChangeFinalized',
  54.             UserPasswordWantedEvent::class => 'onUserPasswordWanted',
  55.             UserPasswordChangedEvent::class => 'onUserPasswordChanged',
  56.             PersonalInformationEmailUpdateEvent::class => 'onPersonalInformationEmailUpdate',
  57.             PersonalInformationEmailUpdatedEvent::class => 'onPersonalInformationEmailUpdated',
  58.             PersonalInformationPasswordUpdatedEvent::class => 'onPersonalInformationPasswordUpdated',
  59.             PersonalInformationUpdatedEvent::class => 'onPersonalInformationUpdated',
  60.             BankInformationUpdatedEvent::class => 'onBankInformationUpdated',
  61.             CustomerDetailsUpdatedEvent::class => 'onCustomerDetailsUpdated',
  62.             UserDeletedEvent::class => 'onUserDeleted',
  63.         ];
  64.     }
  65.     public function onUserCreated(UserCreatedEvent $event): void
  66.     {
  67.         $user $event->getUser();
  68.         // Update activation status, email and commercial consent in customer data
  69.         $this->donneeCompteClientManager->updateDataOnUserEvent(
  70.             $user->getCustomerNumber(),
  71.             DonneeCompteClient::ACTIVATION_PENDING,
  72.             $user->getEmail(),
  73.             false
  74.         );
  75.         $subject $this->transEmail('account.creation.validation.subject');
  76.         $message $this->templating->render('email/account/creation.html.twig', [
  77.             'customer' => $user,
  78.         ]);
  79.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_USER_CREATED);
  80.     }
  81.     protected function sendEmailAndExport(User $userstring $subjectstring $bodystring $mailType): void
  82.     {
  83.         if ($mailType == MailTypeInterface::ACCOUNT_EMAIL_UPDATE) {
  84.             $this->mailer->sendMail([$user->getNewEmail()], $subject$body$mailType);
  85.         } else {
  86.             $this->mailer->sendMail([$user->getEmail()], $subject$body$mailType);
  87.         }
  88.     }
  89.     public function onUserCreationFinalized(UserCreationFinalizedEvent $event): void
  90.     {
  91.         $user $event->getUser();
  92.         // Update the user account status in customer data
  93.         if ($user?->isActivated()) {
  94.             $this->donneeCompteClientManager->updateActivationCompte(
  95.                 $user->getCustomerNumber(),
  96.                 DonneeCompteClient::ACTIVATION_ACTIVE
  97.             );
  98.         }
  99.         // Update the user commercial consent in customer data
  100.         $this->donneeCompteClientManager->updateComConsent(
  101.             $user->getCustomerNumber(),
  102.             $user->hasNewsletter()
  103.         );
  104.         $subject $this->transEmail('account.creation.validation_confirmation.subject');
  105.         $message $this->templating->render('email/account/validation.html.twig', [
  106.             'customer' => $user,
  107.         ]);
  108.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_USER_FINALIZED);
  109.         $this->__retrievePendingNotificationsAndNotify($user);
  110.     }
  111.     private function __retrievePendingNotificationsAndNotify($user): void
  112.     {
  113.         $statistics $this->notificationStatisticsRepository
  114.             ->findOngoingAndFutureNotificationStatisticsByCustomerNumber($user->getCustomerNumber());
  115.         // Send email if set in the notification parameters
  116.         foreach ($statistics as $statistic) {
  117.             $statistic->setUserId($user->getId());
  118.             $notification $statistic->getNotification();
  119.             if (
  120.                 $notification->getSendEmail() === true
  121.                 && $notification->isActive()
  122.                 && $notification->getPublished() === true
  123.             ) {
  124.                 $subject $this->transEmail('notification.new_alert.subject');
  125.                 $message $this->templating->render(
  126.                     'email/notification/new_notification_alert.html.twig',
  127.                     ['customer' => $user'notification' => $notification]
  128.                 );
  129.                 $emailAddress $statistic->getEmail() ?? $user->getEmail();
  130.                 $this->mailer->sendMail(
  131.                     [$emailAddress],
  132.                     $subject,
  133.                     $message,
  134.                     MailTypeInterface::NOTIFICATION_NEW_MESSAGE_EMAIL
  135.                 );
  136.                 $statistic->setEmailSentDate(new \DateTime());
  137.             }
  138.             $this->entityManager->flush();
  139.         }
  140.     }
  141.     public function onAlertUserFinalized(UserAlertFinalizedEvent $event): void
  142.     {
  143.         $user $event->getUser();
  144.         $interval $event->getInterval();
  145.         switch ($interval) {
  146.             case UserAlertFinalizedEvent::DAY20:
  147.                 $subject 'account.creation.alert_validation_confirmation.d20.subject';
  148.                 $template 'email/account/alert_validation_d20.html.twig';
  149.                 break;
  150.             case UserAlertFinalizedEvent::DAY10:
  151.                 $subject 'account.creation.alert_validation_confirmation.d10.subject';
  152.                 $template 'email/account/alert_validation_d10.html.twig';
  153.                 break;
  154.             case UserAlertFinalizedEvent::DAY3:
  155.             default:
  156.                 $subject 'account.creation.alert_validation_confirmation.d3.subject';
  157.                 $template 'email/account/alert_validation_d3.html.twig';
  158.                 break;
  159.         }
  160.         $subject $this->transEmail($subject);
  161.         $message $this->templating->render($template, [
  162.             'customer' => $user,
  163.         ]);
  164.         $this->sendEmailAndExport(
  165.             $user,
  166.             $subject,
  167.             $message,
  168.             MailTypeInterface::ACCOUNT_ALERT_USER_NOT_FINALIZED
  169.         );
  170.     }
  171.     public function onUserMailChanged(UserMailChangedEvent $event): void
  172.     {
  173.         $user $event->getUser();
  174.         $subject $this->transEmail('lost_email.update.subject');
  175.         $message $this->templating->render('email/account/lost_email_update.html.twig', [
  176.             'customer' => $user,
  177.         ]);
  178.         $this->sendEmailAndExport(
  179.             $user,
  180.             $subject,
  181.             $message,
  182.             MailTypeInterface::ACCOUNT_MAIL_CHANGE_INITIALIZED
  183.         );
  184.     }
  185.     public function onUserMailChangeFinalized(UserMailChangeFinalizedEvent $event): void
  186.     {
  187.         $user $event->getUser();
  188.         $subject $this->transEmail('lost_email.confirmation.subject');
  189.         $message $this->templating->render('email/account/lost_email_validation.html.twig', [
  190.             'customer' => $user,
  191.         ]);
  192.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_MAIL_CHANGE_FINALIZED);
  193.     }
  194.     public function onUserPasswordWanted(UserPasswordWantedEvent $event): void
  195.     {
  196.         $user $event->getUser();
  197.         $subject $this->transEmail('lost_password.recovery.subject');
  198.         $message $this->templating->render('email/account/lost_password_recovery.html.twig', [
  199.             'customer' => $user,
  200.         ]);
  201.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_PASSWORD_RECOVERY);
  202.     }
  203.     public function onUserPasswordChanged(UserPasswordChangedEvent $event): void
  204.     {
  205.         $user $event->getUser();
  206.         $subject $this->transEmail('lost_password.confirmation.subject');
  207.         $message $this->templating->render('email/account/lost_password_validation.html.twig', [
  208.             'customer' => $user,
  209.         ]);
  210.         $this->sendEmailAndExport(
  211.             $user,
  212.             $subject,
  213.             $message,
  214.             MailTypeInterface::ACCOUNT_PASSWORD_RECOVERY_UPDATED
  215.         );
  216.     }
  217.     public function onPersonalInformationEmailUpdate(PersonalInformationEmailUpdateEvent $event): void
  218.     {
  219.         $user $event->getUser();
  220.         $subject $this->transEmail('my_profile.update.email.subject');
  221.         $message $this->templating->render('email/account/update_email.html.twig', [
  222.             'customer' => $user,
  223.         ]);
  224.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_EMAIL_UPDATE);
  225.     }
  226.     public function onPersonalInformationEmailUpdated(PersonalInformationEmailUpdatedEvent $event): void
  227.     {
  228.         $user $event->getUser();
  229.         $subject $this->transEmail('my_profile.update.information.subject');
  230.         $message $this->templating->render('email/account/update_email_confirmed.html.twig', [
  231.             'customer' => $user,
  232.         ]);
  233.         // Update the user email address in customer data
  234.         if ($user->getEmail() !== null) {
  235.             $this->donneeCompteClientManager->updateEmail(
  236.                 $user->getCustomerNumber(),
  237.                 $user->getEmail()
  238.             );
  239.         }
  240.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_EMAIL_UPDATED);
  241.     }
  242.     public function onPersonalInformationPasswordUpdated(PersonalInformationPasswordUpdatedEvent $event): void
  243.     {
  244.         $user $event->getUser();
  245.         $subject $this->transEmail('my_profile.update.password.subject');
  246.         $message $this->templating->render('email/account/update_password.html.twig', [
  247.             'customer' => $user,
  248.         ]);
  249.         $this->sendEmailAndExport($user$subject$messageMailTypeInterface::ACCOUNT_PASSWORD_UPDATED);
  250.     }
  251.     public function onPersonalInformationUpdated(PersonalInformationUpdatedEvent $event): void
  252.     {
  253.         $user $event->getUser();
  254.         $subject $this->transEmail('my_profile.update.information.subject');
  255.         $message $this->templating->render('email/account/update_personal_information.html.twig', [
  256.             'customer' => $user,
  257.         ]);
  258.         // Update the user commercial consent in customer data
  259.         $this->donneeCompteClientManager->updateComConsent(
  260.             $user->getCustomerNumber(),
  261.             $user->hasNewsletter()
  262.         );
  263.         $this->sendEmailAndExport(
  264.             $user,
  265.             $subject,
  266.             $message,
  267.             MailTypeInterface::ACCOUNT_PERSONAL_INFORMATION_UPDATED
  268.         );
  269.     }
  270.     public function onCustomerDetailsUpdatedCustomerDetailsUpdatedEvent $event): void
  271.     {
  272.         $user $event->getUser();
  273.         $subject $this->transEmail('my_profile.update.customer_details.subject');
  274.         $message $this->templating->render('email/account/update_customer_details.html.twig', [
  275.             'customer' => $user,
  276.             'donneeCompteClient' => $this->donneeCompteClientManager->getDonneeCompteClientByNumeroClient(
  277.                 $user->getCustomerNumber()
  278.             )
  279.         ]);
  280.         $this->sendEmailAndExport(
  281.             $user,
  282.             $subject,
  283.             $message,
  284.             MailTypeInterface::ACCOUNT_CUSTOMER_DETAIL_UPDATED
  285.         );
  286.     }
  287.     public function onBankInformationUpdated(BankInformationUpdatedEvent $event): void
  288.     {
  289.         $user $event->getUser();
  290.         $subject $this->transEmail('my_profile.update.bank_information.subject');
  291.         $message $this->templating->render('email/account/update_bank_information.html.twig', [
  292.             'customer' => $user,
  293.         ]);
  294.         $this->sendEmailAndExport(
  295.             $user,
  296.             $subject,
  297.             $message,
  298.             MailTypeInterface::ACCOUNT_BANK_INFORMATION_UPDATED
  299.         );
  300.     }
  301.     public function onUserDeleted(UserDeletedEvent $event): void
  302.     {
  303.         $user $event->getUser();
  304.         if (null === $user) {
  305.             return;
  306.         }
  307.         // Update the user account status in customer data
  308.         $this->donneeCompteClientManager->updateDataOnUserEvent(
  309.             $user->getCustomerNumber(),
  310.             DonneeCompteClient::ACTIVATION_INACTIVE,
  311.             null,
  312.             false
  313.         );
  314.         // Send emails
  315.         $notifyMain $event->getOptions()['notifyMain'] ?? false;
  316.         $notifyGuests $event->getOptions()['notifyGuests'] ?? false;
  317.         if ($user->isMainAccount() && 'true' == $notifyMain) {
  318.             $this->mailer->sendMail(
  319.                 [$user->getEmail()],
  320.                 $this->transEmail('account.removal.main.subject'),
  321.                 $this->templating->render('email/account/removal/main.html.twig', ['customer' => $user]),
  322.                 MailTypeInterface::ACCOUNT_USER_DELETED
  323.             );
  324.         } elseif ($user->isGuest() && 'true' == $notifyGuests) {
  325.             $this->mailer->sendMail(
  326.                 [$user->getEmail()],
  327.                 $this->transEmail('account.removal.guest.subject'),
  328.                 $this->templating->render('email/account/removal/guest.html.twig', ['customer' => $user]),
  329.                 MailTypeInterface::ACCOUNT_GUEST_DELETED
  330.             );
  331.         }
  332.     }
  333. }