Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77381fa

Browse files
committed
[Mailer] Add a way to inject Stamps when sending an email via Messenger
1 parent 6eef692 commit 77381fa

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

src/Symfony/Component/Mailer/Event/MessageEvent.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @author Fabien Potencier <[email protected]>
2222
*/
23-
final class MessageEvent extends Event
23+
class MessageEvent extends Event
2424
{
2525
private RawMessage $message;
2626
private Envelope $envelope;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Event;
13+
14+
use Symfony\Component\Mailer\Envelope;
15+
use Symfony\Component\Messenger\Stamp\StampInterface;
16+
use Symfony\Component\Mime\RawMessage;
17+
18+
/**
19+
* Allows the transformation of a Message, the Envelope, and the Messenger stamps before the email is sent to the Messenger bus.
20+
*
21+
* @author Fabien Potencier <[email protected]>
22+
*/
23+
final class QueuingMessageEvent extends MessageEvent
24+
{
25+
/** @var StampInterface[] */
26+
private array $stamps = [];
27+
28+
public function __construct(RawMessage $message, Envelope $envelope, string $transport)
29+
{
30+
parent::__construct($message, $envelope, $transport, true);
31+
}
32+
33+
public function addStamp(StampInterface $stamp): void
34+
{
35+
$this->stamps[] = $stamp;
36+
}
37+
38+
/**
39+
* @return StampInterface[]
40+
*/
41+
public function getStamps(): array
42+
{
43+
return $this->stamps;
44+
}
45+
}

src/Symfony/Component/Mailer/Mailer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\EventDispatcher\EventDispatcherInterface;
1515
use Symfony\Component\Mailer\Event\MessageEvent;
16+
use Symfony\Component\Mailer\Event\QueuingMessageEvent;
1617
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
1718
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
1819
use Symfony\Component\Mailer\Transport\TransportInterface;
@@ -44,6 +45,7 @@ public function send(RawMessage $message, Envelope $envelope = null): void
4445
return;
4546
}
4647

48+
$stamps = [];
4749
if (null !== $this->dispatcher) {
4850
// The dispatched event here has `queued` set to `true`; the goal is NOT to render the message, but to let
4951
// listeners do something before a message is sent to the queue.
@@ -52,12 +54,13 @@ public function send(RawMessage $message, Envelope $envelope = null): void
5254
// Listeners should act depending on the `$queued` argument of the `MessageEvent` instance.
5355
$clonedMessage = clone $message;
5456
$clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
55-
$event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
57+
$event = new QueuingMessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport);
5658
$this->dispatcher->dispatch($event);
59+
$stamps = $event->getStamps();
5760
}
5861

5962
try {
60-
$this->bus->dispatch(new SendEmailMessage($message, $envelope));
63+
$this->bus->dispatch(new SendEmailMessage($message, $envelope), $stamps);
6164
} catch (HandlerFailedException $e) {
6265
foreach ($e->getNestedExceptions() as $nested) {
6366
if ($nested instanceof TransportExceptionInterface) {

src/Symfony/Component/Mailer/Tests/MailerTest.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Psr\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\Mailer\Event\MessageEvent;
16+
use Symfony\Component\Mailer\Event\QueuingMessageEvent;
1717
use Symfony\Component\Mailer\Exception\LogicException;
1818
use Symfony\Component\Mailer\Mailer;
1919
use Symfony\Component\Mailer\Transport\NullTransport;
2020
use Symfony\Component\Mailer\Transport\TransportInterface;
2121
use Symfony\Component\Messenger\Envelope;
2222
use Symfony\Component\Messenger\MessageBusInterface;
23+
use Symfony\Component\Messenger\Stamp\StampInterface;
2324
use Symfony\Component\Mime\Email;
2425
use Symfony\Component\Mime\RawMessage;
2526

@@ -37,19 +38,25 @@ public function testSendMessageToBus()
3738
{
3839
$bus = new class() implements MessageBusInterface {
3940
public $messages = [];
41+
public $stamps = [];
4042

4143
public function dispatch($message, array $stamps = []): Envelope
4244
{
4345
$this->messages[] = $message;
46+
$this->stamps = $stamps;
4447

4548
return new Envelope($message, $stamps);
4649
}
4750
};
4851

52+
$stamp = $this->createMock(StampInterface::class);
53+
4954
$dispatcher = $this->createMock(EventDispatcherInterface::class);
5055
$dispatcher->expects($this->once())
5156
->method('dispatch')
52-
->with(self::callback(static function (MessageEvent $event) {
57+
->with(self::callback(static function (QueuingMessageEvent $event) use ($stamp) {
58+
$event->addStamp($stamp);
59+
5360
return 'Time for Symfony Mailer!' === $event->getMessage()->getSubject();
5461
}))
5562
->willReturnArgument(0)
@@ -68,5 +75,7 @@ public function dispatch($message, array $stamps = []): Envelope
6875

6976
self::assertCount(1, $bus->messages);
7077
self::assertSame($email, $bus->messages[0]->getMessage());
78+
self::assertCount(1, $bus->stamps);
79+
self::assertSame([$stamp], $bus->stamps);
7180
}
7281
}

0 commit comments

Comments
 (0)