Skip to content

Commit

Permalink
Add new subject prefix logic + custome sender name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dumazeau committed Aug 28, 2024
1 parent 5bc815a commit d6a7647
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/Domain/EmailFooter/DefaultEmailFooter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/Domain/EmailFooter/EmailFooterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 3 additions & 2 deletions src/Domain/EmailFooter/EmailFooterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -28,7 +29,7 @@ public function getFooter(): ?string
continue;
}

$footerContent .= $footer->getFooter();
$footerContent .= $footer->getFooter($email);
}

return \htmlspecialchars_decode(
Expand Down
14 changes: 14 additions & 0 deletions src/Domain/SubjectPrefix/SubjectPrefixInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace RichId\MailerBundle\Domain\SubjectPrefix;

use Symfony\Component\Mime\Email as SymfonyEmail;

interface SubjectPrefixInterface
{
public function getPrefix(SymfonyEmail $email): string;

public static function getPriority(): int;
}
32 changes: 32 additions & 0 deletions src/Domain/SubjectPrefix/SubjectPrefixManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace RichId\MailerBundle\Domain\SubjectPrefix;

use Symfony\Component\Mime\Email as SymfonyEmail;

class SubjectPrefixManager
{
/** @var SubjectPrefixInterface[] */
public array $prefixes;

public function getPrefix(SymfonyEmail $email): ?string
{
if (empty($this->prefixes)) {
return null;
}

return \htmlspecialchars_decode(
\trim(
\implode(
' ',
\array_map(
fn (SubjectPrefixInterface $service) => $service->getPrefix($email),
$this->prefixes
)
)
)
);
}
}
2 changes: 1 addition & 1 deletion src/Domain/Updater/FooterEmailUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/Domain/Updater/SenderEmailUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
);
}
Expand Down
17 changes: 15 additions & 2 deletions src/Domain/Updater/SubjectPrefixEmailUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace RichId\MailerBundle\Infrastructure\DependencyInjection\CompilerPass;

use RichCongress\BundleToolbox\Configuration\AbstractCompilerPass;
use RichId\MailerBundle\Domain\SubjectPrefix\SubjectPrefixInterface;
use RichId\MailerBundle\Domain\SubjectPrefix\SubjectPrefixManager;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

final class SubjectPrefixCompilerPass extends AbstractCompilerPass
{
public const TAG = 'email.subject_prefix';

public function process(ContainerBuilder $container): void
{
$references = $this->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();
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
21 changes: 21 additions & 0 deletions tests/Resources/CustomSubjectPrefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace RichId\MailerBundle\Tests\Resources;

use RichId\MailerBundle\Domain\SubjectPrefix\SubjectPrefixInterface;
use Symfony\Component\Mime\Email as SymfonyEmail;

class CustomSubjectPrefix implements SubjectPrefixInterface
{
public function getPrefix(SymfonyEmail $email): string
{
return 'Custom prefix -';
}

public static function getPriority(): int
{
return 0;
}
}
1 change: 1 addition & 0 deletions tests/Resources/Kernel/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ services:

RichId\MailerBundle\Tests\Resources\Fixtures\EmailFooterFixtures: ~
RichId\MailerBundle\Tests\Resources\Stub\ParameterBagStub: ~
RichId\MailerBundle\Tests\Resources\CustomSubjectPrefix: ~
16 changes: 8 additions & 8 deletions tests/SendEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testSendEmailMinimumConfiguration(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $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());
Expand All @@ -93,7 +93,7 @@ public function testSendEmailWithReturnPath(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $email);
self::assertEmailBody('test', $email);
self::assertEmailSubject(null, $email);
self::assertEmailSubject('Custom prefix - ', $email);
self::assertSame('[email protected]', $email->getReturnPath()->getAddress());
self::assertEmpty($email->getCc());
self::assertEmpty($email->getBcc());
Expand All @@ -120,7 +120,7 @@ public function testSendEmailWithBcc(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $email);
self::assertEmailBody('test', $email);
self::assertEmailSubject(null, $email);
self::assertEmailSubject('Custom prefix - ', $email);
self::assertEmailBcc('[email protected]', $email);
self::assertNull($email->getReturnPath());
self::assertEmpty($email->getCc());
Expand Down Expand Up @@ -148,7 +148,7 @@ public function testSendEmailWithYopmailTransformer(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $email);
self::assertEmailBody('test', $email);
self::assertEmailSubject(null, $email);
self::assertEmailSubject('Custom prefix - ', $email);
self::assertEmailBcc('[email protected]', $email);
self::assertNull($email->getReturnPath());
self::assertEmpty($email->getCc());
Expand Down Expand Up @@ -180,7 +180,7 @@ public function testSendEmailWithBccTransformer(): void
self::assertStringContainsString('Destinataire (To): [email protected]', $email->getHtmlBody());
self::assertStringContainsString('Copie (Cc): [email protected]', $email->getHtmlBody());
self::assertStringContainsString('Copie cachée (Cci): [email protected]', $email->getHtmlBody());
self::assertEmailSubject(null, $email);
self::assertEmailSubject('Custom prefix - ', $email);
self::assertEmailBcc([], $email);
self::assertEmailCc([], $email);
self::assertNull($email->getReturnPath());
Expand Down Expand Up @@ -212,7 +212,7 @@ public function testSendEmailWithSubjectPrefix(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $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());
Expand All @@ -234,7 +234,7 @@ public function testSendEmailWithFooter(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $email);
self::assertEmailBody('test<br />First footer line,<br /><br />Second footer line,<br />', $email);
self::assertEmailSubject(null, $email);
self::assertEmailSubject('Custom prefix - ', $email);
self::assertNull($email->getReturnPath());
self::assertEmpty($email->getCc());
self::assertEmpty($email->getBcc());
Expand All @@ -257,7 +257,7 @@ public function testSendEmailWithFooterButDisabled(): void
self::assertEmailTo('[email protected]', $email);
self::assertEmailFrom('[email protected]', $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());
Expand Down

0 comments on commit d6a7647

Please sign in to comment.