src/EventSubscriber/EmailSubscriber.php line 166

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