Forráskód Böngészése

refactor MailHub to natively use messenger and twig

Olivier Massot 3 éve
szülő
commit
d3c26a82e2

+ 10 - 0
config/packages/dev/mailer.yaml

@@ -0,0 +1,10 @@
+framework:
+  mailer:
+    # @see https://symfony.com/doc/5.4/mailer.html#development-debugging
+
+    # Disable the mailing in dev mode
+    dsn: 'null://null'
+
+    # Or send all mails to the same address:
+    #envelope:
+    #  recipients: ['exploitation@opentalent.fr']

+ 1 - 0
config/packages/messenger.yaml

@@ -15,3 +15,4 @@ framework:
             'App\Message\Command\Typo3\Typo3UpdateCommand': async
             'App\Message\Command\Typo3\Typo3DeleteCommand': async
             'App\Message\Command\Typo3\Typo3UndeleteCommand': async
+            'Symfony\Component\Mailer\Messenger\SendEmailMessage': async

+ 0 - 167
src/Message/Command/SendEmail.php

@@ -1,167 +0,0 @@
-<?php
-
-namespace App\Message\Command;
-
-use Symfony\Component\Mime\Email;
-
-/**
- * Envoi d'un email
- */
-class SendEmail
-{
-    public function __construct(
-        private array $from,
-        private array $to,
-        private string $subject,
-        private ?string $text = null,
-        private ?String $html = null,
-        private ?array $cc = null,
-        private ?array $bcc = null,
-        private ?array $replyTo = null,
-        private int $priority = Email::PRIORITY_NORMAL
-    ) {}
-
-    /**
-     * @return array
-     */
-    public function getFrom(): array
-    {
-        return $this->from;
-    }
-
-    /**
-     * @param array $from
-     */
-    public function setFrom(array $from): void
-    {
-        $this->from = $from;
-    }
-
-    /**
-     * @return array
-     */
-    public function getTo(): array
-    {
-        return $this->to;
-    }
-
-    /**
-     * @param array $to
-     */
-    public function setTo(array $to): void
-    {
-        $this->to = $to;
-    }
-
-    /**
-     * @return string
-     */
-    public function getSubject(): string
-    {
-        return $this->subject;
-    }
-
-    /**
-     * @param string $subject
-     */
-    public function setSubject(string $subject): void
-    {
-        $this->subject = $subject;
-    }
-
-    /**
-     * @return string|null
-     */
-    public function getText(): ?string
-    {
-        return $this->text;
-    }
-
-    /**
-     * @param string|null $text
-     */
-    public function setText(?string $text): void
-    {
-        $this->text = $text;
-    }
-
-    /**
-     * @return String|null
-     */
-    public function getHtml(): ?string
-    {
-        return $this->html;
-    }
-
-    /**
-     * @param String|null $html
-     */
-    public function setHtml(?string $html): void
-    {
-        $this->html = $html;
-    }
-
-    /**
-     * @return array|null
-     */
-    public function getCc(): ?array
-    {
-        return $this->cc;
-    }
-
-    /**
-     * @param array|null $cc
-     */
-    public function setCc(?array $cc): void
-    {
-        $this->cc = $cc;
-    }
-
-    /**
-     * @return array|null
-     */
-    public function getBcc(): ?array
-    {
-        return $this->bcc;
-    }
-
-    /**
-     * @param array|null $bcc
-     */
-    public function setBcc(?array $bcc): void
-    {
-        $this->bcc = $bcc;
-    }
-
-    /**
-     * @return array|null
-     */
-    public function getReplyTo(): ?array
-    {
-        return $this->replyTo;
-    }
-
-    /**
-     * @param array|null $replyTo
-     */
-    public function setReplyTo(?array $replyTo): void
-    {
-        $this->replyTo = $replyTo;
-    }
-
-    /**
-     * @return int
-     */
-    public function getPriority(): int
-    {
-        return $this->priority;
-    }
-
-    /**
-     * @param int $priority
-     */
-    public function setPriority(int $priority): void
-    {
-        $this->priority = $priority;
-    }
-}

+ 0 - 50
src/Message/Handler/SendEmailHandler.php

@@ -1,50 +0,0 @@
-<?php
-
-namespace App\Message\Handler;
-
-use App\Message\Command\SendEmail;
-use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
-use Symfony\Component\Mailer\MailerInterface;
-use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
-use Symfony\Component\Mime\Email;
-
-/**
- * Handler for App\Message\Command\SendEmail
- */
-class SendEmailHandler implements MessageHandlerInterface
-{
-    public function __construct(
-        private MailerInterface $mailer
-    ) {}
-
-    /**
-     * @throws TransportExceptionInterface
-     */
-    public function __invoke(SendEmail $sendEmail) {
-        $email = (new Email())
-            ->from(...$sendEmail->getFrom())
-            ->to(...$sendEmail->getTo())
-            ->subject($sendEmail->getSubject());
-
-        if ($sendEmail->getText() !== null) {
-            $email->text($sendEmail->getText());
-        }
-        if ($sendEmail->getHtml() !== null) {
-            $email->html($sendEmail->getHtml());
-        }
-        if ($sendEmail->getCc() !== null) {
-            $email->cc(...$sendEmail->getCc());
-        }
-        if ($sendEmail->getBcc() !== null) {
-            $email->bcc(...$sendEmail->getBcc());
-        }
-        if ($sendEmail->getReplyTo() !== null) {
-            $email->replyTo(...$sendEmail->getReplyTo());
-        }
-        if ($sendEmail->getPriority() !== null) {
-            $email->priority($sendEmail->getPriority());
-        }
-
-        $this->mailer->send($email);
-    }
-}

+ 0 - 1
src/Service/Core/ContactPointUtils.php

@@ -23,7 +23,6 @@ class ContactPointUtils
      *
      * @param Access $access
      * @return ContactPoint|null
-     * @throws \Exception
      * @see ContactPointUtilsTest::testGetPersonContactPointPrincipal()
      */
     public function getPersonContactPointPrincipal(Access $access): ?ContactPoint {

+ 17 - 25
src/Service/MailHub.php

@@ -4,20 +4,16 @@ namespace App\Service;
 
 use App\Entity\Access\Access;
 use App\Entity\Organization\Organization;
-use App\Message\Command\SendEmail;
 use App\Service\Core\ContactPointUtils;
-use Symfony\Component\Messenger\MessageBusInterface;
+use Symfony\Bridge\Twig\Mime\TemplatedEmail;
+use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
+use Symfony\Component\Mailer\MailerInterface;
 use Symfony\Component\Mime\Address;
-use Twig\Environment;
-use Twig\Error\LoaderError;
-use Twig\Error\RuntimeError;
-use Twig\Error\SyntaxError;
 
 class MailHub
 {
     public function __construct(
-        private MessageBusInterface $messageBus,
-        private Environment $twig,
+        private MailerInterface $mailer,
         private string $opentalentNoReplyEmailAddress,
         private ContactPointUtils $contactPointUtils,
         private \App\Service\Access\Utils $accessUtils
@@ -32,7 +28,7 @@ class MailHub
      * @param string $subject
      * @param string $template
      * @param array $data
-     * @throws \Exception
+     * @throws TransportExceptionInterface
      */
     public function sendAutomaticEmailTo(Access $access, string $subject, string $template, array $data): void
     {
@@ -40,29 +36,25 @@ class MailHub
         if ($contactPoint === null || empty($contactPoint->getEmail()) || $contactPoint->getEmailInvalid()) {
             throw new \RuntimeException('Access has no principal email address, abort');
         }
+        /** @noinspection NullPointerExceptionInspection */
         $to = new Address($contactPoint->getEmail(), $access->getPerson()->getFullName());
-        $data['_address'] = $to;
 
-        try {
-            $content = $this->twig->render('@templates/mail/' . $template . '.html.twig', $data);
-        } catch (LoaderError | RuntimeError | SyntaxError $e) {
-            throw new \RuntimeException('Error while rendering the email template ', 0, $e);
-        }
+        $context = ['_address' => $to];
+        $context = array_merge($context, $data);
+
+        $email = (new TemplatedEmail())
+            ->from($this->opentalentNoReplyEmailAddress)
+            ->to($to)
+            ->subject($subject)
+            ->htmlTemplate('@templates/mail/' . $template . '.html.twig')
+            ->context($context);
 
-        $this->messageBus->dispatch(
-            new SendEmail(
-                [$this->opentalentNoReplyEmailAddress],
-                [$to],
-                $subject,
-                null,
-                $content
-            )
-        );
+        $this->mailer->send($email);
     }
 
     /**
      * Sends an automatic 'do-not-reply'-type email to the admin of the organization
-     * @throws \Exception
+     * @throws TransportExceptionInterface
      */
     public function sendAutomaticEmailToAdmin(Organization $organization, string $subject, string $template, array $data): void
     {

+ 0 - 0
templates/mail/subdomain.html.twig → templates/emails/subdomain.html.twig


+ 2 - 2
tests/Service/Access/UtilsTest.php

@@ -97,7 +97,7 @@ class UtilsTest extends TestCase
     /**
      * @see Utils::findAdminFor()
      */
-    public function testGetAdminAccess(){
+    public function testFindAdminFor(){
         $this->accessRepositoryMock
             ->method('findOneBy')
             ->willReturn(new Access());
@@ -108,7 +108,7 @@ class UtilsTest extends TestCase
     /**
      * @see Utils::findAdminFor()
      */
-    public function testGetAdminAccessNotFound(){
+    public function testFindAdminForNotFound(){
         $this->accessRepositoryMock
             ->method('findOneBy')
             ->willReturn(null);