src/EventSubscriber/NotificationSubscriber.php line 246

  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 bool ENABLED = true;
  24. const string 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(): array
  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): void
  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): void
  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, ucfirst(UserNotificationTypes::REGISTRATIONS), $registeredUser, $emailNotification);
  122. }
  123. /**
  124. * @param FormEvent $event
  125. */
  126. public function onDeregistrationFormCreated(FormEvent $event): void
  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, ucfirst(UserNotificationTypes::DEREGISTRATIONS), $registeredUser, $emailNotification);
  156. }
  157. /**
  158. * @param FormEvent $event
  159. */
  160. public function onRenewalCreated(FormEvent $event): void
  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, ucfirst(UserNotificationTypes::RENEWALS), $registeredUser, $emailNotification);
  190. }
  191. /**
  192. * @param FormEvent $event
  193. */
  194. public function onRenewalFormUpcoming(FormEvent $event): void
  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. * @param FormEvent $event
  220. */
  221. public function onRenewalFormDue(FormEvent $event): void
  222. {
  223. /** @var RenewalForm $form */
  224. $form = $event->getForm();
  225. $viewName = 'Notification/due_renewal_notification.html.twig';
  226. $property = $form->getProperty();
  227. $notifications = $this->entityManager->getRepository(UserNotification::class)->findBy([
  228. 'type' => UserNotificationTypes::DUE_RENEWALS,
  229. 'value' => self::ENABLED
  230. ]);
  231. $emailNotification = $this->getEmailNotification(UserNotificationTypes::DUE_RENEWALS);
  232. foreach ($notifications as $notification) {
  233. /** @var UserNotification $notification */
  234. $user = $notification->getUser();
  235. if ($this->shouldNotifyDueRenewal($user, $form))
  236. {
  237. $address = $this->getPropertyFullAddress($property);
  238. $parameters = [];
  239. $parameters['##userName##'] = $user->getFullname();
  240. $parameters['##address##'] = $address;
  241. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  242. // $this->notifyRegister($form->getProperty(), $user, $viewName, 'Due Renewal');
  243. }
  244. }
  245. }
  246. /**
  247. * @param User $user
  248. * @param Property $property
  249. *
  250. * @return bool
  251. */
  252. private function shouldNotifyMunicipal(User $user, Property $property): bool
  253. {
  254. if ($user->getType() === UserTypes::MUNICIPALITY) {
  255. return ($property->getMunicipality() === $user->getMunicipality());
  256. }
  257. return false;
  258. }
  259. /**
  260. * @param User $user
  261. * @param Property $property
  262. *
  263. * @return bool
  264. */
  265. private function shouldNotifyRegister(User $user, Property $property): bool
  266. {
  267. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  268. $isOwner = ($property->getRegistrant() === $user->getOrganization());
  269. $requireRegistration = ($property->getRegistrationDate() == null);
  270. return ($isOwner && $requireRegistration);
  271. }
  272. return false;
  273. }
  274. /**
  275. * @param User $user
  276. * @param RenewalForm $renewal
  277. *
  278. * @return bool
  279. */
  280. private function shouldNotifyUpcomingRenewal(User $user, RenewalForm $renewal): bool
  281. {
  282. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  283. return ($renewal->getRegistrant() === $user->getOrganization());
  284. }
  285. return false;
  286. }
  287. /**
  288. * @param User $user
  289. * @param RenewalForm $renewal
  290. *
  291. * @return bool
  292. */
  293. private function shouldNotifyDueRenewal(User $user, RenewalForm $renewal): bool
  294. {
  295. if ($user->getType() === UserTypes::REGISTER_PARTY) {
  296. $isOwner = ($renewal->getRegistrant() === $user->getOrganization());;
  297. return ($isOwner && $renewal->hasDue());
  298. }
  299. return false;
  300. }
  301. // /**
  302. // * @param Form $form
  303. // * @param $formType
  304. // * @param User $user
  305. // */
  306. // private function notifyMunicipalSuperAdminAndRegistrant(Form $form, $formType, User $user)
  307. // {
  308. // $viewName = 'Notification/form_notification.html.twig';
  309. // $subject = 'Notification for '.$formType;
  310. // $parameters = [
  311. // 'property' => $form->getProperty(),
  312. // 'user' => $user,
  313. // 'formType' => $formType
  314. // ];
  315. //
  316. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  317. // }
  318. /**
  319. * @param Property $property
  320. * @param $viewName
  321. * @param User $user
  322. * @param $subject
  323. */
  324. private function notifyRegister(Property $property, User $user, $viewName, $subject): void
  325. {
  326. $parameters = [
  327. 'property' => $property,
  328. 'user' => $user,
  329. ];
  330. $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  331. }
  332. public function getSuperAdmins(): array
  333. {
  334. return $this->entityManager->getRepository(User::class)->findBy(['isSuperAdmin' => true, 'isVerified' => true]);
  335. }
  336. public function getEmailNotification($type = null)
  337. {
  338. return $this->entityManager->getRepository(EmailNotifications::class)->findOneBy([
  339. 'notificationType' => $type != null ? $type : self::FORM_NOTIFICATION
  340. ]);
  341. }
  342. public function getPropertyFullAddress(Property $property): string
  343. {
  344. $address = $property->getAddress();
  345. $address .= $property->getZipCode() ? ', '.$property->getZipCode() : '';
  346. $address .= $property->getCity() ? ', '.$property->getCity() : '';
  347. $address .= $property->getState() ? ', '.$property->getState() : '';
  348. return $address;
  349. }
  350. public function notifySuperAdminAndRegistrant($form, array $superAdmins, $type, ?User $registrant = null, ?EmailNotifications $emailNotification = null): void
  351. {
  352. $property = $form->getProperty();
  353. $address = $this->getPropertyFullAddress($property);
  354. $parameters = [];
  355. $parameters['##address##'] = $address;
  356. $parameters['##formType##'] = ucfirst($type);
  357. $parameters['##subject##'] = "Notification For ".ucfirst($type);
  358. foreach ($superAdmins as $superAdmin) {
  359. if ($superAdmin !== $registrant) {
  360. continue;
  361. }
  362. $parameters['##userName##'] = $superAdmin->getFullname();
  363. $this->notificationEmailSender->sendNotificationMail($emailNotification, $superAdmin, $parameters);
  364. // $this->notifyMunicipalSuperAdminAndRegistrant($form, $type, $superAdmin);
  365. }
  366. $parameters['##userName##'] = $registrant->getFullname();
  367. $this->notificationEmailSender->sendNotificationMail($emailNotification, $registrant, $parameters);
  368. }
  369. }