<?php
/*
* Unauthorized copying of this file, via any medium is strictly prohibited
* Proprietary and confidential.
*
* @author Bilel AZRI <azri.bilel@gmail.com>
* @author Assma BEN SASSI <bensassiasma.bws@gmail.com>
*
* Bicking man (c) 2019-present.
*/
declare(strict_types=1);
namespace App\EventSubscriber\Mailer;
use App\Event\Document\DocumentDeletedEvent;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Twig\Environment;
final class DocumentDeletedSubscriber implements EventSubscriberInterface
{
private Environment $twig;
private string $senderEmail;
private string $senderName;
private MailerInterface $mailer;
public function __construct(Environment $twig, MailerInterface $mailer, string $senderEmail, string $senderName)
{
$this->twig = $twig;
$this->senderEmail = $senderEmail;
$this->senderName = $senderName;
$this->mailer = $mailer;
}
public static function getSubscribedEvents(): array
{
return [DocumentDeletedEvent::NAME => 'sendMailToUser'];
}
public function sendMailToUser(DocumentDeletedEvent $event): void
{
$document = $event->getDocument();
$emailContent = $this->twig->render('emails/user/document_deleted.html.twig', [
'document' => $document,
]);
$user = $document->getParticipant();
if (null !== $user) {
$email = (new TemplatedEmail())
->from(new Address($this->senderEmail, $this->senderName))
->to((string) $user->getEmail())
->subject('BikingMan document supprimé')
->html($emailContent)
;
$this->mailer->send($email);
}
}
}