src/EventSubscriber/BulkOrderCompletedEmailEventSubscriber.php line 28

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Event\BulkOrderCompletedEmailEvent;
  4. use App\Event\SendEmailEvent;
  5. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class BulkOrderCompletedEmailEventSubscriber 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.             BulkOrderCompletedEmailEvent::NAME => [
  17.                 ['handleEmailSend'10]
  18.             ]
  19.         ];
  20.     }
  21.     public function handleEmailSend(BulkOrderCompletedEmailEvent $event): void
  22.     {
  23.         $customer $event->getCustomer();
  24.         $bulkOrder $event->getBulkOrder();
  25. //        $date = new \DateTime('now');
  26. //        $yesterDay = $date->sub(new \DateInterval('P1D'));
  27. //        $yesterDay = $yesterDay->format('d-m-Y');
  28.         $toEmails  = [];
  29.         foreach($customer->getCompany()->getContacts() as $contact)
  30.         {
  31.             if($contact->isEmailNotificationEnabled())
  32.             {
  33.                 $toEmails[] = $contact->getEmail();
  34.             }
  35.         }
  36.         dump($toEmails);
  37.         dump('coming here');
  38.         if(!empty($toEmails)) {
  39.             $emailParameters = [
  40.                 'from' => [
  41.                     'email' => $this->fromEmail,
  42.                     'name' => $this->fromName
  43.                 ],
  44.                 'to' => [
  45.                     'email' => $toEmails,
  46.                 ],
  47.                 'subject' => 'Bulk Order Details',
  48.                 'template' => 'email/bulk_order_completed.html.twig',
  49.                 'parameters' => [
  50.                     'customer' => $customer,
  51.                     'data' => $event->getData()
  52.                 ],
  53.                 'attachment' => $bulkOrder->getOutFile()
  54.             ];
  55.             $emailEvent =  new SendEmailEvent($emailParameters);
  56.             $this->eventDispatcher->dispatch($emailEventSendEmailEvent::NAME);
  57.         }
  58.     }
  59. }