src/EventSubscriber/SendMisReportEmailEventSubscriber.php line 28

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\SendEmailEvent;
  4. use App\Event\SendMisReportEmailEvent;
  5. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class SendMisReportEmailEventSubscriber implements EventSubscriberInterface
  8. {
  9.     public function __construct(private  EventDispatcherInterface $eventDispatcher,
  10.                                 private string $fromEmail, private string $fromName)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             SendMisReportEmailEvent::NAME => [
  17.                 ['handleMisReportEmailSend'10]
  18.             ]
  19.         ];
  20.     }
  21.     public function handleMisReportEmailSend(SendMisReportEmailEvent $event): void
  22.     {
  23.         $customer $event->getCustomer();
  24.         $attachment $event->getAttachment();
  25.         $adminEmails $event->getAdminEmails();
  26.         $fromDate $event->getFromDate();
  27.         $toDate $event->getToDate();
  28.         $date = new \DateTime('now');
  29.         $yesterDay $date->sub(new \DateInterval('P1D'));
  30.         $yesterDay $yesterDay->format('d-m-Y');
  31.         $toEmails  = [];
  32.         if(!is_null($customer)) {
  33.             foreach($customer->getCompany()->getContacts() as $contact)
  34.             {
  35.                 if($contact->isEmailNotificationEnabled())
  36.                 {
  37.                     $toEmails[] = $contact->getEmail();
  38.                 }
  39.             }
  40.         }
  41.         $adminEmails = !is_null($adminEmails) ? explode(','$adminEmails): [];
  42.         $toEmails array_merge($toEmails$adminEmails);
  43.         dump($adminEmails);
  44.         if(!empty($toEmails)) {
  45.             $emailParameters = [
  46.                 'from' => [
  47.                     'email' => $this->fromEmail,
  48.                     'name' => $this->fromName
  49.                 ],
  50.                 'to' => [
  51.                     'email' => $toEmails,
  52.                 ],
  53.                 'subject' => 'Shipments from '.$fromDate->format('d-m-Y').' to '.$toDate->format('d-m-Y'),
  54.                 'template' => 'email/orders_mis_report.html.twig',
  55.                 'parameters' => [
  56.                     'customerName' => !is_null($customer) ? $customer->getFirstname() : '',
  57.                     'fromDate' => $fromDate->format('d-m-Y'),
  58.                     'toDate' => $toDate->format('d-m-Y'),
  59.                 ],
  60.                 'attachment' => $attachment
  61.             ];
  62.             $emailEvent =  new SendEmailEvent($emailParameters);
  63.             $this->eventDispatcher->dispatch($emailEventSendEmailEvent::NAME);
  64.         }
  65.     }
  66. }