src/EventSubscriber/SendMisReportEmailEventSubscriber.php line 28
<?php
namespace App\EventSubscriber;
use App\Event\SendEmailEvent;
use App\Event\SendMisReportEmailEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendMisReportEmailEventSubscriber implements EventSubscriberInterface
{
public function __construct(private EventDispatcherInterface $eventDispatcher,
private string $fromEmail, private string $fromName)
{
}
public static function getSubscribedEvents(): array
{
return [
SendMisReportEmailEvent::NAME => [
['handleMisReportEmailSend', 10]
]
];
}
public function handleMisReportEmailSend(SendMisReportEmailEvent $event): void
{
$customer = $event->getCustomer();
$attachment = $event->getAttachment();
$adminEmails = $event->getAdminEmails();
$fromDate = $event->getFromDate();
$toDate = $event->getToDate();
$date = new \DateTime('now');
$yesterDay = $date->sub(new \DateInterval('P1D'));
$yesterDay = $yesterDay->format('d-m-Y');
$toEmails = [];
if(!is_null($customer)) {
foreach($customer->getCompany()->getContacts() as $contact)
{
if($contact->isEmailNotificationEnabled())
{
$toEmails[] = $contact->getEmail();
}
}
}
$adminEmails = !is_null($adminEmails) ? explode(',', $adminEmails): [];
$toEmails = array_merge($toEmails, $adminEmails);
dump($adminEmails);
if(!empty($toEmails)) {
$emailParameters = [
'from' => [
'email' => $this->fromEmail,
'name' => $this->fromName
],
'to' => [
'email' => $toEmails,
],
'subject' => 'Shipments from '.$fromDate->format('d-m-Y').' to '.$toDate->format('d-m-Y'),
'template' => 'email/orders_mis_report.html.twig',
'parameters' => [
'customerName' => !is_null($customer) ? $customer->getFirstname() : '',
'fromDate' => $fromDate->format('d-m-Y'),
'toDate' => $toDate->format('d-m-Y'),
],
'attachment' => $attachment
];
$emailEvent = new SendEmailEvent($emailParameters);
$this->eventDispatcher->dispatch($emailEvent, SendEmailEvent::NAME);
}
}
}