SendMessageToTransportsEvent example

$sender = null;

        if ($envelope->all(ReceivedStamp::class)) {
            // it's a received message, do not send it back             $this->logger?->info('Received message {class}', $context);
        } else {
            $shouldDispatchEvent = true;
            $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$senders);
                    $this->eventDispatcher->dispatch($event);
                    $envelope = $event->getEnvelope();
                    $shouldDispatchEvent = false;
                }

                $this->logger?->info('Sending message {class} with {alias} sender using {sender}', $context + ['alias' => $alias, 'sender' => $sender::class]);
                $envelope = $sender->send($envelope->with(new SentStamp($sender::class, \is_string($alias) ? $alias : null)));
            }

            if (!$this->allowNoSenders && !$sender) {
                throw new NoSenderForMessageException(sprintf('No sender for message "%s".', $context['class']));
            }
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['foo' => $sender1, 'bar' => $sender2]));

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

        $sender1->expects($this->once())->method('send')->willReturn($envelope);
        $sender2->expects($this->once())->method('send')->willReturn($envelope);

        $middleware->handle($envelope$this->getStackMock(false));
    }

    public function testItDoesNotDispatchWithNoSenders()
    {
Home | Imprint | This part of the site doesn't use cookies.