Skip to content

Commit 3502463

Browse files
committed
minor #11889 [Messenger] get() usage (Guikingone)
This PR was squashed before being merged into the 4.3 branch (closes #11889). Discussion ---------- [Messenger] `get()` usage Hi everyone 👋 As noticed here #11861 (comment) by @xabbuh, a small error has been found in the `get()` documentation. This PR contains a small improvement. Commits ------- 5ccad7f [Messenger] `get()` usage
2 parents 6724aca + 5ccad7f commit 3502463

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

components/messenger.rst

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ do is to write your own CSV receiver::
270270

271271
use App\Message\NewOrder;
272272
use Symfony\Component\Messenger\Envelope;
273+
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
273274
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
274275
use Symfony\Component\Serializer\SerializerInterface;
275276

@@ -286,17 +287,24 @@ do is to write your own CSV receiver::
286287

287288
public function get(): iterable
288289
{
289-
$ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
290-
291-
foreach ($ordersFromCsv as $orderFromCsv) {
292-
$order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);
293-
294-
$envelope = new Envelope($order);
295-
296-
$handler($envelope);
290+
// Receive the envelope according to your transport ($yourEnvelope here),
291+
// in most cases, using a connection is the easiest solution.
292+
293+
if (null === $yourEnvelope) {
294+
return [];
297295
}
298-
299-
return [$envelope];
296+
297+
try {
298+
$envelope = $this->serializer->decode([
299+
'body' => $yourEnvelope['body'],
300+
'headers' => $yourEnvelope['headers'],
301+
]);
302+
} catch (MessageDecodingFailedException $exception) {
303+
$this->connection->reject($yourEnvelope['id']);
304+
throw $exception;
305+
}
306+
307+
return [$yourEnvelope->with(new CustomStamp($yourEnvelope['id']);
300308
}
301309

302310
public function ack(Envelope $envelope): void
@@ -306,7 +314,8 @@ do is to write your own CSV receiver::
306314

307315
public function reject(Envelope $envelope): void
308316
{
309-
// Reject the message if needed
317+
// In the case of a custom connection
318+
$this->connection->reject($this->findCustomStamp($envelope)->getId());
310319
}
311320
}
312321

0 commit comments

Comments
 (0)