src/EventSubscriber/SupportSubscriber.php line 226

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\DeregistrationForm;
  4. use App\Entity\EmailNotifications;
  5. use App\Entity\Property;
  6. use App\Entity\RegistrationForm;
  7. use App\Entity\RenewalForm;
  8. use App\Event\AppEvents;
  9. use App\Event\Support\InvoiceEvent;
  10. use App\Event\Support\PropertyEvent;
  11. use App\Event\Support\RegistrationEvent;
  12. use App\Entity\Contact;
  13. use App\Entity\User;
  14. use App\Helper\InvoiceHelper;
  15. use App\Lib\EmailSenderInterface;
  16. use App\Lib\NotificationEmailSender;
  17. use App\Repository\EmailNotificationsRepository;
  18. use App\Twig\AppExtension;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. /**
  21. * Class SupportListener
  22. */
  23. class SupportSubscriber implements EventSubscriberInterface
  24. {
  25. /**
  26. * @var EmailSenderInterface
  27. */
  28. private $emailSender;
  29. /**
  30. * @var string
  31. */
  32. private $contactMail;
  33. /**
  34. * @var string
  35. */
  36. private $supportMail;
  37. private EmailNotificationsRepository $emailNotificationsRepository;
  38. private NotificationEmailSender $notificationEmailSender;
  39. private InvoiceHelper $invoiceHelper;
  40. /**
  41. * @var TwigExtension
  42. */
  43. private $twigExtension;
  44. /**
  45. * SupportListener constructor.
  46. *
  47. * @param EmailSenderInterface $emailSender
  48. * @param string $contactMail
  49. * @param string $supportMail
  50. */
  51. public function __construct(
  52. EmailSenderInterface $emailSender,
  53. $contactMail,
  54. $supportMail,
  55. EmailNotificationsRepository $emailNotificationsRepository,
  56. NotificationEmailSender $notificationEmailSender,
  57. InvoiceHelper $invoiceHelper,
  58. AppExtension $twigExtension
  59. ) {
  60. $this->emailSender = $emailSender;
  61. $this->contactMail = $contactMail;
  62. $this->supportMail = $supportMail;
  63. $this->emailNotificationsRepository = $emailNotificationsRepository;
  64. $this->notificationEmailSender = $notificationEmailSender;
  65. $this->invoiceHelper = $invoiceHelper;
  66. $this->twigExtension = $twigExtension;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public static function getSubscribedEvents(): array
  72. {
  73. return [
  74. AppEvents::INVOICE_DISPUTE_CREATED => 'onInvoiceDisputeCreated',
  75. AppEvents::INVOICE_SUPPORT_CREATED => 'onInvoiceSupportCreated',
  76. AppEvents::PROPERTY_DISPUTE_CREATED => 'onPropertyDisputeCreated',
  77. AppEvents::PROPERTY_SUPPORT_CREATED => 'onPropertySupportCreated',
  78. AppEvents::REGISTRATION_DISPUTE_CREATED => 'onRegistrationDisputeCreated',
  79. AppEvents::REGISTRATION_SUPPORT_CREATED => 'onRegistrationSupportCreated'
  80. ];
  81. }
  82. /**
  83. * @param RegistrationEvent $event
  84. */
  85. public function onRegistrationDisputeCreated(RegistrationEvent $event): void
  86. {
  87. $ticket = $event->getTicket();
  88. $form = $ticket->getForm();
  89. $formType = '';
  90. if ($form instanceof RegistrationForm) {
  91. $formType = 'Registration Form';
  92. } elseif ($form instanceof RenewalForm) {
  93. $formType = 'Renewal Form';
  94. } elseif ($form instanceof DeregistrationForm) {
  95. $formType = 'Deregistration Form';
  96. }
  97. //Send Confirmation Email to User.
  98. $user = $event->getUser();
  99. $subject = 'MuniReg - '.$formType.' Dispute Created';
  100. $this->sendRegistrationFormSupportOrDispute($form, $formType, $subject, $user);
  101. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  102. //Send Contact Email to Munireg.
  103. $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  104. $this->sendContactMessage($user, $formType.' Dispute', $contactSubject, $ticket->getMessage(), $form->getProperty());
  105. }
  106. /**
  107. * @param RegistrationEvent $event
  108. */
  109. public function onRegistrationSupportCreated(RegistrationEvent $event): void
  110. {
  111. $ticket = $event->getTicket();
  112. $form = $ticket->getForm();
  113. $formType = '';
  114. if ($form instanceof RegistrationForm) {
  115. $formType = 'Registration Form';
  116. } elseif ($form instanceof RenewalForm) {
  117. $formType = 'Renewal Form';
  118. } elseif ($form instanceof DeregistrationForm) {
  119. $formType = 'Deregistration Form';
  120. }
  121. //Send Confirmation Email to User.
  122. $user = $event->getUser();
  123. $subject = 'MuniReg - '.$formType.' Support Created';
  124. $this->sendRegistrationFormSupportOrDispute($form, $formType, $subject, $user);
  125. //$this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  126. //Send Contact Email to Munireg.
  127. $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  128. $this->sendContactMessage($user, $formType.' Support', $contactSubject, $ticket->getMessage(), $form->getProperty());
  129. }
  130. /**
  131. * @param PropertyEvent $event
  132. */
  133. public function onPropertyDisputeCreated(PropertyEvent $event): void
  134. {
  135. $ticket = $event->getTicket();
  136. //Send Confirmation Email to User.
  137. $user = $event->getUser();
  138. $viewName = 'Support/property_confirmation.html.twig';
  139. $emailNotification = $this->getEmailNotification('property_support');
  140. $subject = 'MuniReg - Property Dispute Created';
  141. $parameters = [
  142. '##userName##' => $user->getFullname(),
  143. '##supportType##' => 'Support Ticket',
  144. '##address##' => $this->getPropertyFullAddress($ticket->getProperty()),
  145. '##subject##' => $subject,
  146. ];
  147. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  148. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  149. //Send Contact Email to Munireg.
  150. $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  151. $this->sendContactMessage($user, 'Property Dispute', $contactSubject, $ticket->getMessage());
  152. }
  153. /**
  154. * @param PropertyEvent $event
  155. */
  156. public function onPropertySupportCreated(PropertyEvent $event): void
  157. {
  158. $ticket = $event->getTicket();
  159. //Send Confirmation Email to User.
  160. $user = $event->getUser();
  161. $viewName = 'Support/property_confirmation.html.twig';
  162. $emailNotification = $this->getEmailNotification('property_support');
  163. $subject = 'MuniReg - Property Support Created';
  164. $parameters = [
  165. '##userName##' => $user->getFullname(),
  166. '##supportType##' => 'Support Ticket',
  167. '##address##' => $this->getPropertyFullAddress($ticket->getProperty()),
  168. '##subject##' => $subject,
  169. ];
  170. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  171. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  172. //Send Contact Email to Munireg.
  173. $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  174. $this->sendContactMessage($user, 'Property Support', $contactSubject, $ticket->getMessage(), $ticket->getProperty());
  175. }
  176. /**
  177. * @param InvoiceEvent $event
  178. */
  179. public function onInvoiceDisputeCreated(InvoiceEvent $event): void
  180. {
  181. $ticket = $event->getTicket();
  182. //Send Confirmation Email to User.
  183. $user = $event->getUser();
  184. $viewName = 'Support/invoice_confirmation.html.twig';
  185. $emailNotification = $this->getEmailNotification('invoice_support');
  186. $subject = 'MuniReg - Invoice Dispute Created';
  187. $parameters = [
  188. '##userName##' => $user->getFullname(),
  189. '##invoiceType##' => $this->invoiceHelper->getInvoiceTypeLabel($ticket->getInvoice()->getType()),
  190. '##invoiceId##' => '#'.$ticket->getInvoice()->getId(),
  191. '##supportType##' => 'Dispute',
  192. '##subject##' => $subject
  193. ];
  194. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  195. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  196. //Send Contact Email to Munireg.
  197. $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  198. $this->sendContactMessage($user, 'Invoice Dispute', $contactSubject, $ticket->getMessage());
  199. }
  200. /**
  201. * @param InvoiceEvent $event
  202. */
  203. public function onInvoiceSupportCreated(InvoiceEvent $event): void
  204. {
  205. $ticket = $event->getTicket();
  206. //Send Confirmation Email to User.
  207. $user = $event->getUser();
  208. $emailNotification = $this->getEmailNotification('invoice_support');
  209. $invoiceTypeLabel = $this->twigExtension->getInvoiceTypeLabel($ticket->getInvoice()->getType());
  210. $viewName = 'Support/invoice_confirmation.html.twig';
  211. $parameters = [
  212. 'user' => $user,
  213. 'invoice' => $ticket->getInvoice(),
  214. 'supportType' => 'Support Ticket',
  215. 'invoiceTypeLabel' => $invoiceTypeLabel
  216. ];
  217. $subject = 'MuniReg - Invoice Support Created';
  218. $parameters = [
  219. '##userName##' => $user->getFullname(),
  220. '##invoiceType##' => $this->invoiceHelper->getInvoiceTypeLabel($ticket->getInvoice()->getType()),
  221. '##invoiceId##' => '#'.$ticket->getInvoice()->getId(),
  222. '##supportType##' => 'Support Ticket',
  223. '##subject##' => $subject
  224. ];
  225. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  226. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  227. //Send Contact Email to Munireg.
  228. $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  229. $this->sendContactMessage($user, 'Invoice Support', $contactSubject, $ticket->getMessage());
  230. }
  231. /**
  232. * @param User $user
  233. * @param string $type
  234. * @param string $subject
  235. * @param string $message
  236. */
  237. private function sendContactMessage(User $user, string $type, string $subject, string $message, $property = null): void
  238. {
  239. $parameters = [];
  240. /** @var EmailNotifications $emailNotification*/
  241. $emailNotification = $this->getEmailNotification('munireg_support_contact');
  242. if (!$property instanceof Property) {
  243. $body = $emailNotification->getBody();
  244. $newBody = str_replace('<p>Property Address: <strong>##address##</strong></p>', '', $body);
  245. $emailNotification->setBody($newBody);
  246. } else {
  247. $parameters['##address##'] = $this->getPropertyFullAddress($property);
  248. }
  249. $contact = new Contact($user, $subject, $message);
  250. $parameters['##subject##'] = $subject;
  251. $parameters['##contactType##'] = $type;
  252. $parameters['##userName##'] = $user->getType();
  253. $parameters['##contactSubject##'] = $contact->getSubject();
  254. $parameters['##message##'] = $contact->getMessage();
  255. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  256. // $viewName = 'Support/munireg_contact.html.twig';
  257. // $parameters = [ 'contactType' => $type, 'contact' => $contact, 'property' => $property];
  258. // $this->emailSender->sendEmail($viewName, $parameters, $subject, $this->supportMail);
  259. }
  260. public function getEmailNotification($type)
  261. {
  262. return $this->emailNotificationsRepository->findOneBy([
  263. 'notificationType' => $type
  264. ]);
  265. }
  266. public function getPropertyFullAddress(Property $property): string
  267. {
  268. $address = $property->getAddress();
  269. $address .= $property->getZipCode() ? ', '.$property->getZipCode() : '';
  270. $address .= $property->getCity() ? ', '.$property->getCity() : '';
  271. $address .= $property->getState() ? ', '.$property->getState() : '';
  272. return $address;
  273. }
  274. public function sendRegistrationFormSupportOrDispute($form, $formType, $subject, $user): void
  275. {
  276. $emailNotification = $this->getEmailNotification('registration_support');
  277. $parameters = [];
  278. $parameters['##subject##'] = $subject;
  279. $parameters['##userName##'] = $user->getFullname();
  280. $parameters['##supportType##'] = 'Support Ticket';
  281. $parameters['##formType##'] = $formType;
  282. $parameters['##address##'] = $this->getPropertyFullAddress($form->getProperty());
  283. $this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
  284. }
  285. }