Skip to content

Commit 2f044ea

Browse files
Add stamps to handle trait
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
1 parent aa23093 commit 2f044ea

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add `$stamps = []` parameter to `HandleTrait`
8+
49
7.2
510
---
611

src/Symfony/Component/Messenger/HandleTrait.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Messenger\Exception\LogicException;
1515
use Symfony\Component\Messenger\Stamp\HandledStamp;
16+
use Symfony\Component\Messenger\Stamp\StampInterface;
1617

1718
/**
1819
* Leverages a message bus to expect a single, synchronous message handling and return its result.
@@ -29,15 +30,16 @@ trait HandleTrait
2930
* This behavior is useful for both synchronous command & query buses,
3031
* the last one usually returning the handler result.
3132
*
32-
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
33+
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
34+
* @param StampInterface[] $stamps Stamps to be set on the Envelope which are used to control middlewares behaviors
3335
*/
34-
private function handle(object $message): mixed
36+
private function handle(object $message, array $stamps = []): mixed
3537
{
3638
if (!isset($this->messageBus)) {
3739
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));
3840
}
3941

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

@@ -48,7 +50,7 @@ private function handle(object $message): mixed
4850
if (\count($handledStamps) > 1) {
4951
$handlers = implode(', ', array_map(fn (HandledStamp $stamp): string => \sprintf('"%s"', $stamp->getHandlerName()), $handledStamps));
5052

51-
throw new LogicException(\sprintf('Message of type "%s" was handled multiple times. Only one handler is expected when using "%s::%s()", got %d: %s.', get_debug_type($envelope->getMessage()), static::class, __FUNCTION__, \count($handledStamps), $handlers));
53+
throw new LogicException(\sprintf('Message of type "%s" was handled multiple times. Only one handler is expected when using "%s::%s()", got %d: "%s".', get_debug_type($envelope->getMessage()), static::class, __FUNCTION__, \count($handledStamps), $handlers));
5254
}
5355

5456
return $handledStamps[0]->getResult();

src/Symfony/Component/Messenger/MessageBusInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface MessageBusInterface
2323
* Dispatches the given message.
2424
*
2525
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
26-
* @param StampInterface[] $stamps
26+
* @param StampInterface[] $stamps Stamps set on the Envelope which are used to control middlewares behaviors
2727
*
2828
* @throws ExceptionInterface
2929
*/

src/Symfony/Component/Messenger/Tests/HandleTraitTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Messenger\MessageBus;
1919
use Symfony\Component\Messenger\MessageBusInterface;
2020
use Symfony\Component\Messenger\Stamp\HandledStamp;
21+
use Symfony\Component\Messenger\Stamp\StampInterface;
2122
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2223

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

60+
public function testHandleWithStamps()
61+
{
62+
$bus = $this->createMock(MessageBus::class);
63+
$queryBus = new TestQueryBus($bus);
64+
$stamp = $this->createMock(StampInterface::class);
65+
66+
$query = new DummyMessage('Hello');
67+
$bus->expects($this->once())->method('dispatch')->with($query, [$stamp])->willReturn(
68+
new Envelope($query, [new HandledStamp('result', 'DummyHandler::__invoke')])
69+
);
70+
71+
$queryBus->query($query, [$stamp]);
72+
}
73+
5974
public function testHandleThrowsOnNoHandledStamp()
6075
{
6176
$this->expectException(LogicException::class);
@@ -96,8 +111,8 @@ public function __construct(?MessageBusInterface $messageBus)
96111
}
97112
}
98113

99-
public function query($query): string
114+
public function query($query, array $stamps = []): string
100115
{
101-
return $this->handle($query);
116+
return $this->handle($query, $stamps);
102117
}
103118
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy