src/EventSubscriber/NotificationSubscriber.php line 246

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\EmailNotifications;
  4. use App\Event\AppEvents;
  5. use App\Event\FormEvent;
  6. use App\Event\PropertyEvent;
  7. use App\Entity\Form;
  8. use App\Entity\Property;
  9. use App\Entity\RenewalForm;
  10. use App\Entity\User;
  11. use App\Entity\UserNotification;
  12. use App\Lib\NotificationEmailSender;
  13. use App\ValueObject\UserNotificationTypes;
  14. use App\ValueObject\UserTypes;
  15. use App\Lib\EmailSenderInterface;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. /**
  19.  * Class NotificationListener
  20.  */
  21. class NotificationSubscriber implements EventSubscriberInterface
  22. {
  23.     const ENABLED true;
  24.     const FORM_NOTIFICATION 'form_notification';
  25.     /**
  26.      * @var EmailSenderInterface
  27.      */
  28.     private $emailSender;
  29.     /**
  30.      * @var EntityManagerInterface
  31.      */
  32.     private $entityManager;
  33.     private NotificationEmailSender $notificationEmailSender;
  34.     /**
  35.      * NotificationSubscriber constructor.
  36.      *
  37.      * @param EmailSenderInterface   $emailSender
  38.      * @param EntityManagerInterface $entityManager
  39.      */
  40.     public function __construct(
  41.         EmailSenderInterface $emailSender,
  42.         EntityManagerInterface $entityManager,
  43.         NotificationEmailSender $notificationEmailSender
  44.     ) {
  45.         $this->emailSender $emailSender;
  46.         $this->entityManager $entityManager;
  47.         $this->notificationEmailSender $notificationEmailSender;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             AppEvents::PROPERTY_CREATED => 'onPropertyCreated',
  56.             AppEvents::REGISTRATION_FORM_CREATED => 'onRegistrationFormCreated',
  57.             AppEvents::DEREGISTRATION_FORM_CREATED => 'onDeregistrationFormCreated',
  58.             AppEvents::RENEWAL_FORM_CREATED => 'onRenewalCreated',
  59.             AppEvents::RENEWAL_FORM_DUE => 'onRenewalFormDue',
  60.             AppEvents::RENEWAL_FORM_UPCOMING => 'onRenewalFormUpcoming'
  61.         ];
  62.     }
  63.     /**
  64.      * @param PropertyEvent $event
  65.      */
  66.     public function onPropertyCreated(PropertyEvent $event)
  67.     {
  68.         $property $event->getProperty();
  69.         $viewName 'Notification/candidate_notification.html.twig';
  70.         $notifications $this->entityManager->getRepository(UserNotification::class)->findBy([
  71.             'type' => UserNotificationTypes::NEW_CANDIDATES,
  72.             'value' => self::ENABLED
  73.         ]);
  74.         $emailNotification $this->getEmailNotification(UserNotificationTypes::NEW_CANDIDATES);
  75.         foreach ($notifications as $notification) {
  76.             /** @var UserNotification $notification */
  77.             $user $notification->getUser();
  78.             if ($this->shouldNotifyRegister($user$property))
  79.             {
  80.                 $address $this->getPropertyFullAddress($property);
  81.                 $parameters = [];
  82.                 $parameters['##userName##'] = $user->getFullname();
  83.                 $parameters['##address##'] = $address;
  84.                 $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  85. //                $this->notifyRegister($property, $user, $viewName, 'New Candidate Property');
  86.             }
  87.         }
  88.     }
  89.     /**
  90.      * @param FormEvent $event
  91.      */
  92.     public function onRegistrationFormCreated(FormEvent $event)
  93.     {
  94.         $form $event->getForm();
  95.         $property $form->getProperty();
  96.         $superAdmins $this->getSuperAdmins();
  97.         $registeredUser null;
  98.         if (!empty($form->getCreatedBy())) {
  99.             $registeredUser $this->entityManager->getRepository(User::class)->findOneByEmail($form->getCreatedBy());
  100.         }
  101.         $notifications $this->entityManager->getRepository(UserNotification::class)->findBy([
  102.             'type' => UserNotificationTypes::REGISTRATIONS,
  103.             'value' => self::ENABLED
  104.         ]);
  105.         $emailNotification $this->getEmailNotification();
  106.         foreach ($notifications as $notification) {
  107.             /** @var UserNotification $notification */
  108.             $user $notification->getUser();
  109.             if ($this->shouldNotifyMunicipal($user$property))
  110.             {
  111.                 $address $this->getPropertyFullAddress($property);
  112.                 $parameters = [];
  113.                 $parameters['##userName##'] = $user->getFullname();
  114.                 $parameters['##address##'] = $address;
  115.                 $parameters['##subject##'] = "Notification For ".ucfirst(UserNotificationTypes::REGISTRATIONS);
  116.                 $parameters['##formType##'] = ucfirst(UserNotificationTypes::REGISTRATIONS);
  117.                 $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  118. //                $this->notifyMunicipalSuperAdminAndRegistrant($form, 'Registration', $user);
  119.             }
  120.         }
  121.         $this->notifySuperAdminAndRegistrant($form$superAdmins$registeredUserucfirst(UserNotificationTypes::REGISTRATIONS), $emailNotification);
  122.     }
  123.     /**
  124.      * @param FormEvent $event
  125.      */
  126.     public function onDeregistrationFormCreated(FormEvent $event)
  127.     {
  128.         $form $event->getForm();
  129.         $property $form->getProperty();
  130.         $superAdmins $this->getSuperAdmins();
  131.         $registeredUser null;
  132.         if (!empty($form->getCreatedBy())) {
  133.             $registeredUser $this->entityManager->getRepository(User::class)->findOneByEmail($form->getCreatedBy());
  134.         }
  135.         $notifications $this->entityManager->getRepository(UserNotification::class)->findBy([
  136.             'type' => UserNotificationTypes::DEREGISTRATIONS,
  137.             'value' => self::ENABLED
  138.         ]);
  139.         $emailNotification $this->getEmailNotification();
  140.         foreach ($notifications as $notification) {
  141.             /** @var UserNotification $notification */
  142.             $user $notification->getUser();
  143.             if ($this->shouldNotifyMunicipal($user$property))
  144.             {
  145.                 $address $this->getPropertyFullAddress($property);
  146.                 $parameters = [];
  147.                 $parameters['##userName##'] = $user->getFullname();
  148.                 $parameters['##address##'] = $address;
  149.                 $parameters['##subject##'] = "Notification For ".ucfirst(UserNotificationTypes::DEREGISTRATIONS);
  150.                 $parameters['##formType##'] = ucfirst(UserNotificationTypes::DEREGISTRATIONS);
  151.                 $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  152. //                $this->notifyMunicipalSuperAdminAndRegistrant($form, 'Deregistration', $user);
  153.             }
  154.         }
  155.         $this->notifySuperAdminAndRegistrant($form$superAdmins$registeredUserucfirst(UserNotificationTypes::DEREGISTRATIONS), $emailNotification);
  156.     }
  157.     /**
  158.      * @param FormEvent $event
  159.      */
  160.     public function onRenewalCreated(FormEvent $event)
  161.     {
  162.         $form $event->getForm();
  163.         $property $form->getProperty();
  164.         $superAdmins $this->getSuperAdmins();
  165.         $registeredUser null;
  166.         if (!empty($form->getCreatedBy())) {
  167.             $registeredUser $this->entityManager->getRepository(User::class)->findOneByEmail($form->getCreatedBy());
  168.         }
  169.         $notifications $this->entityManager->getRepository(UserNotification::class)->findBy([
  170.             'type' => UserNotificationTypes::RENEWALS,
  171.             'value' => self::ENABLED
  172.         ]);
  173.         $emailNotification $this->getEmailNotification();
  174.         foreach ($notifications as $notification) {
  175.             /** @var UserNotification $notification */
  176.             $user $notification->getUser();
  177.             if ($this->shouldNotifyMunicipal($user$property))
  178.             {
  179.                 $address $this->getPropertyFullAddress($property);
  180.                 $parameters = [];
  181.                 $parameters['##userName##'] = $user->getFullname();
  182.                 $parameters['##address##'] = $address;
  183.                 $parameters['##subject##'] = "Notification For ".ucfirst(UserNotificationTypes::RENEWALS);
  184.                 $parameters['##formType##'] = ucfirst(UserNotificationTypes::RENEWALS);
  185.                 $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  186. //                $this->notifyMunicipalSuperAdminAndRegistrant($form, 'Renewal', $user);
  187.             }
  188.         }
  189.         $this->notifySuperAdminAndRegistrant($form$superAdmins$registeredUserucfirst(UserNotificationTypes::RENEWALS), $emailNotification);
  190.     }
  191.     /**
  192.      * @param FormEvent $event
  193.      */
  194.     public function onRenewalFormUpcoming(FormEvent $event)
  195.     {
  196.         /** @var RenewalForm $form */
  197.         $form $event->getForm();
  198.         $property $form->getProperty();
  199.         $viewName 'Notification/upcoming_renewal_notification.html.twig';
  200.         $notifications $this->entityManager->getRepository(UserNotification::class)->findBy([
  201.             'type' => UserNotificationTypes::UPCOMING_RENEWALS,
  202.             'value' => self::ENABLED
  203.         ]);
  204.         $emailNotification $this->getEmailNotification(UserNotificationTypes::UPCOMING_RENEWALS);
  205.         foreach ($notifications as $notification) {
  206.             /** @var UserNotification $notification */
  207.             $user $notification->getUser();
  208.             if ($this->shouldNotifyUpcomingRenewal($user$form))
  209.             {
  210.                 $address $this->getPropertyFullAddress($property);
  211.                 $parameters = [];
  212.                 $parameters['##userName##'] = $user->getFullname();
  213.                 $parameters['##address##'] = $address;
  214.                 $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  215.             }
  216.         }
  217.     }
  218.         
  219.     /**
  220.      * @param FormEvent $event
  221.      */
  222.     public function onRenewalFormDue(FormEvent $event)
  223.     {
  224.         /** @var RenewalForm $form */
  225.         $form $event->getForm();
  226.         $viewName 'Notification/due_renewal_notification.html.twig';
  227.         $property $form->getProperty();
  228.         $notifications $this->entityManager->getRepository(UserNotification::class)->findBy([
  229.             'type' => UserNotificationTypes::DUE_RENEWALS,
  230.             'value' => self::ENABLED
  231.         ]);
  232.         $emailNotification $this->getEmailNotification(UserNotificationTypes::DUE_RENEWALS);
  233.         foreach ($notifications as $notification) {
  234.             /** @var UserNotification $notification */
  235.             $user $notification->getUser();
  236.             if ($this->shouldNotifyDueRenewal($user$form))
  237.             {
  238.                 $address $this->getPropertyFullAddress($property);
  239.                 $parameters = [];
  240.                 $parameters['##userName##'] = $user->getFullname();
  241.                 $parameters['##address##'] = $address;
  242.                 $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  243. //                $this->notifyRegister($form->getProperty(), $user, $viewName, 'Due Renewal');
  244.             }
  245.         }
  246.     }
  247.     /**
  248.      * @param User $user
  249.      * @param Property $property
  250.      *
  251.      * @return bool
  252.      */
  253.     private function shouldNotifyMunicipal(User $userProperty $property): bool
  254.     {
  255.         if ($user->getType() === UserTypes::MUNICIPALITY) {
  256.             $isOwner = ($property->getMunicipality() === $user->getMunicipality());
  257.             return $isOwner;
  258.         }
  259.         return false;
  260.     }
  261.     /**
  262.      * @param User $user
  263.      * @param Property $property
  264.      *
  265.      * @return bool
  266.      */
  267.     private function shouldNotifyRegister(User $userProperty $property)
  268.     {
  269.         if ($user->getType() === UserTypes::REGISTER_PARTY) {
  270.             $isOwner = ($property->getRegistrant() === $user->getOrganization());
  271.             $requireRegistration = ($property->getRegistrationDate() == null);
  272.             return ($isOwner && $requireRegistration);
  273.         }
  274.         return false;
  275.     }
  276.     /**
  277.      * @param User $user
  278.      * @param RenewalForm $renewal
  279.      *
  280.      * @return bool
  281.      */
  282.     private function shouldNotifyUpcomingRenewal(User $userRenewalForm $renewal)
  283.     {
  284.         if ($user->getType() === UserTypes::REGISTER_PARTY) {
  285.             return ($renewal->getRegistrant() === $user->getOrganization());
  286.         }
  287.         return false;
  288.     }
  289.     /**
  290.      * @param User $user
  291.      * @param RenewalForm $renewal
  292.      *
  293.      * @return bool
  294.      */
  295.     private function shouldNotifyDueRenewal(User $userRenewalForm $renewal)
  296.     {
  297.         if ($user->getType() === UserTypes::REGISTER_PARTY) {
  298.             $isOwner = ($renewal->getRegistrant() === $user->getOrganization());;
  299.             return ($isOwner && $renewal->hasDue());
  300.         }
  301.         return false;
  302.     }
  303. //    /**
  304. //     * @param Form $form
  305. //     * @param $formType
  306. //     * @param User $user
  307. //     */
  308. //    private function notifyMunicipalSuperAdminAndRegistrant(Form $form, $formType, User $user)
  309. //    {
  310. //        $viewName = 'Notification/form_notification.html.twig';
  311. //        $subject = 'Notification for '.$formType;
  312. //        $parameters = [
  313. //            'property' => $form->getProperty(),
  314. //            'user' => $user,
  315. //            'formType' => $formType
  316. //        ];
  317. //
  318. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  319. //    }
  320.     /**
  321.      * @param Property $property
  322.      * @param $viewName
  323.      * @param User $user
  324.      * @param $subject
  325.      */
  326.     private function notifyRegister(Property $propertyUser $user$viewName$subject)
  327.     {
  328.         $parameters = [
  329.             'property' => $property,
  330.             'user' => $user,
  331.         ];
  332.         $this->emailSender->sendEmail($viewName$parameters$subject$user->getEmail());
  333.     }
  334.     public function getSuperAdmins()
  335.     {
  336.         return $this->entityManager->getRepository(User::class)->findBy(['isSuperAdmin' => true'isVerified' => true]);
  337.     }
  338.     public function getEmailNotification($type null)
  339.     {
  340.         return $this->entityManager->getRepository(EmailNotifications::class)->findOneBy([
  341.             'notificationType' => $type != null $type self::FORM_NOTIFICATION
  342.         ]);
  343.     }
  344.     public function getPropertyFullAddress(Property $property)
  345.     {
  346.         $address $property->getAddress();
  347.         $address .= $property->getZipCode() ? ', '.$property->getZipCode() : '';
  348.         $address .= $property->getCity() ? ', '.$property->getCity() : '';
  349.         $address .= $property->getState() ? ', '.$property->getState() : '';
  350.         return $address;
  351.     }
  352.     public function notifySuperAdminAndRegistrant($form, array $superAdminsUser $registrant null$typeEmailNotifications $emailNotification null)
  353.     {
  354.         $property $form->getProperty();
  355.         $address $this->getPropertyFullAddress($property);
  356.         $parameters = [];
  357.         $parameters['##address##'] = $address;
  358.         $parameters['##formType##'] = ucfirst($type);
  359.         $parameters['##subject##'] = "Notification For ".ucfirst($type);
  360.         foreach ($superAdmins as $superAdmin) {
  361.             if ($superAdmin !== $registrant) {
  362.                 continue;
  363.             }
  364.             $parameters['##userName##'] = $superAdmin->getFullname();
  365.             $this->notificationEmailSender->sendNotificationMail($emailNotification$superAdmin$parameters);
  366. //            $this->notifyMunicipalSuperAdminAndRegistrant($form, $type, $superAdmin);
  367.         }
  368.         $parameters['##userName##'] = $registrant->getFullname();
  369.         $this->notificationEmailSender->sendNotificationMail($emailNotification$registrant$parameters);
  370.     }
  371. }