src/EventSubscriber/SupportSubscriber.php line 142

Open in your IDE?
  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()
  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)
  86.     {
  87.         $ticket $event->getTicket();
  88.         $form $ticket->getForm();
  89.         if ($form instanceof RegistrationForm) {
  90.             $formType 'Registration Form';
  91.         } elseif ($form instanceof RenewalForm) {
  92.             $formType 'Renewal Form';
  93.         } elseif ($form instanceof DeregistrationForm) {
  94.             $formType 'Deregistration Form';
  95.         }
  96.         //Send Confirmation Email to User.
  97.         $user $event->getUser();
  98.         $subject 'MuniReg - '.$formType.' Dispute Created';
  99.         $this->sendRegistrationFormSupportOrDispute($form$formType$subject$user);
  100. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  101.         //Send Contact Email to Munireg.
  102.         $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  103.         $this->sendContactMessage($user$formType.' Dispute'$contactSubject$ticket->getMessage(), $form->getProperty());
  104.     }
  105.     /**
  106.      * @param RegistrationEvent $event
  107.      */
  108.     public function onRegistrationSupportCreated(RegistrationEvent $event)
  109.     {
  110.         $ticket $event->getTicket();
  111.         $form $ticket->getForm();
  112.         if ($form instanceof RegistrationForm) {
  113.             $formType 'Registration Form';
  114.         } elseif ($form instanceof RenewalForm) {
  115.             $formType 'Renewal Form';
  116.         } elseif ($form instanceof DeregistrationForm) {
  117.             $formType 'Deregistration Form';
  118.         }
  119.         //Send Confirmation Email to User.
  120.         $user $event->getUser();
  121.         $subject 'MuniReg - '.$formType.' Support Created';
  122.         $this->sendRegistrationFormSupportOrDispute($form$formType$subject$user);
  123.         //$this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  124.         //Send Contact Email to Munireg.
  125.         $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  126.         $this->sendContactMessage($user$formType.' Support'$contactSubject$ticket->getMessage(), $form->getProperty());
  127.     }
  128.     /**
  129.      * @param PropertyEvent $event
  130.      */
  131.     public function onPropertyDisputeCreated(PropertyEvent $event)
  132.     {
  133.         $ticket $event->getTicket();
  134.         //Send Confirmation Email to User.
  135.         $user $event->getUser();
  136.         $viewName 'Support/property_confirmation.html.twig';
  137.         $emailNotification $this->getEmailNotification('property_support');
  138.         $subject 'MuniReg - Property Dispute Created';
  139.         $parameters = [
  140.             '##userName##' => $user->getFullname(),
  141.             '##supportType##' => 'Support Ticket',
  142.             '##address##' => $this->getPropertyFullAddress($ticket->getProperty()),
  143.             '##subject##' => $subject,
  144.         ];
  145.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  146. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  147.         //Send Contact Email to Munireg.
  148.         $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  149.         $this->sendContactMessage($user'Property Dispute'$contactSubject$ticket->getMessage());
  150.     }
  151.     /**
  152.      * @param PropertyEvent $event
  153.      */
  154.     public function onPropertySupportCreated(PropertyEvent $event)
  155.     {
  156.         $ticket $event->getTicket();
  157.         //Send Confirmation Email to User.
  158.         $user $event->getUser();
  159.         $viewName 'Support/property_confirmation.html.twig';
  160.         $emailNotification $this->getEmailNotification('property_support');
  161.         $subject 'MuniReg - Property Support Created';
  162.         $parameters = [
  163.             '##userName##' => $user->getFullname(),
  164.             '##supportType##' => 'Support Ticket',
  165.             '##address##' => $this->getPropertyFullAddress($ticket->getProperty()),
  166.             '##subject##' => $subject,
  167.         ];
  168.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  169. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  170.         //Send Contact Email to Munireg.
  171.         $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  172.         $this->sendContactMessage($user'Property Support'$contactSubject$ticket->getMessage(), $ticket->getProperty());
  173.     }
  174.     /**
  175.      * @param InvoiceEvent $event
  176.      */
  177.     public function onInvoiceDisputeCreated(InvoiceEvent $event)
  178.     {
  179.         $ticket $event->getTicket();
  180.         //Send Confirmation Email to User.
  181.         $user $event->getUser();
  182.         $viewName 'Support/invoice_confirmation.html.twig';
  183.         $emailNotification $this->getEmailNotification('invoice_support');
  184.         $subject 'MuniReg - Invoice Dispute Created';
  185.         $parameters = [
  186.             '##userName##' => $user->getFullname(),
  187.             '##invoiceType##' => $this->invoiceHelper->getInvoiceTypeLabel($ticket->getInvoice()->getType()),
  188.             '##invoiceId##' => '#'.$ticket->getInvoice()->getId(),
  189.             '##supportType##' => 'Dispute',
  190.             '##subject##' => $subject
  191.         ];
  192.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  193. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  194.         //Send Contact Email to Munireg.
  195.         $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  196.         $this->sendContactMessage($user'Invoice Dispute'$contactSubject$ticket->getMessage());
  197.     }
  198.     /**
  199.      * @param InvoiceEvent $event
  200.      */
  201.     public function onInvoiceSupportCreated(InvoiceEvent $event)
  202.     {
  203.         $ticket $event->getTicket();
  204.         //Send Confirmation Email to User.
  205.         $user $event->getUser();
  206.         $emailNotification $this->getEmailNotification('invoice_support');
  207.         $invoiceTypeLabel $this->twigExtension->getInvoiceTypeLabel($ticket->getInvoice()->getType());
  208.         $viewName 'Support/invoice_confirmation.html.twig';
  209.         $parameters = [
  210.             'user' => $user,
  211.             'invoice' => $ticket->getInvoice(),
  212.             'supportType' => 'Support Ticket',
  213.             'invoiceTypeLabel' => $invoiceTypeLabel
  214.         ];
  215.         $subject 'MuniReg - Invoice Support Created';
  216.         $parameters = [
  217.             '##userName##' => $user->getFullname(),
  218.             '##invoiceType##' => $this->invoiceHelper->getInvoiceTypeLabel($ticket->getInvoice()->getType()),
  219.             '##invoiceId##' => '#'.$ticket->getInvoice()->getId(),
  220.             '##supportType##' => 'Support Ticket',
  221.             '##subject##' => $subject
  222.         ];
  223.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  224. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $user->getEmail());
  225.         //Send Contact Email to Munireg.
  226.         $contactSubject = ($ticket->getSubject() != '') ? $subject.' - '.$ticket->getSubject() : $subject;
  227.         $this->sendContactMessage($user'Invoice Support'$contactSubject$ticket->getMessage());
  228.     }
  229.     /**
  230.      * @param User   $user
  231.      * @param string $type
  232.      * @param string $subject
  233.      * @param string $message
  234.      */
  235.     private function sendContactMessage(User $user$type$subject$message$property null)
  236.     {
  237.         $parameters = [];
  238.         /** @var EmailNotifications $emailNotification*/
  239.         $emailNotification $this->getEmailNotification('munireg_support_contact');
  240.         if (!$property instanceof Property) {
  241.             $body $emailNotification->getBody();
  242.             $newBody str_replace('<p>Property Address: <strong>##address##</strong></p>'''$body);
  243.             $emailNotification->setBody($newBody);
  244.         } else {
  245.             $parameters['##address##'] = $this->getPropertyFullAddress($property);
  246.         }
  247.         $contact = new Contact($user$subject$message);
  248.         $parameters['##subject##'] = $subject;
  249.         $parameters['##contactType##'] = $type;
  250.         $parameters['##userName##'] = $user->getType();
  251.         $parameters['##contactSubject##'] = $contact->getSubject();
  252.         $parameters['##message##'] = $contact->getMessage();
  253.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  254. //        $viewName = 'Support/munireg_contact.html.twig';
  255. //        $parameters = [ 'contactType' => $type, 'contact' => $contact, 'property' => $property];
  256. //        $this->emailSender->sendEmail($viewName, $parameters, $subject, $this->supportMail);
  257.     }
  258.     public function getEmailNotification($type)
  259.     {
  260.         return $this->emailNotificationsRepository->findOneBy([
  261.             'notificationType' => $type
  262.         ]);
  263.     }
  264.     public function getPropertyFullAddress(Property $property)
  265.     {
  266.         $address $property->getAddress();
  267.         $address .= $property->getZipCode() ? ', '.$property->getZipCode() : '';
  268.         $address .= $property->getCity() ? ', '.$property->getCity() : '';
  269.         $address .= $property->getState() ? ', '.$property->getState() : '';
  270.         return $address;
  271.     }
  272.     public function sendRegistrationFormSupportOrDispute($form$formType$subject$user)
  273.     {
  274.         $emailNotification $this->getEmailNotification('registration_support');
  275.         $parameters = [];
  276.         $parameters['##subject##'] = $subject;
  277.         $parameters['##userName##'] = $user->getFullname();
  278.         $parameters['##supportType##'] = 'Support Ticket';
  279.         $parameters['##formType##'] = $formType;
  280.         $parameters['##address##'] = $this->getPropertyFullAddress($form->getProperty());
  281.         $this->notificationEmailSender->sendNotificationMail($emailNotification$user$parameters);
  282.     }
  283. }