-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(EmailManager): Removed EmailManager and ContactFormManager direct…
… usage. Use their *Factory service to avoid sharing service data between async requests.
- Loading branch information
1 parent
f5289b8
commit 80e25e5
Showing
25 changed files
with
321 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
|
||
namespace RZ\Roadiz\CoreBundle\Console; | ||
|
||
use RZ\Roadiz\CoreBundle\Mailer\EmailManager; | ||
use RZ\Roadiz\CoreBundle\Mailer\EmailManagerFactory; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
|
@@ -16,7 +16,7 @@ | |
final class MailerTestCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly EmailManager $emailManager, | ||
private readonly EmailManagerFactory $emailManagerFactory, | ||
?string $name = null | ||
) { | ||
parent::__construct($name); | ||
|
@@ -37,7 +37,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
$to = Address::create($input->getArgument('email')); | ||
$from = Address::create($input->getOption('from') ?? '[email protected]'); | ||
|
||
$this->emailManager | ||
$this->emailManagerFactory->create() | ||
->setReceiver($to) | ||
->setSender($from) | ||
// Uses email_sender customizable setting | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
use RZ\Roadiz\CoreBundle\Bag\Settings; | ||
use RZ\Roadiz\CoreBundle\CustomForm\Message\CustomFormAnswerNotifyMessage; | ||
use RZ\Roadiz\CoreBundle\Entity\CustomFormAnswer; | ||
use RZ\Roadiz\CoreBundle\Mailer\EmailManager; | ||
use RZ\Roadiz\CoreBundle\Mailer\EmailManagerFactory; | ||
use RZ\Roadiz\Documents\Models\DocumentInterface; | ||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; | ||
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; | ||
|
@@ -24,11 +24,11 @@ | |
final class CustomFormAnswerNotifyMessageHandler implements MessageHandlerInterface | ||
{ | ||
public function __construct( | ||
private ManagerRegistry $managerRegistry, | ||
private EmailManager $emailManager, | ||
private Settings $settingsBag, | ||
private FilesystemOperator $documentsStorage, | ||
private LoggerInterface $logger, | ||
private readonly ManagerRegistry $managerRegistry, | ||
private readonly EmailManagerFactory $emailManagerFactory, | ||
private readonly Settings $settingsBag, | ||
private readonly FilesystemOperator $documentsStorage, | ||
private readonly LoggerInterface $messengerLogger, | ||
) { | ||
} | ||
|
||
|
@@ -51,12 +51,6 @@ public function __invoke(CustomFormAnswerNotifyMessage $message): void | |
$answer->toArray(false) | ||
); | ||
|
||
$receiver = array_filter( | ||
array_map('trim', explode(',', $answer->getCustomForm()->getEmail() ?? '')) | ||
); | ||
$receiver = array_map(function (string $email) { | ||
return new Address($email); | ||
}, $receiver); | ||
$this->sendAnswer( | ||
$answer, | ||
[ | ||
|
@@ -65,60 +59,83 @@ public function __invoke(CustomFormAnswerNotifyMessage $message): void | |
'customForm' => $answer->getCustomForm(), | ||
'title' => $message->getTitle(), | ||
'requestLocale' => $message->getLocale(), | ||
], | ||
$receiver | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return Address[] | ||
*/ | ||
private function getCustomFormReceivers(CustomFormAnswer $answer): array | ||
{ | ||
$receiver = array_filter( | ||
array_map('trim', explode(',', $answer->getCustomForm()->getEmail() ?? '')) | ||
); | ||
return array_map(function (string $email) { | ||
return new Address($email); | ||
}, $receiver); | ||
} | ||
|
||
/** | ||
* Send an answer form by Email. | ||
* | ||
* @param CustomFormAnswer $answer | ||
* @param array $assignation | ||
* @param string|array|null $receiver | ||
* @throws TransportExceptionInterface | ||
* @throws LoaderError | ||
* @throws RuntimeError | ||
* @throws SyntaxError | ||
*/ | ||
private function sendAnswer( | ||
CustomFormAnswer $answer, | ||
array $assignation, | ||
$receiver | ||
array $assignation | ||
): void { | ||
$defaultSender = $this->settingsBag->get('email_sender'); | ||
$defaultSender = !empty($defaultSender) ? $defaultSender : '[email protected]'; | ||
$this->emailManager->setAssignation($assignation); | ||
$this->emailManager->setEmailTemplate('@RoadizCore/email/forms/answerForm.html.twig'); | ||
$this->emailManager->setEmailPlainTextTemplate('@RoadizCore/email/forms/answerForm.txt.twig'); | ||
$this->emailManager->setSubject($assignation['title']); | ||
$this->emailManager->setEmailTitle($assignation['title']); | ||
$this->emailManager->setSender($defaultSender); | ||
$defaultSender = filter_var($defaultSender, FILTER_VALIDATE_EMAIL) ? $defaultSender : '[email protected]'; | ||
$receivers = $this->getCustomFormReceivers($answer); | ||
|
||
$realSender = filter_var($answer->getEmail(), FILTER_VALIDATE_EMAIL) ? $answer->getEmail() : $defaultSender; | ||
$emailManager = $this->emailManagerFactory->create(); | ||
$emailManager->setAssignation($assignation); | ||
$emailManager->setEmailTemplate('@RoadizCore/email/forms/answerForm.html.twig'); | ||
$emailManager->setEmailPlainTextTemplate('@RoadizCore/email/forms/answerForm.txt.twig'); | ||
$emailManager->setSubject($assignation['title']); | ||
$emailManager->setEmailTitle($assignation['title']); | ||
$emailManager->setSender($realSender); | ||
|
||
try { | ||
foreach ($answer->getAnswerFields() as $customFormAnswerAttr) { | ||
/** @var DocumentInterface $document */ | ||
foreach ($customFormAnswerAttr->getDocuments() as $document) { | ||
$this->emailManager->addResource( | ||
$emailManager->addResource( | ||
$this->documentsStorage->readStream($document->getMountPath()), | ||
$document->getFilename(), | ||
$this->documentsStorage->mimeType($document->getMountPath()) | ||
); | ||
$this->messengerLogger->debug(sprintf( | ||
'Joining document %s to email.', | ||
$document->getFilename() | ||
)); | ||
} | ||
} | ||
} catch (FilesystemException $exception) { | ||
$this->logger->error($exception->getMessage(), [ | ||
$this->messengerLogger->error($exception->getMessage(), [ | ||
'entity' => $answer | ||
]); | ||
} | ||
|
||
if (empty($receiver)) { | ||
$this->emailManager->setReceiver($defaultSender); | ||
if (empty($receivers)) { | ||
$emailManager->setReceiver($defaultSender); | ||
} else { | ||
$this->emailManager->setReceiver($receiver); | ||
$emailManager->setReceiver($receivers); | ||
} | ||
|
||
// Send the message | ||
$this->emailManager->send(); | ||
$emailManager->send(); | ||
$this->messengerLogger->debug(sprintf( | ||
'CustomForm (%s) answer sent to %s', | ||
$answer->getCustomForm()->getName(), | ||
$realSender | ||
)); | ||
} | ||
} |
Oops, something went wrong.