-
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.
Add new subject prefix logic + custome sender name
- Loading branch information
Dumazeau
committed
Aug 28, 2024
1 parent
5bc815a
commit d6a7647
Showing
14 changed files
with
147 additions
and
17 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 |
---|---|---|
@@ -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; | ||
} |
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 |
---|---|---|
@@ -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 | ||
) | ||
) | ||
) | ||
); | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
src/Infrastructure/DependencyInjection/CompilerPass/SubjectPrefixCompilerPass.php
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 |
---|---|---|
@@ -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(); | ||
} | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|
@@ -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()); | ||
|