<?php
namespace App\EventSubscriber;
use App\Entity\EmailNotifications;
use App\Event\AppEvents;
use App\Event\FormEvent;
use App\Event\PropertyEvent;
use App\Entity\Form;
use App\Entity\Property;
use App\Entity\RenewalForm;
use App\Entity\User;
use App\Entity\UserNotification;
use App\Lib\NotificationEmailSender;
use App\ValueObject\UserNotificationTypes;
use App\ValueObject\UserTypes;
use App\Lib\EmailSenderInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class NotificationListener
*/
class NotificationSubscriber implements EventSubscriberInterface
{
const ENABLED = true;
const FORM_NOTIFICATION = 'form_notification';
/**
* @var EmailSenderInterface
*/
private $emailSender;
/**
* @var EntityManagerInterface
*/
private $entityManager;
private NotificationEmailSender $notificationEmailSender;
/**
* NotificationSubscriber constructor.
*
* @param EmailSenderInterface $emailSender
* @param EntityManagerInterface $entityManager
*/
public function __construct(
EmailSenderInterface $emailSender,
EntityManagerInterface $entityManager,
NotificationEmailSender $notificationEmailSender
) {
$this->emailSender = $emailSender;
$this->entityManager = $entityManager;
$this->notificationEmailSender = $notificationEmailSender;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
AppEvents::PROPERTY_CREATED => 'onPropertyCreated',
AppEvents::REGISTRATION_FORM_CREATED => 'onRegistrationFormCreated',
AppEvents::DEREGISTRATION_FORM_CREATED => 'onDeregistrationFormCreated',
AppEvents::RENEWAL_FORM_CREATED => 'onRenewalCreated',
AppEvents::RENEWAL_FORM_DUE => 'onRenewalFormDue',
AppEvents::RENEWAL_FORM_UPCOMING => 'onRenewalFormUpcoming'
];
}
/**
* @param PropertyEvent $event
*/
public function onPropertyCreated(PropertyEvent $event)
{
$property = $event->getProperty();
$viewName = 'Notification/candidate_notification.html.twig';
$notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
'type' => UserNotificationTypes::NEW_CANDIDATES,
'value' => self::ENABLED
]);
$emailNotification = $this->getEmailNotification(UserNotificationTypes::NEW_CANDIDATES);
foreach ($notifications as $notification) {
/** @var UserNotification $notification */
$user = $notification->getUser();
if ($this->shouldNotifyRegister($user, $property))
{
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##userName##'] = $user->getFullname();
$parameters['##address##'] = $address;
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->notifyRegister($property, $user, $viewName, 'New Candidate Property');
}
}
}
/**
* @param FormEvent $event
*/
public function onRegistrationFormCreated(FormEvent $event)
{
$form = $event->getForm();
$property = $form->getProperty();
$superAdmins = $this->getSuperAdmins();
$registeredUser = null;
if (!empty($form->getCreatedBy())) {
$registeredUser = $this->entityManager->getRepository(User::class)->findOneByEmail($form->getCreatedBy());
}
$notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
'type' => UserNotificationTypes::REGISTRATIONS,
'value' => self::ENABLED
]);
$emailNotification = $this->getEmailNotification();
foreach ($notifications as $notification) {
/** @var UserNotification $notification */
$user = $notification->getUser();
if ($this->shouldNotifyMunicipal($user, $property))
{
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##userName##'] = $user->getFullname();
$parameters['##address##'] = $address;
$parameters['##subject##'] = "Notification For ".ucfirst(UserNotificationTypes::REGISTRATIONS);
$parameters['##formType##'] = ucfirst(UserNotificationTypes::REGISTRATIONS);
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->notifyMunicipalSuperAdminAndRegistrant($form, 'Registration', $user);
}
}
$this->notifySuperAdminAndRegistrant($form, $superAdmins, $registeredUser, ucfirst(UserNotificationTypes::REGISTRATIONS), $emailNotification);
}
/**
* @param FormEvent $event
*/
public function onDeregistrationFormCreated(FormEvent $event)
{
$form = $event->getForm();
$property = $form->getProperty();
$superAdmins = $this->getSuperAdmins();
$registeredUser = null;
if (!empty($form->getCreatedBy())) {
$registeredUser = $this->entityManager->getRepository(User::class)->findOneByEmail($form->getCreatedBy());
}
$notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
'type' => UserNotificationTypes::DEREGISTRATIONS,
'value' => self::ENABLED
]);
$emailNotification = $this->getEmailNotification();
foreach ($notifications as $notification) {
/** @var UserNotification $notification */
$user = $notification->getUser();
if ($this->shouldNotifyMunicipal($user, $property))
{
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##userName##'] = $user->getFullname();
$parameters['##address##'] = $address;
$parameters['##subject##'] = "Notification For ".ucfirst(UserNotificationTypes::DEREGISTRATIONS);
$parameters['##formType##'] = ucfirst(UserNotificationTypes::DEREGISTRATIONS);
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->notifyMunicipalSuperAdminAndRegistrant($form, 'Deregistration', $user);
}
}
$this->notifySuperAdminAndRegistrant($form, $superAdmins, $registeredUser, ucfirst(UserNotificationTypes::DEREGISTRATIONS), $emailNotification);
}
/**
* @param FormEvent $event
*/
public function onRenewalCreated(FormEvent $event)
{
$form = $event->getForm();
$property = $form->getProperty();
$superAdmins = $this->getSuperAdmins();
$registeredUser = null;
if (!empty($form->getCreatedBy())) {
$registeredUser = $this->entityManager->getRepository(User::class)->findOneByEmail($form->getCreatedBy());
}
$notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
'type' => UserNotificationTypes::RENEWALS,
'value' => self::ENABLED
]);
$emailNotification = $this->getEmailNotification();
foreach ($notifications as $notification) {
/** @var UserNotification $notification */
$user = $notification->getUser();
if ($this->shouldNotifyMunicipal($user, $property))
{
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##userName##'] = $user->getFullname();
$parameters['##address##'] = $address;
$parameters['##subject##'] = "Notification For ".ucfirst(UserNotificationTypes::RENEWALS);
$parameters['##formType##'] = ucfirst(UserNotificationTypes::RENEWALS);
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->notifyMunicipalSuperAdminAndRegistrant($form, 'Renewal', $user);
}
}
$this->notifySuperAdminAndRegistrant($form, $superAdmins, $registeredUser, ucfirst(UserNotificationTypes::RENEWALS), $emailNotification);
}
/**
* @param FormEvent $event
*/
public function onRenewalFormUpcoming(FormEvent $event)
{
/** @var RenewalForm $form */
$form = $event->getForm();
$property = $form->getProperty();
$viewName = 'Notification/upcoming_renewal_notification.html.twig';
$notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
'type' => UserNotificationTypes::UPCOMING_RENEWALS,
'value' => self::ENABLED
]);
$emailNotification = $this->getEmailNotification(UserNotificationTypes::UPCOMING_RENEWALS);
foreach ($notifications as $notification) {
/** @var UserNotification $notification */
$user = $notification->getUser();
if ($this->shouldNotifyUpcomingRenewal($user, $form))
{
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##userName##'] = $user->getFullname();
$parameters['##address##'] = $address;
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
}
}
}
/**
* @param FormEvent $event
*/
public function onRenewalFormDue(FormEvent $event)
{
/** @var RenewalForm $form */
$form = $event->getForm();
$viewName = 'Notification/due_renewal_notification.html.twig';
$property = $form->getProperty();
$notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
'type' => UserNotificationTypes::DUE_RENEWALS,
'value' => self::ENABLED
]);
$emailNotification = $this->getEmailNotification(UserNotificationTypes::DUE_RENEWALS);
foreach ($notifications as $notification) {
/** @var UserNotification $notification */
$user = $notification->getUser();
if ($this->shouldNotifyDueRenewal($user, $form))
{
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##userName##'] = $user->getFullname();
$parameters['##address##'] = $address;
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->notifyRegister($form->getProperty(), $user, $viewName, 'Due Renewal');
}
}
}
/**
* @param User $user
* @param Property $property
*
* @return bool
*/
private function shouldNotifyMunicipal(User $user, Property $property): bool
{
if ($user->getType() === UserTypes::MUNICIPALITY) {
$isOwner = ($property->getMunicipality() === $user->getMunicipality());
return $isOwner;
}
return false;
}
/**
* @param User $user
* @param Property $property
*
* @return bool
*/
private function shouldNotifyRegister(User $user, Property $property)
{
if ($user->getType() === UserTypes::REGISTER_PARTY) {
$isOwner = ($property->getRegistrant() === $user->getOrganization());
$requireRegistration = ($property->getRegistrationDate() == null);
return ($isOwner && $requireRegistration);
}
return false;
}
/**
* @param User $user
* @param RenewalForm $renewal
*
* @return bool
*/
private function shouldNotifyUpcomingRenewal(User $user, RenewalForm $renewal)
{
if ($user->getType() === UserTypes::REGISTER_PARTY) {
return ($renewal->getRegistrant() === $user->getOrganization());
}
return false;
}
/**
* @param User $user
* @param RenewalForm $renewal
*
* @return bool
*/
private function shouldNotifyDueRenewal(User $user, RenewalForm $renewal)
{
if ($user->getType() === UserTypes::REGISTER_PARTY) {
$isOwner = ($renewal->getRegistrant() === $user->getOrganization());;
return ($isOwner && $renewal->hasDue());
}
return false;
}
// /**
// * @param Form $form
// * @param $formType
// * @param User $user
// */
// private function notifyMunicipalSuperAdminAndRegistrant(Form $form, $formType, User $user)
// {
// $viewName = 'Notification/form_notification.html.twig';
// $subject = 'Notification for '.$formType;
// $parameters = [
// 'property' => $form->getProperty(),
// 'user' => $user,
// 'formType' => $formType
// ];
//
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
// }
/**
* @param Property $property
* @param $viewName
* @param User $user
* @param $subject
*/
private function notifyRegister(Property $property, User $user, $viewName, $subject)
{
$parameters = [
'property' => $property,
'user' => $user,
];
$this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
}
public function getSuperAdmins()
{
return $this->entityManager->getRepository(User::class)->findBy(['isSuperAdmin' => true, 'isVerified' => true]);
}
public function getEmailNotification($type = null)
{
return $this->entityManager->getRepository(EmailNotifications::class)->findOneBy([
'notificationType' => $type != null ? $type : self::FORM_NOTIFICATION
]);
}
public function getPropertyFullAddress(Property $property)
{
$address = $property->getAddress();
$address .= $property->getZipCode() ? ', '.$property->getZipCode() : '';
$address .= $property->getCity() ? ', '.$property->getCity() : '';
$address .= $property->getState() ? ', '.$property->getState() : '';
return $address;
}
public function notifySuperAdminAndRegistrant($form, array $superAdmins, User $registrant = null, $type, EmailNotifications $emailNotification = null)
{
$property = $form->getProperty();
$address = $this->getPropertyFullAddress($property);
$parameters = [];
$parameters['##address##'] = $address;
$parameters['##formType##'] = ucfirst($type);
$parameters['##subject##'] = "Notification For ".ucfirst($type);
foreach ($superAdmins as $superAdmin) {
if ($superAdmin !== $registrant) {
continue;
}
$parameters['##userName##'] = $superAdmin->getFullname();
$this->notificationEmailSender->sendNotificationMail($emailNotification, $superAdmin, $parameters);
// $this->notifyMunicipalSuperAdminAndRegistrant($form, $type, $superAdmin);
}
$parameters['##userName##'] = $registrant->getFullname();
$this->notificationEmailSender->sendNotificationMail($emailNotification, $registrant, $parameters);
}
}