Skip to content

Commit c17ea71

Browse files
committed
[Messenger] EnvelopeItemInterface::isTransportable(): bool
1 parent a9acb32 commit c17ea71

File tree

7 files changed

+73
-13
lines changed

7 files changed

+73
-13
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
1616
use Symfony\Component\Messenger\Envelope;
1717
use Symfony\Component\Messenger\EnvelopeAwareInterface;
18+
use Symfony\Component\Messenger\EnvelopeItemInterface;
1819
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
1920

2021
/**
@@ -40,6 +41,10 @@ public function handle($message, callable $next)
4041
return $next($message);
4142
}
4243

44+
$envelope = new Envelope($envelope->getMessage(), array_filter($envelope->all(), function (EnvelopeItemInterface $item): bool {
45+
return $item->isTransportable();
46+
}));
47+
4348
if (!empty($senders = $this->senderLocator->getSendersForMessage($envelope->getMessage()))) {
4449
foreach ($senders as $sender) {
4550
if (null === $sender) {

src/Symfony/Component/Messenger/Asynchronous/Transport/ReceivedMessage.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
*/
2626
final class ReceivedMessage implements EnvelopeItemInterface
2727
{
28-
public function serialize()
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function isTransportable(): bool
2932
{
30-
return '';
31-
}
32-
33-
public function unserialize($serialized)
34-
{
35-
// noop
33+
return false;
3634
}
3735
}

src/Symfony/Component/Messenger/EnvelopeItemInterface.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@
1313

1414
/**
1515
* An envelope item related to a message.
16-
* This item must be serializable for transport.
1716
*
1817
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
1918
*
2019
* @experimental in 4.1
2120
*/
22-
interface EnvelopeItemInterface extends \Serializable
21+
interface EnvelopeItemInterface
2322
{
23+
/**
24+
* @return bool True if this item can be transported. Otherwise, it'll be ignored during send.
25+
*/
26+
public function isTransportable(): bool;
2427
}

src/Symfony/Component/Messenger/Middleware/Configuration/ValidationConfiguration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @experimental in 4.1
2121
*/
22-
final class ValidationConfiguration implements EnvelopeItemInterface
22+
final class ValidationConfiguration implements EnvelopeItemInterface, \Serializable
2323
{
2424
private $groups;
2525

@@ -36,6 +36,14 @@ public function getGroups()
3636
return $this->groups;
3737
}
3838

39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function isTransportable(): bool
43+
{
44+
return true;
45+
}
46+
3947
public function serialize()
4048
{
4149
$isGroupSequence = $this->groups instanceof GroupSequence;

src/Symfony/Component/Messenger/Tests/Asynchronous/Middleware/SendMessageMiddlewareTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
1717
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
1818
use Symfony\Component\Messenger\Envelope;
19+
use Symfony\Component\Messenger\EnvelopeItemInterface;
1920
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
2021
use Symfony\Component\Messenger\Transport\SenderInterface;
2122

@@ -37,6 +38,27 @@ public function testItSendsTheMessageToAssignedSender()
3738
$middleware->handle($message, $next);
3839
}
3940

41+
public function testItSendsEnvelopeWithTransportableItemsOnly()
42+
{
43+
$envelope = Envelope::wrap(new DummyMessage('Hey'))
44+
->with(new TransportableItem())
45+
->with(new NonTransportableItem())
46+
;
47+
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
48+
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));
49+
50+
$middleware = new SendMessageMiddleware(new InMemorySenderLocator(array(
51+
$sender,
52+
)));
53+
54+
$sender->expects($this->once())->method('send')->with(Envelope::wrap(new DummyMessage('Hey'))
55+
->with(new TransportableItem())
56+
);
57+
$next->expects($this->never())->method($this->anything());
58+
59+
$middleware->handle($envelope, $next);
60+
}
61+
4062
public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage()
4163
{
4264
$envelope = Envelope::wrap(new DummyMessage('Hey'));
@@ -114,3 +136,19 @@ public function getSendersForMessage($message): array
114136
return $this->senders;
115137
}
116138
}
139+
140+
class TransportableItem implements EnvelopeItemInterface
141+
{
142+
public function isTransportable(): bool
143+
{
144+
return true;
145+
}
146+
}
147+
148+
class NonTransportableItem implements EnvelopeItemInterface
149+
{
150+
public function isTransportable(): bool
151+
{
152+
return false;
153+
}
154+
}

src/Symfony/Component/Messenger/Transport/Serialization/Serializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function encode(Envelope $envelope): array
6868
}
6969

7070
$headers = array('type' => \get_class($envelope->getMessage()));
71-
if ($configurations = $envelope->all()) {
72-
$headers['X-Message-Envelope-Items'] = serialize($configurations);
71+
if ($items = $envelope->all()) {
72+
$headers['X-Message-Envelope-Items'] = serialize($items);
7373
}
7474

7575
return array(

src/Symfony/Component/Messenger/Transport/Serialization/SerializerConfiguration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @experimental in 4.1
2020
*/
21-
final class SerializerConfiguration implements EnvelopeItemInterface
21+
final class SerializerConfiguration implements EnvelopeItemInterface, \Serializable
2222
{
2323
private $context;
2424

@@ -32,6 +32,14 @@ public function getContext(): array
3232
return $this->context;
3333
}
3434

35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function isTransportable(): bool
39+
{
40+
return true;
41+
}
42+
3543
public function serialize()
3644
{
3745
return serialize(array('context' => $this->context));

0 commit comments

Comments
 (0)