<?php
namespace App\EventSubscriber;
use App\Entity\EmailNotifications;
use App\Event\AppEvents;
use App\Event\EmployeeCreatedEvent;
use App\Event\NotifiedForUpdateUserPasswordEvent;
use App\Event\Review\NotifyReviewEvent;
use App\Event\Review\UserEvent;
use App\Event\UpdateUserPasswordEvent;
use App\Event\UserResetPasswordEvent;
use App\Helper\ReviewHelper;
use App\Entity\User;
use App\Lib\EmailSender;
use App\Lib\EmailSenderInterface;
use App\Lib\NotificationEmailSender;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Class EmailSubscriber
*/
class EmailSubscriber implements EventSubscriberInterface
{
/**
* @var EmailSender
*/
private $emailSender;
/**
* @var ReviewHelper
*/
private $reviewHelper;
private EntityManagerInterface $entityManager;
private NotificationEmailSender $notificationEmailSender;
private UrlGeneratorInterface $urlGenerator;
/**
* EmailListener constructor.
*
* @param EmailSender $emailSender
* @param ReviewHelper $reviewHelper,
*/
public function __construct(
EmailSender $emailSender,
ReviewHelper $reviewHelper,
EntityManagerInterface $entityManager,
NotificationEmailSender $notificationEmailSender,
UrlGeneratorInterface $urlGenerator
) {
$this->emailSender = $emailSender;
$this->reviewHelper = $reviewHelper;
$this->entityManager = $entityManager;
$this->notificationEmailSender = $notificationEmailSender;
$this->urlGenerator = $urlGenerator;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
AppEvents::EMPLOYEE_CREATED => 'onNewEmployeeCreated',
AppEvents::USER_RESET_PASSWORD => 'onUserResetPassword',
AppEvents::USER_REGISTRATION_APPROVED => 'onUserRegistrationApproved',
AppEvents::REVIEW_NOTIFY => 'onNotifyReview',
AppEvents::REVIEW_NOTIFY_DENIED => 'onNotifyReviewDenied',
AppEvents::UPDATE_USER_PASSWORD => 'onUpdateUserPassword',
AppEvents::NOTIFIED_USERS_FOR_UPDATE_PASSWORD => 'onNotifiedUsersForUpdatePassword',
];
}
/**
* @param UserResetPasswordEvent $event
*/
public function onUserResetPassword(UserResetPasswordEvent $event)
{
$user = $event->getUser();
$notificationType = $event->getType();
$emailNotification = $this->getNotificationType($notificationType);
$parameters = [];
$parameters["##userName##"] = $user->getFullname();
$parameters["##userMail##"] = $user->getEmail();
$parameters["##resetPasswordLink##"] = $_ENV['MAIN_URL'].$this->urlGenerator->generate('app_change_password', ['code' => $user->getForgetPasswordCode()]);
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $viewName = 'Email/resetPassword.html.twig';
// $parameters = ['user' => $user];
// $subject = 'MuniReg - Reset Password';
//
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
}
/**
* @param EmployeeCreatedEvent $event
*/
public function onNewEmployeeCreated(EmployeeCreatedEvent $event)
{
$user = $event->getUser();
$notificationType = $event->getType();
$emailNotification = $this->getNotificationType($notificationType);
$parameters = [];
$parameters["##userFullName##"] = $user->getFullname();
$parameters["##loginLink##"] = $_ENV['BASE_URL'].$this->urlGenerator->generate('app_login');
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
//
// $viewName = 'Email/newEmployee.html.twig';
// $parameters = ['user' => $user];
// $subject = 'Munireg - New Employee Created';
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
}
/**
* @param UserEvent $event
*/
public function onUserRegistrationApproved(UserEvent $event)
{
$user = $event->getUser();
$notificationType = $event->getType();
$emailNotification = $this->getNotificationType($notificationType);
$parameters = [];
$parameters["##baseURL##"] = $_ENV['BASE_URL'];
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $viewName = 'Email/userRegistrationApprovedNotify.html.twig';
// $parameters = ['user' => $user];
// $subject = 'Munireg - New Account Created';
//
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
}
/**
* @param NotifyReviewEvent $event
*/
public function onNotifyReview(NotifyReviewEvent $event)
{
// this one remains
$review = $event->getReview();
/** @var User $user */
$user = $review->getReviewCreator();
if ($user) {
$changes = $this->reviewHelper->getChangeSetValues($event->getReview(), $event->getChangeSet());
$reviewType = $this->reviewHelper->getReviewType($review);
if ($reviewType && strpos($reviewType, 'Edition') !== false && !$changes) {
return;
}
$viewName = 'Email/reviewNotify.html.twig';
$parameters = [
'user' => $user,
'review' => $review,
'reviewType' => $reviewType,
'changes' => $changes
];
$subject = 'Munireg - Review Updated';
$this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
}
}
/**
* @param NotifyReviewEvent $event
*/
public function onNotifyReviewDenied(NotifyReviewEvent $event)
{
$review = $event->getReview();
/** @var User $user */
$user = $review->getReviewCreator();
if ($user) {
$notificationType = $event->getType();
$emailNotification = $this->getNotificationType($notificationType);
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user);
// $viewName = 'Email/reviewDeniedNotify.html.twig';
// $subject = 'Munireg - Review Denied';
//
// $this->emailSender->sendEmail($viewName, [], $subject, $user->getEmail());
}
}
/**
* @param UpdateUserPasswordEvent $event
*/
public function onUpdateUserPassword(UpdateUserPasswordEvent $event)
{
$this->sendPasswordUpdateNotification($event);
}
public function onNotifiedUsersForUpdatePassword(NotifiedForUpdateUserPasswordEvent $event)
{
$this->sendPasswordUpdateNotification($event);
}
public function getNotificationType(string $notificationType)
{
return $this->entityManager->getRepository(EmailNotifications::class)->findOneBy(['notificationType' => $notificationType]);
}
private function sendPasswordUpdateNotification($event)
{
$user = $event->getUser();
$notificationType = $event->getType();
$emailNotification = $this->getNotificationType($notificationType);
$parameters = [];
$parameters["##userName##"] = $user->getFullname();
$parameters["##userMail##"] = $user->getEmail();
$parameters["##resetPasswordLink##"] = $_ENV['MAIN_URL'].$this->urlGenerator->generate('app_update_user_password', ['code' => $user->getForgetPasswordCode()]);
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
}
}