<?php
namespace App\EventSubscriber;
use App\Entity\DeregistrationForm;
use App\Entity\EmailNotifications;
use App\Entity\Property;
use App\Entity\RegistrationForm;
use App\Entity\RenewalForm;
use App\Event\AppEvents;
use App\Event\Support\InvoiceEvent;
use App\Event\Support\PropertyEvent;
use App\Event\Support\RegistrationEvent;
use App\Entity\Contact;
use App\Entity\User;
use App\Helper\InvoiceHelper;
use App\Lib\EmailSenderInterface;
use App\Lib\NotificationEmailSender;
use App\Repository\EmailNotificationsRepository;
use App\Twig\AppExtension;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Class SupportListener
*/
class SupportSubscriber implements EventSubscriberInterface
{
/**
* @var EmailSenderInterface
*/
private $emailSender;
/**
* @var string
*/
private $contactMail;
/**
* @var string
*/
private $supportMail;
private EmailNotificationsRepository $emailNotificationsRepository;
private NotificationEmailSender $notificationEmailSender;
private InvoiceHelper $invoiceHelper;
/**
* @var TwigExtension
*/
private $twigExtension;
/**
* SupportListener constructor.
*
* @param EmailSenderInterface $emailSender
* @param string $contactMail
* @param string $supportMail
*/
public function __construct(
EmailSenderInterface $emailSender,
$contactMail,
$supportMail,
EmailNotificationsRepository $emailNotificationsRepository,
NotificationEmailSender $notificationEmailSender,
InvoiceHelper $invoiceHelper,
AppExtension $twigExtension
) {
$this->emailSender = $emailSender;
$this->contactMail = $contactMail;
$this->supportMail = $supportMail;
$this->emailNotificationsRepository = $emailNotificationsRepository;
$this->notificationEmailSender = $notificationEmailSender;
$this->invoiceHelper = $invoiceHelper;
$this->twigExtension = $twigExtension;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
AppEvents::INVOICE_DISPUTE_CREATED => 'onInvoiceDisputeCreated',
AppEvents::INVOICE_SUPPORT_CREATED => 'onInvoiceSupportCreated',
AppEvents::PROPERTY_DISPUTE_CREATED => 'onPropertyDisputeCreated',
AppEvents::PROPERTY_SUPPORT_CREATED => 'onPropertySupportCreated',
AppEvents::REGISTRATION_DISPUTE_CREATED => 'onRegistrationDisputeCreated',
AppEvents::REGISTRATION_SUPPORT_CREATED => 'onRegistrationSupportCreated'
];
}
/**
* @param RegistrationEvent $event
*/
public function onRegistrationDisputeCreated(RegistrationEvent $event)
{
$ticket = $event->getTicket();
$form = $ticket->getForm();
if ($form instanceof RegistrationForm) {
$formType = 'Registration Form';
} elseif ($form instanceof RenewalForm) {
$formType = 'Renewal Form';
} elseif ($form instanceof DeregistrationForm) {
$formType = 'Deregistration Form';
}
//Send Confirmation Email to User.
$user = $event->getUser();
$subject = 'MuniReg - '.$formType.' Dispute Created';
$this->sendRegistrationFormSupportOrDispute($form, $formType, $subject, $user);
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
//Send Contact Email to Munireg.
$contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
$this->sendContactMessage($user, $formType.' Dispute', $contactSubject, $ticket->getMessage(), $form->getProperty());
}
/**
* @param RegistrationEvent $event
*/
public function onRegistrationSupportCreated(RegistrationEvent $event)
{
$ticket = $event->getTicket();
$form = $ticket->getForm();
if ($form instanceof RegistrationForm) {
$formType = 'Registration Form';
} elseif ($form instanceof RenewalForm) {
$formType = 'Renewal Form';
} elseif ($form instanceof DeregistrationForm) {
$formType = 'Deregistration Form';
}
//Send Confirmation Email to User.
$user = $event->getUser();
$subject = 'MuniReg - '.$formType.' Support Created';
$this->sendRegistrationFormSupportOrDispute($form, $formType, $subject, $user);
//$this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
//Send Contact Email to Munireg.
$contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
$this->sendContactMessage($user, $formType.' Support', $contactSubject, $ticket->getMessage(), $form->getProperty());
}
/**
* @param PropertyEvent $event
*/
public function onPropertyDisputeCreated(PropertyEvent $event)
{
$ticket = $event->getTicket();
//Send Confirmation Email to User.
$user = $event->getUser();
$viewName = 'Support/property_confirmation.html.twig';
$emailNotification = $this->getEmailNotification('property_support');
$subject = 'MuniReg - Property Dispute Created';
$parameters = [
'##userName##' => $user->getFullname(),
'##supportType##' => 'Support Ticket',
'##address##' => $this->getPropertyFullAddress($ticket->getProperty()),
'##subject##' => $subject,
];
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
//Send Contact Email to Munireg.
$contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
$this->sendContactMessage($user, 'Property Dispute', $contactSubject, $ticket->getMessage());
}
/**
* @param PropertyEvent $event
*/
public function onPropertySupportCreated(PropertyEvent $event)
{
$ticket = $event->getTicket();
//Send Confirmation Email to User.
$user = $event->getUser();
$viewName = 'Support/property_confirmation.html.twig';
$emailNotification = $this->getEmailNotification('property_support');
$subject = 'MuniReg - Property Support Created';
$parameters = [
'##userName##' => $user->getFullname(),
'##supportType##' => 'Support Ticket',
'##address##' => $this->getPropertyFullAddress($ticket->getProperty()),
'##subject##' => $subject,
];
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
//Send Contact Email to Munireg.
$contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
$this->sendContactMessage($user, 'Property Support', $contactSubject, $ticket->getMessage(), $ticket->getProperty());
}
/**
* @param InvoiceEvent $event
*/
public function onInvoiceDisputeCreated(InvoiceEvent $event)
{
$ticket = $event->getTicket();
//Send Confirmation Email to User.
$user = $event->getUser();
$viewName = 'Support/invoice_confirmation.html.twig';
$emailNotification = $this->getEmailNotification('invoice_support');
$subject = 'MuniReg - Invoice Dispute Created';
$parameters = [
'##userName##' => $user->getFullname(),
'##invoiceType##' => $this->invoiceHelper->getInvoiceTypeLabel($ticket->getInvoice()->getType()),
'##invoiceId##' => '#'.$ticket->getInvoice()->getId(),
'##supportType##' => 'Dispute',
'##subject##' => $subject
];
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
//Send Contact Email to Munireg.
$contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
$this->sendContactMessage($user, 'Invoice Dispute', $contactSubject, $ticket->getMessage());
}
/**
* @param InvoiceEvent $event
*/
public function onInvoiceSupportCreated(InvoiceEvent $event)
{
$ticket = $event->getTicket();
//Send Confirmation Email to User.
$user = $event->getUser();
$emailNotification = $this->getEmailNotification('invoice_support');
$invoiceTypeLabel = $this->twigExtension->getInvoiceTypeLabel($ticket->getInvoice()->getType());
$viewName = 'Support/invoice_confirmation.html.twig';
$parameters = [
'user' => $user,
'invoice' => $ticket->getInvoice(),
'supportType' => 'Support Ticket',
'invoiceTypeLabel' => $invoiceTypeLabel
];
$subject = 'MuniReg - Invoice Support Created';
$parameters = [
'##userName##' => $user->getFullname(),
'##invoiceType##' => $this->invoiceHelper->getInvoiceTypeLabel($ticket->getInvoice()->getType()),
'##invoiceId##' => '#'.$ticket->getInvoice()->getId(),
'##supportType##' => 'Support Ticket',
'##subject##' => $subject
];
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
//Send Contact Email to Munireg.
$contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
$this->sendContactMessage($user, 'Invoice Support', $contactSubject, $ticket->getMessage());
}
/**
* @param User $user
* @param string $type
* @param string $subject
* @param string $message
*/
private function sendContactMessage(User $user, $type, $subject, $message, $property = null)
{
$parameters = [];
/** @var EmailNotifications $emailNotification*/
$emailNotification = $this->getEmailNotification('munireg_support_contact');
if (!$property instanceof Property) {
$body = $emailNotification->getBody();
$newBody = str_replace('<p>Property Address: <strong>##address##</strong></p>', '', $body);
$emailNotification->setBody($newBody);
} else {
$parameters['##address##'] = $this->getPropertyFullAddress($property);
}
$contact = new Contact($user, $subject, $message);
$parameters['##subject##'] = $subject;
$parameters['##contactType##'] = $type;
$parameters['##userName##'] = $user->getType();
$parameters['##contactSubject##'] = $contact->getSubject();
$parameters['##message##'] = $contact->getMessage();
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
// $viewName = 'Support/munireg_contact.html.twig';
// $parameters = [ 'contactType' => $type, 'contact' => $contact, 'property' => $property];
// $this->emailSender->sendEmail($viewName, $parameters, $subject, $this->supportMail);
}
public function getEmailNotification($type)
{
return $this->emailNotificationsRepository->findOneBy([
'notificationType' => $type
]);
}
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 sendRegistrationFormSupportOrDispute($form, $formType, $subject, $user)
{
$emailNotification = $this->getEmailNotification('registration_support');
$parameters = [];
$parameters['##subject##'] = $subject;
$parameters['##userName##'] = $user->getFullname();
$parameters['##supportType##'] = 'Support Ticket';
$parameters['##formType##'] = $formType;
$parameters['##address##'] = $this->getPropertyFullAddress($form->getProperty());
$this->notificationEmailSender->sendNotificationMail($emailNotification, $user, $parameters);
}
}