diff --git a/src/Domain/EmailFooter/DefaultEmailFooter.php b/src/Domain/EmailFooter/DefaultEmailFooter.php index d62bff2..eb3a969 100644 --- a/src/Domain/EmailFooter/DefaultEmailFooter.php +++ b/src/Domain/EmailFooter/DefaultEmailFooter.php @@ -7,6 +7,7 @@ use RichId\MailerBundle\Domain\Entity\EmailFooter; use RichId\MailerBundle\Domain\Port\EmailFooterRepositoryInterface; use RichId\MailerBundle\Domain\Port\TranslatorInterface; +use Symfony\Component\Mime\Email as SymfonyEmail; use Symfony\Contracts\Service\Attribute\Required; final class DefaultEmailFooter implements EmailFooterInterface @@ -17,7 +18,7 @@ final class DefaultEmailFooter implements EmailFooterInterface #[Required] public TranslatorInterface $translator; - public function getFooter(): string + public function getFooter(?SymfonyEmail $email = null): string { $content = ''; $footers = $this->emailFooterRepository->getEmailFooters(); diff --git a/src/Domain/EmailFooter/EmailFooterInterface.php b/src/Domain/EmailFooter/EmailFooterInterface.php index 9f40fb0..2f8f886 100644 --- a/src/Domain/EmailFooter/EmailFooterInterface.php +++ b/src/Domain/EmailFooter/EmailFooterInterface.php @@ -4,8 +4,11 @@ namespace RichId\MailerBundle\Domain\EmailFooter; +use Symfony\Component\Mime\Email as SymfonyEmail; + interface EmailFooterInterface { - public function getFooter(): string; + public function getFooter(?SymfonyEmail $email = null): string; + public static function getPriority(): int; } diff --git a/src/Domain/EmailFooter/EmailFooterManager.php b/src/Domain/EmailFooter/EmailFooterManager.php index fca9a9d..95a5d70 100644 --- a/src/Domain/EmailFooter/EmailFooterManager.php +++ b/src/Domain/EmailFooter/EmailFooterManager.php @@ -5,6 +5,7 @@ namespace RichId\MailerBundle\Domain\EmailFooter; use RichId\MailerBundle\Domain\Port\ConfigurationInterface; +use Symfony\Component\Mime\Email as SymfonyEmail; use Symfony\Contracts\Service\Attribute\Required; final class EmailFooterManager @@ -15,7 +16,7 @@ final class EmailFooterManager #[Required] public ConfigurationInterface $configuration; - public function getFooter(): ?string + public function getFooter(?SymfonyEmail $email = null): ?string { if (empty($this->footers)) { return null; @@ -28,7 +29,7 @@ public function getFooter(): ?string continue; } - $footerContent .= $footer->getFooter(); + $footerContent .= $footer->getFooter($email); } return \htmlspecialchars_decode( diff --git a/src/Domain/SubjectPrefix/SubjectPrefixInterface.php b/src/Domain/SubjectPrefix/SubjectPrefixInterface.php new file mode 100644 index 0000000..4859cf9 --- /dev/null +++ b/src/Domain/SubjectPrefix/SubjectPrefixInterface.php @@ -0,0 +1,14 @@ +prefixes)) { + return null; + } + + return \htmlspecialchars_decode( + \trim( + \implode( + ' ', + \array_map( + fn (SubjectPrefixInterface $service) => $service->getPrefix($email), + $this->prefixes + ) + ) + ) + ); + } +} diff --git a/src/Domain/Updater/FooterEmailUpdater.php b/src/Domain/Updater/FooterEmailUpdater.php index 2df01a8..2998648 100644 --- a/src/Domain/Updater/FooterEmailUpdater.php +++ b/src/Domain/Updater/FooterEmailUpdater.php @@ -16,7 +16,7 @@ final class FooterEmailUpdater public function __invoke(SymfonyEmail $email): void { - $footer = $this->emailFooterManager->getFooter(); + $footer = $this->emailFooterManager->getFooter($email); $htmlBody = $email->getHtmlBody(); $forceDisabled = $email instanceof Email && $email->isFooterDisabled(); diff --git a/src/Domain/Updater/SenderEmailUpdater.php b/src/Domain/Updater/SenderEmailUpdater.php index 3284318..3ef4a49 100644 --- a/src/Domain/Updater/SenderEmailUpdater.php +++ b/src/Domain/Updater/SenderEmailUpdater.php @@ -16,10 +16,12 @@ final class SenderEmailUpdater public function __invoke(Email $email): void { + $customSenderName = $email->getHeaders()->get('custom-sender-name')?->getBodyAsString(); + $email->from( new Address( $this->configuration->getSenderAddress(), - $this->configuration->getSenderName() + $customSenderName ?? $this->configuration->getSenderName() ) ); } diff --git a/src/Domain/Updater/SubjectPrefixEmailUpdater.php b/src/Domain/Updater/SubjectPrefixEmailUpdater.php index e551a1c..b0acb25 100644 --- a/src/Domain/Updater/SubjectPrefixEmailUpdater.php +++ b/src/Domain/Updater/SubjectPrefixEmailUpdater.php @@ -5,6 +5,7 @@ namespace RichId\MailerBundle\Domain\Updater; use RichId\MailerBundle\Domain\Port\ConfigurationInterface; +use RichId\MailerBundle\Domain\SubjectPrefix\SubjectPrefixManager; use Symfony\Component\Mime\Email; use Symfony\Contracts\Service\Attribute\Required; @@ -13,12 +14,24 @@ final class SubjectPrefixEmailUpdater #[Required] public ConfigurationInterface $configuration; + #[Required] + public SubjectPrefixManager $subjectPrefixManager; + public function __invoke(Email $email): void { $subject = $email->getSubject() ?? ''; - $subjectPrefix = $this->configuration->getSubjectPrefix(); - if ($subjectPrefix === null || $subjectPrefix === '' || \str_starts_with($subject, $subjectPrefix)) { + $subjectPrefix = \implode( + ' ', + \array_filter( + [ + $this->configuration->getSubjectPrefix(), + $this->subjectPrefixManager->getPrefix($email) + ] + ) + ); + + if ($subjectPrefix === '' || \str_starts_with($subject, $subjectPrefix)) { return; } diff --git a/src/Infrastructure/DependencyInjection/CompilerPass/EmailFooterCompilerPass.php b/src/Infrastructure/DependencyInjection/CompilerPass/EmailFooterCompilerPass.php index 376d12a..95b8bc6 100644 --- a/src/Infrastructure/DependencyInjection/CompilerPass/EmailFooterCompilerPass.php +++ b/src/Infrastructure/DependencyInjection/CompilerPass/EmailFooterCompilerPass.php @@ -5,6 +5,7 @@ namespace RichId\MailerBundle\Infrastructure\DependencyInjection\CompilerPass; use RichCongress\BundleToolbox\Configuration\AbstractCompilerPass; +use RichId\MailerBundle\Domain\EmailFooter\EmailFooterInterface; use RichId\MailerBundle\Domain\EmailFooter\EmailFooterManager; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; @@ -27,7 +28,7 @@ private function getReferences(ContainerBuilder $container): array $container, self::TAG, static function (Reference $reference): int { - /** @var \RichId\MailerBundle\Domain\EmailFooter\EmailFooterInterface $class */ + /** @var EmailFooterInterface $class */ $class = (string) $reference; return $class::getPriority(); diff --git a/src/Infrastructure/DependencyInjection/CompilerPass/SubjectPrefixCompilerPass.php b/src/Infrastructure/DependencyInjection/CompilerPass/SubjectPrefixCompilerPass.php new file mode 100644 index 0000000..8e462bd --- /dev/null +++ b/src/Infrastructure/DependencyInjection/CompilerPass/SubjectPrefixCompilerPass.php @@ -0,0 +1,38 @@ +getReferences($container); + $definition = $container->getDefinition(SubjectPrefixManager::class); + $definition->setProperty('prefixes', $references); + } + + /** @return Reference[] */ + private function getReferences(ContainerBuilder $container): array + { + return self::getSortedReferencesByTag( + $container, + self::TAG, + static function (Reference $reference): int { + /** @var SubjectPrefixInterface $class */ + $class = (string) $reference; + + return $class::getPriority(); + } + ); + } +} diff --git a/src/Infrastructure/DependencyInjection/RichIdMailerExtension.php b/src/Infrastructure/DependencyInjection/RichIdMailerExtension.php index 36a4526..a30384b 100644 --- a/src/Infrastructure/DependencyInjection/RichIdMailerExtension.php +++ b/src/Infrastructure/DependencyInjection/RichIdMailerExtension.php @@ -6,7 +6,9 @@ use RichCongress\BundleToolbox\Configuration\AbstractExtension; use RichId\MailerBundle\Domain\EmailFooter\EmailFooterInterface; +use RichId\MailerBundle\Domain\SubjectPrefix\SubjectPrefixInterface; use RichId\MailerBundle\Infrastructure\DependencyInjection\CompilerPass\EmailFooterCompilerPass; +use RichId\MailerBundle\Infrastructure\DependencyInjection\CompilerPass\SubjectPrefixCompilerPass; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; @@ -32,5 +34,6 @@ public function load(array $configs, ContainerBuilder $container): void $loader->load('services.xml'); $container->registerForAutoconfiguration(EmailFooterInterface::class)->addTag(EmailFooterCompilerPass::TAG); + $container->registerForAutoconfiguration(SubjectPrefixInterface::class)->addTag(SubjectPrefixCompilerPass::TAG); } } diff --git a/tests/Resources/CustomSubjectPrefix.php b/tests/Resources/CustomSubjectPrefix.php new file mode 100644 index 0000000..9f4514e --- /dev/null +++ b/tests/Resources/CustomSubjectPrefix.php @@ -0,0 +1,21 @@ +getReturnPath()); self::assertEmpty($email->getCc()); self::assertEmpty($email->getBcc()); @@ -93,7 +93,7 @@ public function testSendEmailWithReturnPath(): void self::assertEmailTo('test@test.test', $email); self::assertEmailFrom('sender@test.test', $email); self::assertEmailBody('test', $email); - self::assertEmailSubject(null, $email); + self::assertEmailSubject('Custom prefix - ', $email); self::assertSame('bounces@test.test', $email->getReturnPath()->getAddress()); self::assertEmpty($email->getCc()); self::assertEmpty($email->getBcc()); @@ -120,7 +120,7 @@ public function testSendEmailWithBcc(): void self::assertEmailTo('test@test.test', $email); self::assertEmailFrom('sender@test.test', $email); self::assertEmailBody('test', $email); - self::assertEmailSubject(null, $email); + self::assertEmailSubject('Custom prefix - ', $email); self::assertEmailBcc('bcc@test.test', $email); self::assertNull($email->getReturnPath()); self::assertEmpty($email->getCc()); @@ -148,7 +148,7 @@ public function testSendEmailWithYopmailTransformer(): void self::assertEmailTo('test_test_test@yopmail.com', $email); self::assertEmailFrom('sender@test.test', $email); self::assertEmailBody('test', $email); - self::assertEmailSubject(null, $email); + self::assertEmailSubject('Custom prefix - ', $email); self::assertEmailBcc('bcc@test.test', $email); self::assertNull($email->getReturnPath()); self::assertEmpty($email->getCc()); @@ -180,7 +180,7 @@ public function testSendEmailWithBccTransformer(): void self::assertStringContainsString('Destinataire (To): test@test.test', $email->getHtmlBody()); self::assertStringContainsString('Copie (Cc): cc@test.test', $email->getHtmlBody()); self::assertStringContainsString('Copie cachée (Cci): bcc@test.test', $email->getHtmlBody()); - self::assertEmailSubject(null, $email); + self::assertEmailSubject('Custom prefix - ', $email); self::assertEmailBcc([], $email); self::assertEmailCc([], $email); self::assertNull($email->getReturnPath()); @@ -212,7 +212,7 @@ public function testSendEmailWithSubjectPrefix(): void self::assertEmailTo('test@test.test', $email); self::assertEmailFrom('sender@test.test', $email); self::assertEmailBody('test', $email); - self::assertEmailSubject('My prefix - My subject', $email); + self::assertEmailSubject('My prefix - Custom prefix - My subject', $email); self::assertNull($email->getReturnPath()); self::assertEmpty($email->getCc()); self::assertEmpty($email->getBcc()); @@ -234,7 +234,7 @@ public function testSendEmailWithFooter(): void self::assertEmailTo('test@test.test', $email); self::assertEmailFrom('sender@test.test', $email); self::assertEmailBody('test
First footer line,

Second footer line,
', $email); - self::assertEmailSubject(null, $email); + self::assertEmailSubject('Custom prefix - ', $email); self::assertNull($email->getReturnPath()); self::assertEmpty($email->getCc()); self::assertEmpty($email->getBcc()); @@ -257,7 +257,7 @@ public function testSendEmailWithFooterButDisabled(): void self::assertEmailTo('test@test.test', $email); self::assertEmailFrom('sender@test.test', $email); self::assertEmailBody('test', $email); - self::assertEmailSubject(null, $email); + self::assertEmailSubject('Custom prefix - ', $email); self::assertNull($email->getReturnPath()); self::assertEmpty($email->getCc()); self::assertEmpty($email->getBcc());