Skip to content

Commit b61ae3a

Browse files
[Messenger] make dispatch(), handle() and send() methods return Envelope
1 parent 61c2159 commit b61ae3a

28 files changed

+90
-67
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* The component is not experimental anymore
88
* All the changes below are BC BREAKS
9-
* `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
9+
* `MessageBusInterface::dispatch()`, `MiddlewareInterface::handle()` and `SenderInterface::send()` return `Envelope`
1010
* `MiddlewareInterface::handle()` now require an `Envelope` as first argument and a `StackInterface` as second
1111
* `EnvelopeAwareInterface` has been removed
1212
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
@@ -31,7 +31,6 @@ CHANGELOG
3131
* `AbstractHandlerLocator` is now internal
3232
* `HandlerLocatorInterface::resolve()` has been replaced by `getHandler(Envelope $envelope): ?callable` and shouldn't throw when no handlers are found
3333
* `SenderLocatorInterface::getSenderForMessage()` has been replaced by `getSender(Envelope $envelope)`
34-
* `SenderInterface::send()` returns `void`
3534
* Classes in the `Middleware\Enhancers` sub-namespace have been moved to the `Middleware` one
3635
* Classes in the `Asynchronous\Routing` sub-namespace have been moved to the `Transport\Sender\Locator` sub-namespace
3736
* The `Asynchronous/Middleware/SendMessageMiddleware` class has been moved to the `Middleware` namespace

MessageBus.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ public function getIterator()
5050
/**
5151
* {@inheritdoc}
5252
*/
53-
public function dispatch($message): void
53+
public function dispatch($message): Envelope
5454
{
5555
if (!\is_object($message)) {
5656
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object, but got %s.', __METHOD__, \gettype($message)));
5757
}
58+
$envelope = $message instanceof Envelope ? $message : new Envelope($message);
5859
$middlewareIterator = $this->middlewareAggregate->getIterator();
5960

6061
while ($middlewareIterator instanceof \IteratorAggregate) {
@@ -63,10 +64,10 @@ public function dispatch($message): void
6364
$middlewareIterator->rewind();
6465

6566
if (!$middlewareIterator->valid()) {
66-
return;
67+
return $envelope;
6768
}
6869
$stack = new StackMiddleware($middlewareIterator);
6970

70-
$middlewareIterator->current()->handle($message instanceof Envelope ? $message : new Envelope($message), $stack);
71+
return $middlewareIterator->current()->handle($envelope, $stack);
7172
}
7273
}

MessageBusInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ interface MessageBusInterface
2121
*
2222
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
2323
*/
24-
public function dispatch($message): void;
24+
public function dispatch($message): Envelope;
2525
}

Middleware/ActivationMiddleware.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
3535
/**
3636
* {@inheritdoc}
3737
*/
38-
public function handle(Envelope $envelope, StackInterface $stack): void
38+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3939
{
4040
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
41-
$this->inner->handle($envelope, $stack);
42-
} else {
43-
$stack->next()->handle($envelope, $stack);
41+
return $this->inner->handle($envelope, $stack);
4442
}
43+
44+
return $stack->next()->handle($envelope, $stack);
4545
}
4646
}

Middleware/HandleMessageMiddleware.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ public function __construct(HandlerLocatorInterface $messageHandlerLocator, bool
3434
*
3535
* @throws NoHandlerForMessageException When no handler is found and $allowNoHandlers is false
3636
*/
37-
public function handle(Envelope $envelope, StackInterface $stack): void
37+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3838
{
3939
if (null !== $handler = $this->messageHandlerLocator->getHandler($envelope)) {
4040
$handler($envelope->getMessage());
41-
$stack->next()->handle($envelope, $stack);
4241
} elseif (!$this->allowNoHandlers) {
4342
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', \get_class($envelope->getMessage())));
4443
}
44+
45+
return $stack->next()->handle($envelope, $stack);
4546
}
4647
}

Middleware/LoggingMiddleware.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __construct(LoggerInterface $logger)
2929
/**
3030
* {@inheritdoc}
3131
*/
32-
public function handle(Envelope $envelope, StackInterface $stack): void
32+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3333
{
3434
$message = $envelope->getMessage();
3535
$context = array(
@@ -39,7 +39,7 @@ public function handle(Envelope $envelope, StackInterface $stack): void
3939
$this->logger->debug('Starting handling message {name}', $context);
4040

4141
try {
42-
$stack->next()->handle($envelope, $stack);
42+
$envelope = $stack->next()->handle($envelope, $stack);
4343
} catch (\Throwable $e) {
4444
$context['exception'] = $e;
4545
$this->logger->warning('An exception occurred while handling message {name}', $context);
@@ -48,5 +48,7 @@ public function handle(Envelope $envelope, StackInterface $stack): void
4848
}
4949

5050
$this->logger->debug('Finished handling message {name}', $context);
51+
52+
return $envelope;
5153
}
5254
}

Middleware/MiddlewareInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
*/
1919
interface MiddlewareInterface
2020
{
21-
public function handle(Envelope $envelope, StackInterface $stack): void;
21+
public function handle(Envelope $envelope, StackInterface $stack): Envelope;
2222
}

Middleware/SendMessageMiddleware.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,24 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
3434
/**
3535
* {@inheritdoc}
3636
*/
37-
public function handle(Envelope $envelope, StackInterface $stack): void
37+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
3838
{
3939
if ($envelope->get(ReceivedStamp::class)) {
4040
// It's a received message. Do not send it back:
41-
$stack->next()->handle($envelope, $stack);
42-
43-
return;
41+
return $stack->next()->handle($envelope, $stack);
4442
}
4543

4644
$sender = $this->senderLocator->getSender($envelope);
4745

4846
if ($sender) {
49-
$sender->send($envelope);
47+
$envelope = $sender->send($envelope);
5048

5149
if (!AbstractSenderLocator::getValueFromMessageRouting($this->messagesToSendAndHandleMapping, $envelope)) {
5250
// message has no corresponding handler
53-
return;
51+
return $envelope;
5452
}
5553
}
5654

57-
$stack->next()->handle($envelope, $stack);
55+
return $stack->next()->handle($envelope, $stack);
5856
}
5957
}

Middleware/StackMiddleware.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public function next(): MiddlewareInterface
4141
return $iterator->current();
4242
}
4343

44-
public function handle(Envelope $envelope, StackInterface $stack): void
44+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4545
{
46-
// no-op: this is the last null middleware
46+
return $envelope;
4747
}
4848
}

Middleware/TraceableMiddleware.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, st
3737
/**
3838
* {@inheritdoc}
3939
*/
40-
public function handle(Envelope $envelope, StackInterface $stack): void
40+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
4141
{
4242
$class = \get_class($this->inner);
4343
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
@@ -49,7 +49,7 @@ public function handle(Envelope $envelope, StackInterface $stack): void
4949
$this->stopwatch->start($eventName, $this->eventCategory);
5050

5151
try {
52-
$this->inner->handle($envelope, new TraceableInnerMiddleware($stack, $this->stopwatch, $eventName, $this->eventCategory));
52+
return $this->inner->handle($envelope, new TraceableInnerMiddleware($stack, $this->stopwatch, $eventName, $this->eventCategory));
5353
} finally {
5454
if ($this->stopwatch->isStarted($eventName)) {
5555
$this->stopwatch->stop($eventName);
@@ -79,15 +79,17 @@ public function __construct(StackInterface $stack, Stopwatch $stopwatch, string
7979
/**
8080
* {@inheritdoc}
8181
*/
82-
public function handle(Envelope $envelope, StackInterface $stack): void
82+
public function handle(Envelope $envelope, StackInterface $stack): Envelope
8383
{
8484
$this->stopwatch->stop($this->eventName);
8585
if ($this === $stack) {
86-
$this->stack->next()->handle($envelope, $this->stack);
86+
$envelope = $this->stack->next()->handle($envelope, $this->stack);
8787
} else {
88-
$stack->next()->handle($envelope, $stack);
88+
$envelope = $stack->next()->handle($envelope, $stack);
8989
}
9090
$this->stopwatch->start($this->eventName, $this->eventCategory);
91+
92+
return $envelope;
9193
}
9294

9395
/**

0 commit comments

Comments
 (0)