-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Draw\Component\Mailer\BodyRenderer; | ||
|
||
use Draw\Component\Mailer\Email\LocalizeEmailInterface; | ||
use Symfony\Component\Mime\BodyRendererInterface; | ||
use Symfony\Component\Mime\Message; | ||
use Symfony\Contracts\Translation\LocaleAwareInterface; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class LocalizeBodyRenderer implements BodyRendererInterface | ||
{ | ||
private ?LocaleAwareInterface $translator = null; | ||
|
||
public function __construct( | ||
private BodyRendererInterface $bodyRenderer, | ||
TranslatorInterface $translator | ||
) { | ||
if ($translator instanceof LocaleAwareInterface) { | ||
$this->translator = $translator; | ||
} | ||
} | ||
|
||
public function render(Message $message): void | ||
{ | ||
$currentLocale = null; | ||
if ($this->translator && $message instanceof LocalizeEmailInterface && $message->getLocale()) { | ||
$currentLocale = $this->translator->getLocale(); | ||
$this->translator->setLocale($message->getLocale()); | ||
} | ||
|
||
try { | ||
$this->bodyRenderer->render($message); | ||
} finally { | ||
if ($currentLocale) { | ||
$this->translator?->setLocale($currentLocale); | ||
} | ||
} | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Draw\Component\Mailer\Email; | ||
|
||
interface LocalizeEmailInterface | ||
{ | ||
public function getLocale(): ?string; | ||
} |
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Draw\Component\Mailer\Tests; | ||
|
||
use Draw\Component\Mailer\Email\LocalizeEmailInterface; | ||
use Draw\Component\Mailer\EmailComposer; | ||
use Draw\Component\Mailer\EmailWriter\EmailWriterInterface; | ||
use Draw\Component\Tester\MockTrait; | ||
|
@@ -14,19 +15,24 @@ | |
use Symfony\Component\Mime\Address; | ||
use Symfony\Component\Mime\Email; | ||
use Symfony\Component\Mime\Message; | ||
use Symfony\Component\Translation\Translator; | ||
|
||
#[CoversClass(EmailComposer::class)] | ||
class EmailComposerTest extends TestCase | ||
{ | ||
use MockTrait; | ||
|
||
private EmailComposer $object; | ||
|
||
private ContainerInterface&MockObject $serviceLocator; | ||
|
||
private Translator&MockObject $translator; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->object = new EmailComposer( | ||
$this->serviceLocator = $this->createMock(ContainerInterface::class) | ||
$this->serviceLocator = $this->createMock(ContainerInterface::class), | ||
$this->translator = $this->createMock(Translator::class) | ||
); | ||
} | ||
|
||
|
@@ -156,4 +162,34 @@ public function compose2(Message $email): void | |
static::assertSame(1, $emailWriter->compose1CallCounter); | ||
static::assertSame(1, $emailWriter->compose2CallCounter); | ||
} | ||
|
||
public function testComposeLocalizeEmail(): void | ||
{ | ||
$message = new class() extends Email implements LocalizeEmailInterface { | ||
public function getLocale(): ?string | ||
{ | ||
return 'fr'; | ||
} | ||
}; | ||
|
||
$this->translator | ||
->expects(static::once()) | ||
->method('getLocale') | ||
->willReturn('en'); | ||
|
||
$this->translator | ||
->expects(static::exactly(2)) | ||
->method('setLocale') | ||
->with( | ||
...static::withConsecutive( | ||
['fr'], | ||
['en'] | ||
) | ||
); | ||
|
||
$this->object->compose( | ||
$message, | ||
new Envelope(new Address('[email protected]'), [new Address('[email protected]')]) | ||
); | ||
} | ||
} |