Skip to content

[Messenger] Add $stamps parameter to HandleTrait::handle #42124

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
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add `SentForRetryStamp` that identifies whether a failed message was sent for retry
* Add `Symfony\Component\Messenger\Middleware\DeduplicateMiddleware` and `Symfony\Component\Messenger\Stamp\DeduplicateStamp`
* Add `--class-filter` option to the `messenger:failed:remove` command
* Add `$stamps` parameter to `HandleTrait::handle`

7.2
---
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/Messenger/HandleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Messenger\Exception\LogicException;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;

/**
* Leverages a message bus to expect a single, synchronous message handling and return its result.
Expand All @@ -29,15 +30,16 @@ trait HandleTrait
* This behavior is useful for both synchronous command & query buses,
* the last one usually returning the handler result.
*
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
* @param StampInterface[] $stamps Stamps to be set on the Envelope which are used to control middleware behavior
*/
private function handle(object $message): mixed
private function handle(object $message, array $stamps = []): mixed
{
if (!isset($this->messageBus)) {
throw new LogicException(\sprintf('You must provide a "%s" instance in the "%s::$messageBus" property, but that property has not been initialized yet.', MessageBusInterface::class, static::class));
}

$envelope = $this->messageBus->dispatch($message);
$envelope = $this->messageBus->dispatch($message, $stamps);
/** @var HandledStamp[] $handledStamps */
$handledStamps = $envelope->all(HandledStamp::class);

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Messenger/MessageBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface MessageBusInterface
* Dispatches the given message.
*
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
* @param StampInterface[] $stamps
* @param StampInterface[] $stamps Stamps set on the Envelope which are used to control middleware behavior
*
* @throws ExceptionInterface
*/
Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/Messenger/Tests/HandleTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Messenger\MessageBus;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\HandledStamp;
use Symfony\Component\Messenger\Stamp\StampInterface;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;

class HandleTraitTest extends TestCase
Expand Down Expand Up @@ -56,6 +57,20 @@ public function testHandleAcceptsEnvelopes()
$this->assertSame('result', $queryBus->query($envelope));
}

public function testHandleWithStamps()
{
$bus = $this->createMock(MessageBus::class);
$queryBus = new TestQueryBus($bus);
$stamp = $this->createMock(StampInterface::class);

$query = new DummyMessage('Hello');
$bus->expects($this->once())->method('dispatch')->with($query, [$stamp])->willReturn(
new Envelope($query, [new HandledStamp('result', 'DummyHandler::__invoke')])
);

$queryBus->query($query, [$stamp]);
}

public function testHandleThrowsOnNoHandledStamp()
{
$this->expectException(LogicException::class);
Expand Down Expand Up @@ -96,8 +111,8 @@ public function __construct(?MessageBusInterface $messageBus)
}
}

public function query($query): string
public function query($query, array $stamps = []): string
{
return $this->handle($query);
return $this->handle($query, $stamps);
}
}
Loading