src/EventSubscriber/EmailSubscriber.php line 187

  1. use Doctrine\ORM\EntityManagerInterface;
  2. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  3. use Symfony\Component\Messenger\Exception\ExceptionInterface;
  4. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  5. readonly class EmailSubscriber implements EventSubscriberInterface
  6. {
  7. public function __construct(
  8. private EmailSender $emailSender,
  9. private ReviewHelper $reviewHelper,
  10. private EntityManagerInterface $entityManager,
  11. private NotificationEmailSender $notificationEmailSender,
  12. private UrlGeneratorInterface $urlGenerator
  13. ) {
  14. }
  15. /**
  16. * @return array
  17. */
  18. public static function getSubscribedEvents(): array
  19. {
  20. return [
  21. AppEvents::EMPLOYEE_CREATED => 'onNewEmployeeCreated',
  22. AppEvents::USER_RESET_PASSWORD => 'onUserResetPassword',
  23. AppEvents::USER_REGISTRATION_APPROVED => 'onUserRegistrationApproved',
  24. AppEvents::REVIEW_NOTIFY => 'onNotifyReview',
  25. AppEvents::REVIEW_NOTIFY_DENIED => 'onNotifyReviewDenied',
  26. AppEvents::UPDATE_USER_PASSWORD => 'onUpdateUserPassword',
  27. AppEvents::NOTIFIED_USERS_FOR_UPDATE_PASSWORD => 'onNotifiedUsersForUpdatePassword',
  28. ];
  29. }
  30. public function onUserResetPassword(UserResetPasswordEvent $event): void
  31. {
  32. $user = $event->getUser();
  33. $notificationType = $event->getType();
  34. $emailNotification = $this->getNotificationType($notificationType);
  35. $parameters = [];
  36. $parameters["##userName##"] = $user->getFullname();
  37. $parameters["##userMail##"] = $user->getEmail();
  38. $parameters["##resetPasswordLink##"] = $_ENV['MAIN_URL'].$this->urlGenerator->generate('app_change_password', ['code' => $user->getForgetPasswordCode()]);
  39. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  40. }
  41. public function onNewEmployeeCreated(EmployeeCreatedEvent $event): void
  42. {
  43. $user = $event->getUser();
  44. $notificationType = $event->getType();
  45. $emailNotification = $this->getNotificationType($notificationType);
  46. $parameters = [];
  47. $parameters["##userFullName##"] = $user->getFullname();
  48. $parameters["##loginLink##"] = $_ENV['BASE_URL'].$this->urlGenerator->generate('app_login');
  49. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  50. }
  51. /**
  52. * @param UserEvent $event
  53. */
  54. public function onUserRegistrationApproved(UserEvent $event): void
  55. {
  56. $user = $event->getUser();
  57. $notificationType = $event->getType();
  58. $emailNotification = $this->getNotificationType($notificationType);
  59. $parameters = [];
  60. $parameters["##baseURL##"] = $_ENV['BASE_URL'];
  61. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  62. }
  63. public function onNotifyReview(NotifyReviewEvent $event): void
  64. {
  65. // this one remains
  66. $review = $event->getReview();
  67. /** @var User $user */
  68. $user = $review->getReviewCreator();
  69. if ($user) {
  70. $changes = $this->reviewHelper->getChangeSetValues($event->getReview(), $event->getChangeSet());
  71. $reviewType = $this->reviewHelper->getReviewType($review);
  72. if ($reviewType && strpos($reviewType, 'Edition') !== false && !$changes) {
  73. return;
  74. }
  75. $viewName = 'Email/reviewNotify.html.twig';
  76. $parameters = [
  77. 'user' => $user,
  78. 'review' => $review,
  79. 'reviewType' => $reviewType,
  80. 'changes' => $changes
  81. ];
  82. $subject = 'Munireg - Review Updated';
  83. $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  84. }
  85. }
  86. /**
  87. * @param NotifyReviewEvent $event
  88. * @throws ExceptionInterface
  89. */
  90. public function onNotifyReviewDenied(NotifyReviewEvent $event): void
  91. {
  92. $review = $event->getReview();
  93. $reason = $event->getReason();
  94. /** @var User $user */
  95. $user = $review->getReviewCreator();
  96. $parameters = ['##reason##' => $reason];
  97. if ($review instanceof RegistrationFormReview || $review instanceof RenewalFormReview || $review instanceof DeregistrationFormReview || $review instanceof InspectionFormReview) {
  98. $property = $review->getProperty();
  99. $address = $property->getAddress() . ', ' . $property->getCity() . ', ' . $property->getState() . ', ' . $property->getZipCode();
  100. $parameters['##address##'] = 'Property Address: ' . $address;
  101. }
  102. if ($user) {
  103. $notificationType = $event->getType();
  104. $emailNotification = $this->getNotificationType($notificationType);
  105. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  106. }
  107. }
  108. public function onUpdateUserPassword(UpdateUserPasswordEvent $event): void
  109. {
  110. $this->sendPasswordUpdateNotification($event);
  111. }
  112. public function onNotifiedUsersForUpdatePassword(NotifiedForUpdateUserPasswordEvent $event): void
  113. {
  114. $this->sendPasswordUpdateNotification($event);
  115. }
  116. public function getNotificationType(string $notificationType)
  117. {
  118. return $this->entityManager->getRepository(EmailNotifications::class)->findOneBy(['notificationType' => $notificationType]);
  119. }
  120. private function sendPasswordUpdateNotification($event): void
  121. {
  122. $user = $event->getUser();
  123. $notificationType = $event->getType();
  124. $emailNotification = $this->getNotificationType($notificationType);
  125. $parameters = [];
  126. $parameters["##userName##"] = $user->getFullname();
  127. $parameters["##userMail##"] = $user->getEmail();
  128. $parameters["##resetPasswordLink##"] = $_ENV['MAIN_URL'].$this->urlGenerator->generate('app_update_user_password', ['code' => $user->getForgetPasswordCode()]);
  129. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  130. }
  131. }