Skip to content

Commit bcb1ba5

Browse files
minor #62110 [Messenger] Simplify code (HypeMC)
This PR was merged into the 7.4 branch. Discussion ---------- [Messenger] Simplify code | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Noticed while working on #61843. There's no reason for the event to be dispatched inside the loop anymore: 1. The event was [originally dispatched outside the loop](#30650). 2. It was later [moved inside the loop](#30676) because of the retry mechanism. 3. [The retry mechanism was later extracted](#34185), so there's no longer a need for the event to be dispatched inside the loop. Commits ------- beeb4b9 [Messenger] Simplify code
2 parents d21e6ef + beeb4b9 commit bcb1ba5

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/Symfony/Component/Messenger/Event/SendMessageToTransportsEvent.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
*
2020
* The event is *only* dispatched if the message will actually
2121
* be sent to at least one transport. If the message is sent
22-
* to multiple transports, the message is dispatched only once.
23-
* This message is only dispatched the first time a message
22+
* to multiple transports, the event is dispatched only once.
23+
* This event is only dispatched the first time a message
2424
* is sent to a transport, not also if it is retried.
2525
*
2626
* @author Ryan Weaver <ryan@symfonycasts.com>

src/Symfony/Component/Messenger/Middleware/SendMessageMiddleware.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
4747
// it's a received message, do not send it back
4848
$this->logger?->info('Received message {class}', $context);
4949
} else {
50-
$shouldDispatchEvent = true;
5150
$senders = $this->sendersLocator->getSenders($envelope);
5251
$senders = \is_array($senders) ? $senders : iterator_to_array($senders);
53-
foreach ($senders as $alias => $sender) {
54-
if (null !== $this->eventDispatcher && $shouldDispatchEvent) {
55-
$event = new SendMessageToTransportsEvent($envelope, $senders);
56-
$this->eventDispatcher->dispatch($event);
57-
$envelope = $event->getEnvelope();
58-
$shouldDispatchEvent = false;
59-
}
6052

53+
if (null !== $this->eventDispatcher && $senders) {
54+
$event = new SendMessageToTransportsEvent($envelope, $senders);
55+
$this->eventDispatcher->dispatch($event);
56+
$envelope = $event->getEnvelope();
57+
}
58+
59+
foreach ($senders as $alias => $sender) {
6160
$this->logger?->info('Sending message {class} with {alias} sender using {sender}', $context + ['alias' => $alias, 'sender' => $sender::class]);
6261
$envelope = $sender->send($envelope->with(new SentStamp($sender::class, \is_string($alias) ? $alias : null)));
6362
}

0 commit comments

Comments
 (0)