src/EventSubscriber/Mailer/DocumentDeletedSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. /*
  3.  * Unauthorized copying of this file, via any medium is strictly prohibited
  4.  * Proprietary and confidential.
  5.  *
  6.  * @author Bilel AZRI          <azri.bilel@gmail.com>
  7.  * @author Assma BEN SASSI     <bensassiasma.bws@gmail.com>
  8.  *
  9.  * Bicking man (c) 2019-present.
  10.  */
  11. declare(strict_types=1);
  12. namespace App\EventSubscriber\Mailer;
  13. use App\Event\Document\DocumentDeletedEvent;
  14. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\Mailer\MailerInterface;
  17. use Symfony\Component\Mime\Address;
  18. use Twig\Environment;
  19. final class DocumentDeletedSubscriber implements EventSubscriberInterface
  20. {
  21.     private Environment $twig;
  22.     private string $senderEmail;
  23.     private string $senderName;
  24.     private MailerInterface $mailer;
  25.     public function __construct(Environment $twigMailerInterface $mailerstring $senderEmailstring $senderName)
  26.     {
  27.         $this->twig $twig;
  28.         $this->senderEmail $senderEmail;
  29.         $this->senderName $senderName;
  30.         $this->mailer $mailer;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [DocumentDeletedEvent::NAME => 'sendMailToUser'];
  35.     }
  36.     public function sendMailToUser(DocumentDeletedEvent $event): void
  37.     {
  38.         $document $event->getDocument();
  39.         $emailContent $this->twig->render('emails/user/document_deleted.html.twig', [
  40.             'document' => $document,
  41.         ]);
  42.         $user $document->getParticipant();
  43.         if (null !== $user) {
  44.             $email = (new TemplatedEmail())
  45.                 ->from(new Address($this->senderEmail$this->senderName))
  46.                 ->to((string) $user->getEmail())
  47.                 ->subject('BikingMan document supprimé')
  48.                 ->html($emailContent)
  49.             ;
  50.             $this->mailer->send($email);
  51.         }
  52.     }
  53. }