Skip to content

Commit

Permalink
Merge branch '4.4'
Browse files Browse the repository at this point in the history
* 4.4:
  [Messenger] `get()` usage
  • Loading branch information
javiereguiluz committed Sep 26, 2019
2 parents 86d2c25 + b607ee3 commit d08f681
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions components/messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ do is to write your own CSV receiver::

use App\Message\NewOrder;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\MessageDecodingFailedException;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Serializer\SerializerInterface;

Expand All @@ -280,17 +281,23 @@ do is to write your own CSV receiver::

public function get(): iterable
{
$ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');

foreach ($ordersFromCsv as $orderFromCsv) {
$order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);

$envelope = new Envelope($order);
// Receive the envelope according to your transport ($yourEnvelope here),
// in most cases, using a connection is the easiest solution.
if (null === $yourEnvelope) {
return [];
}

$handler($envelope);
try {
$envelope = $this->serializer->decode([
'body' => $yourEnvelope['body'],
'headers' => $yourEnvelope['headers'],
]);
} catch (MessageDecodingFailedException $exception) {
$this->connection->reject($yourEnvelope['id']);
throw $exception;
}

return [$envelope];
return [$envelope->with(new CustomStamp($yourEnvelope['id']);
}

public function ack(Envelope $envelope): void
Expand All @@ -300,7 +307,8 @@ do is to write your own CSV receiver::

public function reject(Envelope $envelope): void
{
// Reject the message if needed
// In the case of a custom connection
$this->connection->reject($this->findCustomStamp($envelope)->getId());
}
}

Expand Down

0 comments on commit d08f681

Please sign in to comment.