src/EventSubscriber/EmailSubscriber.php line 166

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\EmailNotifications;
  4. use App\Event\AppEvents;
  5. use App\Event\EmployeeCreatedEvent;
  6. use App\Event\NotifiedForUpdateUserPasswordEvent;
  7. use App\Event\Review\NotifyReviewEvent;
  8. use App\Event\Review\UserEvent;
  9. use App\Event\UpdateUserPasswordEvent;
  10. use App\Event\UserResetPasswordEvent;
  11. use App\Helper\ReviewHelper;
  12. use App\Entity\User;
  13. use App\Lib\EmailSender;
  14. use App\Lib\EmailSenderInterface;
  15. use App\Lib\NotificationEmailSender;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  19. /**
  20.  * Class EmailSubscriber
  21.  */
  22. class EmailSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var EmailSender
  26.      */
  27.     private $emailSender;
  28.     /**
  29.      * @var ReviewHelper
  30.      */
  31.     private $reviewHelper;
  32.     private EntityManagerInterface $entityManager;
  33.     private NotificationEmailSender $notificationEmailSender;
  34.     private UrlGeneratorInterface $urlGenerator;
  35.     /**
  36.      * EmailListener constructor.
  37.      *
  38.      * @param EmailSender $emailSender
  39.      * @param ReviewHelper         $reviewHelper,
  40.      */
  41.     public function __construct(
  42.         EmailSender $emailSender,
  43.         ReviewHelper $reviewHelper,
  44.         EntityManagerInterface $entityManager,
  45.         NotificationEmailSender $notificationEmailSender,
  46.         UrlGeneratorInterface $urlGenerator
  47.     ) {
  48.         $this->emailSender $emailSender;
  49.         $this->reviewHelper $reviewHelper;
  50.         $this->entityManager $entityManager;
  51.         $this->notificationEmailSender $notificationEmailSender;
  52.         $this->urlGenerator $urlGenerator;
  53.     }
  54.     /**
  55.      * @return array
  56.      */
  57.     public static function getSubscribedEvents()
  58.     {
  59.         return [
  60.             AppEvents::EMPLOYEE_CREATED => 'onNewEmployeeCreated',
  61.             AppEvents::USER_RESET_PASSWORD => 'onUserResetPassword',
  62.             AppEvents::USER_REGISTRATION_APPROVED => 'onUserRegistrationApproved',
  63.             AppEvents::REVIEW_NOTIFY => 'onNotifyReview',
  64.             AppEvents::REVIEW_NOTIFY_DENIED => 'onNotifyReviewDenied',
  65.             AppEvents::UPDATE_USER_PASSWORD => 'onUpdateUserPassword',
  66.             AppEvents::NOTIFIED_USERS_FOR_UPDATE_PASSWORD => 'onNotifiedUsersForUpdatePassword',
  67.         ];
  68.     }
  69.     /**
  70.      * @param UserResetPasswordEvent $event
  71.      */
  72.     public function onUserResetPassword(UserResetPasswordEvent $event)
  73.     {
  74.         $user $event->getUser();
  75.         $notificationType $event->getType();
  76.         $emailNotification $this->getNotificationType($notificationType);
  77.         $parameters = [];
  78.         $parameters["##userName##"] = $user->getFullname();
  79.         $parameters["##userMail##"] = $user->getEmail();
  80.         $parameters["##resetPasswordLink##"] = $_ENV['MAIN_URL'].$this->urlGenerator->generate('app_change_password', ['code' => $user->getForgetPasswordCode()]);
  81.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  82. //        $viewName = 'Email/resetPassword.html.twig';
  83. //        $parameters = ['user' => $user];
  84. //        $subject = 'MuniReg - Reset Password';
  85. //
  86. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  87.     }
  88.     /**
  89.      * @param EmployeeCreatedEvent $event
  90.      */
  91.     public function onNewEmployeeCreated(EmployeeCreatedEvent $event)
  92.     {
  93.         $user $event->getUser();
  94.         $notificationType $event->getType();
  95.         $emailNotification $this->getNotificationType($notificationType);
  96.         $parameters = [];
  97.         $parameters["##userFullName##"] = $user->getFullname();
  98.         $parameters["##loginLink##"] = $_ENV['BASE_URL'].$this->urlGenerator->generate('app_login');
  99.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  100. //
  101. //        $viewName = 'Email/newEmployee.html.twig';
  102. //        $parameters = ['user' => $user];
  103. //        $subject = 'Munireg - New Employee Created';
  104. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  105.     }
  106.     /**
  107.      * @param UserEvent $event
  108.      */
  109.     public function onUserRegistrationApproved(UserEvent $event)
  110.     {
  111.         $user $event->getUser();
  112.         $notificationType $event->getType();
  113.         $emailNotification $this->getNotificationType($notificationType);
  114.         $parameters = [];
  115.         $parameters["##baseURL##"] = $_ENV['BASE_URL'];
  116.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  117. //        $viewName = 'Email/userRegistrationApprovedNotify.html.twig';
  118. //        $parameters = ['user' => $user];
  119. //        $subject = 'Munireg - New Account Created';
  120. //
  121. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  122.     }
  123.     /**
  124.      * @param NotifyReviewEvent $event
  125.      */
  126.     public function onNotifyReview(NotifyReviewEvent $event)
  127.     {
  128.         // this one remains
  129.         $review $event->getReview();
  130.         /** @var User $user */
  131.         $user $review->getReviewCreator();
  132.         if ($user) {
  133.             $changes $this->reviewHelper->getChangeSetValues($event->getReview(), $event->getChangeSet());
  134.             $reviewType $this->reviewHelper->getReviewType($review);
  135.             if ($reviewType && strpos($reviewType'Edition') !== false && !$changes) {
  136.                 return;
  137.             }
  138.             $viewName 'Email/reviewNotify.html.twig';
  139.             $parameters = [
  140.                 'user' => $user,
  141.                 'review' => $review,
  142.                 'reviewType' => $reviewType,
  143.                 'changes' => $changes
  144.             ];
  145.             $subject 'Munireg - Review Updated';
  146.             $this->emailSender->sendEmail($viewName$parameters$subject$user->getEmail());
  147.         }
  148.     }
  149.     /**
  150.      * @param NotifyReviewEvent $event
  151.      */
  152.     public function onNotifyReviewDenied(NotifyReviewEvent $event)
  153.     {
  154.         $review $event->getReview();
  155.         /** @var User $user */
  156.         $user $review->getReviewCreator();
  157.         if ($user) {
  158.             $notificationType $event->getType();
  159.             $emailNotification $this->getNotificationType($notificationType);
  160.             $this->notificationEmailSender->sendNotificationMail($emailNotification$user);
  161. //            $viewName = 'Email/reviewDeniedNotify.html.twig';
  162. //            $subject = 'Munireg - Review Denied';
  163. //
  164. //            $this->emailSender->sendEmail($viewName, [], $subject, $user->getEmail());
  165.         }
  166.     }
  167.     /**
  168.      * @param UpdateUserPasswordEvent $event
  169.      */
  170.     public function onUpdateUserPassword(UpdateUserPasswordEvent $event)
  171.     {
  172.         $this->sendPasswordUpdateNotification($event);
  173.     }
  174.     public function onNotifiedUsersForUpdatePassword(NotifiedForUpdateUserPasswordEvent $event)
  175.     {
  176.         $this->sendPasswordUpdateNotification($event);
  177.     }
  178.     public function getNotificationType(string $notificationType)
  179.     {
  180.         return $this->entityManager->getRepository(EmailNotifications::class)->findOneBy(['notificationType' => $notificationType]);
  181.     }
  182.     private function sendPasswordUpdateNotification($event)
  183.     {
  184.         $user $event->getUser();
  185.         $notificationType $event->getType();
  186.         $emailNotification $this->getNotificationType($notificationType);
  187.         $parameters = [];
  188.         $parameters["##userName##"] = $user->getFullname();
  189.         $parameters["##userMail##"] = $user->getEmail();
  190.         $parameters["##resetPasswordLink##"] = $_ENV['MAIN_URL'].$this->urlGenerator->generate('app_update_user_password', ['code' => $user->getForgetPasswordCode()]);
  191.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  192.     }
  193. }