Skip to content

Commit 5dfc18e

Browse files
authored
Messenger: add support for HandledStamp (#2420)
* Messenger: add support for HandledStamp * messenger test handledstamp
1 parent a3f6167 commit 5dfc18e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/Bridge/Symfony/Messenger/DataPersister.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Core\Util\ClassInfoTrait;
1919
use Symfony\Component\Messenger\Envelope;
2020
use Symfony\Component\Messenger\MessageBusInterface;
21+
use Symfony\Component\Messenger\Stamp\HandledStamp;
2122

2223
/**
2324
* Dispatches the given resource using the message bus of Symfony Messenger.
@@ -52,9 +53,12 @@ public function supports($data): bool
5253
*/
5354
public function persist($data)
5455
{
55-
$this->messageBus->dispatch($data);
56+
$envelope = $this->messageBus->dispatch($data);
57+
if (null === $stamp = $envelope->last(HandledStamp::class)) {
58+
return $data;
59+
}
5660

57-
return $data;
61+
return $stamp->getResult();
5862
}
5963

6064
/**

tests/Bridge/Symfony/Messenger/DataPersisterTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Prophecy\Argument;
2323
use Symfony\Component\Messenger\Envelope;
2424
use Symfony\Component\Messenger\MessageBusInterface;
25+
use Symfony\Component\Messenger\Stamp\HandledStamp;
2526

2627
/**
2728
* @author Kévin Dunglas <[email protected]>
@@ -60,4 +61,15 @@ public function testRemove()
6061
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
6162
$dataPersister->remove($dummy);
6263
}
64+
65+
public function testHandle()
66+
{
67+
$dummy = new Dummy();
68+
69+
$messageBus = $this->prophesize(MessageBusInterface::class);
70+
$messageBus->dispatch($dummy)->willReturn(new Envelope(new \stdClass(), new HandledStamp('result', 'DummyHandler::__invoke')))->shouldBeCalled();
71+
72+
$dataPersister = new DataPersister($this->prophesize(ResourceMetadataFactoryInterface::class)->reveal(), $messageBus->reveal());
73+
$this->assertSame('result', $dataPersister->persist($dummy));
74+
}
6375
}

0 commit comments

Comments
 (0)