|
|
@@ -0,0 +1,75 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+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\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 string $opentalentNoReplyEmailAddress,
|
|
|
+ private ContactPointUtils $contactPointUtils,
|
|
|
+ private \App\Service\Access\Utils $accessUtils
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends an automatic 'do-not-reply'-type email to the user
|
|
|
+ *
|
|
|
+ * NB: These emails are not registered in the DB
|
|
|
+ *
|
|
|
+ * @param Access $access
|
|
|
+ * @param string $subject
|
|
|
+ * @param string $template
|
|
|
+ * @param array $data
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function sendAutomaticEmailTo(Access $access, string $subject, string $template, array $data): void
|
|
|
+ {
|
|
|
+ $contactPoint = $this->contactPointUtils->getPersonContactPointPrincipal($access);
|
|
|
+ if ($contactPoint === null || empty($contactPoint->getEmail()) || $contactPoint->getEmailInvalid()) {
|
|
|
+ throw new \RuntimeException('Access has no principal email address, abort');
|
|
|
+ }
|
|
|
+ $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);
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->messageBus->dispatch(
|
|
|
+ new SendEmail(
|
|
|
+ [$this->opentalentNoReplyEmailAddress],
|
|
|
+ [$to],
|
|
|
+ $subject,
|
|
|
+ null,
|
|
|
+ $content
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Sends an automatic 'do-not-reply'-type email to the admin of the organization
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function sendAutomaticEmailToAdmin(Organization $organization, string $subject, string $template, array $data): void
|
|
|
+ {
|
|
|
+ $admin = $this->accessUtils->findAdminFor($organization);
|
|
|
+ if ($admin === null) {
|
|
|
+ throw new \RuntimeException('No admin found for organization ' . $organization->getId());
|
|
|
+ }
|
|
|
+ $this->sendAutomaticEmailTo($admin, $subject, $template, $data);
|
|
|
+ }
|
|
|
+}
|