src/EventSubscriber/SendConsolidatedInvoiceEventSubscriber.php line 27

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\SendCodWithdrawalEmailEvent;
  4. use App\Event\SendConsolidatedInvoiceEvent;
  5. use App\Event\SendEmailEvent;
  6. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class SendConsolidatedInvoiceEventSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private  EventDispatcherInterface $eventDispatcher,
  11.                                 private string $fromEmail, private string $fromName)
  12.     {
  13.     }
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             SendConsolidatedInvoiceEvent::NAME => [
  18.                 ['handleSendConsolidatedInvoice'10]
  19.             ]
  20.         ];
  21.     }
  22.     public function handleSendConsolidatedInvoice(SendConsolidatedInvoiceEvent $event)
  23.     {
  24.         $consolidatedInvoice $event->getInvoice();
  25.         $attachment $event->getAttachment();
  26.         $customer $event->getCustomer();
  27.         $emailParameters = [
  28.             'from' => [
  29.                 'email' => $this->fromEmail,
  30.                 'name' => $this->fromName
  31.             ],
  32.             'to' => [
  33.                 'email' => $customer->getEmail()
  34.             ],
  35.             'subject' => 'Statement and Invoice for  '.$consolidatedInvoice->getItems()->first()->getItemName(),
  36.             'template' => 'email/consolidated_statement.html.twig',
  37.             'parameters' => [
  38.                 'customer' => $customer,
  39.                 'itemName' => $consolidatedInvoice->getItems()->first()->getItemName(),
  40.                 'invoice' => $consolidatedInvoice,
  41.             ],
  42.             'attachment' => $attachment
  43.         ];
  44.         $emailEvent =  new SendEmailEvent($emailParameters);
  45.         $this->eventDispatcher->dispatch($emailEventSendEmailEvent::NAME);
  46.     }
  47. }