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

Skip to content

[Messenger] Pass sender details to SendMessageToTransportsEvent #40152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Event;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Sender\SenderInterface;

/**
* Event is dispatched before a message is sent to the transport.
Expand All @@ -28,9 +29,12 @@ final class SendMessageToTransportsEvent
{
private Envelope $envelope;

public function __construct(Envelope $envelope)
private array $senders;

public function __construct(Envelope $envelope, array $senders)
{
$this->envelope = $envelope;
$this->senders = $senders;
}

public function getEnvelope(): Envelope
Expand All @@ -42,4 +46,12 @@ public function setEnvelope(Envelope $envelope)
{
$this->envelope = $envelope;
}

/**
* @return array<string, SenderInterface>
*/
public function getSenders(): array
{
return $this->senders;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$this->logger->info('Received message {class}', $context);
} else {
$shouldDispatchEvent = true;
foreach ($this->sendersLocator->getSenders($envelope) as $alias => $sender) {
$senders = $this->sendersLocator->getSenders($envelope);
$senders = \is_array($senders) ? $senders : iterator_to_array($senders);
foreach ($senders as $alias => $sender) {
if (null !== $this->eventDispatcher && $shouldDispatchEvent) {
$event = new SendMessageToTransportsEvent($envelope);
$event = new SendMessageToTransportsEvent($envelope, $senders);
$this->eventDispatcher->dispatch($event);
$envelope = $event->getEnvelope();
$shouldDispatchEvent = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ public function testItDispatchesTheEventOneTime()
{
$envelope = new Envelope(new DummyMessage('original envelope'));

$sender1 = $this->createMock(SenderInterface::class);
$sender2 = $this->createMock(SenderInterface::class);

$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->once())
->method('dispatch')
->with(new SendMessageToTransportsEvent($envelope));

$sender1 = $this->createMock(SenderInterface::class);
$sender2 = $this->createMock(SenderInterface::class);
->with(new SendMessageToTransportsEvent($envelope, ['foo' => $sender1, 'bar' => $sender2]));

$sendersLocator = $this->createSendersLocator([DummyMessage::class => ['foo', 'bar']], ['foo' => $sender1, 'bar' => $sender2]);
$middleware = new SendMessageMiddleware($sendersLocator, $dispatcher);
Expand Down