<?php
namespace App\Subscriber;
use App\Infra\Events\ContactEvent;
use App\Infra\Events\SessionMessageEvent;
use App\Infra\Services\MailerService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContactSubscriber implements EventSubscriberInterface
{
private $mailerService;
private $eventDispatcher;
public static function getSubscribedEvents()
{
return [
contactEvent::CONTACT_SEND_EMAIL => 'contactSendEmail',
];
}
public function __construct(
MailerService $mailerService,
EventDispatcherInterface $eventDispatcher
) {
$this->mailerService = $mailerService;
$this->eventDispatcher = $eventDispatcher;
}
public function contactSendEmail(ContactEvent $event)
{
$this->eventDispatcher->dispatch(
new SessionMessageEvent(
'success',
'Votre demande a bien été envoyée'),
SessionMessageEvent::SESSION_MESSAGE
);
$this->mailerService->sendMail(
$_ENV['ADMIN_MAIL'],
$event->getContact()->mail,
$event->getContact()->object,
'emails/contact_email.html.twig', ['contact' => $event->getContact()]
);
}
}