@@ -111,10 +111,10 @@ that will do the required processing for your message::
111111
112112 class MyMessageHandler
113113 {
114- public function __invoke(MyMessage $message)
115- {
116- // Message processing...
117- }
114+ public function __invoke(MyMessage $message)
115+ {
116+ // Message processing...
117+ }
118118 }
119119
120120Adding Metadata to Messages (Envelopes)
@@ -199,13 +199,12 @@ transport will be responsible for communicating with your message broker or 3rd
199199Your own Sender
200200~~~~~~~~~~~~~~~
201201
202- Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
203- you can create your own message sender.
204202Imagine that you already have an ``ImportantAction `` message going through the
205203message bus and being handled by a handler. Now, you also want to send this
206204message as an email.
207205
208- First, create your sender::
206+ Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
207+ you can create your own message sender::
209208
210209 namespace App\MessageSender;
211210
@@ -215,34 +214,34 @@ First, create your sender::
215214
216215 class ImportantActionToEmailSender implements SenderInterface
217216 {
218- private $mailer;
219- private $toEmail;
220-
221- public function __construct(\Swift_Mailer $mailer, string $toEmail)
222- {
223- $this->mailer = $mailer;
224- $this->toEmail = $toEmail;
225- }
226-
227- public function send(Envelope $envelope): Envelope
228- {
229- $message = $envelope->getMessage();
230-
231- if (!$message instanceof ImportantAction) {
232- throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', ImportantAction::class));
233- }
234-
235- $this->mailer->send(
236- (new \Swift_Message('Important action made'))
237- ->setTo($this->toEmail)
238- ->setBody(
239- '<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>',
240- 'text/html'
241- )
242- );
243-
244- return $envelope;
245- }
217+ private $mailer;
218+ private $toEmail;
219+
220+ public function __construct(\Swift_Mailer $mailer, string $toEmail)
221+ {
222+ $this->mailer = $mailer;
223+ $this->toEmail = $toEmail;
224+ }
225+
226+ public function send(Envelope $envelope): Envelope
227+ {
228+ $message = $envelope->getMessage();
229+
230+ if (!$message instanceof ImportantAction) {
231+ throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', ImportantAction::class));
232+ }
233+
234+ $this->mailer->send(
235+ (new \Swift_Message('Important action made'))
236+ ->setTo($this->toEmail)
237+ ->setBody(
238+ '<h1>Important action</h1><p>Made by '.$message->getUsername().'</p>',
239+ 'text/html'
240+ )
241+ );
242+
243+ return $envelope;
244+ }
246245 }
247246
248247Your own Receiver
@@ -257,9 +256,7 @@ application but you can't use an API and need to use a shared CSV file with new
257256orders.
258257
259258You will read this CSV file and dispatch a ``NewOrder `` message. All you need to
260- do is to write your custom CSV receiver and Symfony will do the rest.
261-
262- First, create your receiver::
259+ do is to write your own CSV receiver::
263260
264261 namespace App\MessageReceiver;
265262
@@ -270,30 +267,30 @@ First, create your receiver::
270267
271268 class NewOrdersFromCsvFileReceiver implements ReceiverInterface
272269 {
273- private $serializer;
274- private $filePath;
270+ private $serializer;
271+ private $filePath;
275272
276- public function __construct(SerializerInterface $serializer, string $filePath)
277- {
273+ public function __construct(SerializerInterface $serializer, string $filePath)
274+ {
278275 $this->serializer = $serializer;
279276 $this->filePath = $filePath;
280- }
277+ }
281278
282- public function receive(callable $handler): void
283- {
284- $ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
279+ public function receive(callable $handler): void
280+ {
281+ $ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
285282
286- foreach ($ordersFromCsv as $orderFromCsv) {
287- $order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);
283+ foreach ($ordersFromCsv as $orderFromCsv) {
284+ $order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);
288285
289- $handler(new Envelope($order));
290- }
291- }
286+ $handler(new Envelope($order));
287+ }
288+ }
292289
293- public function stop(): void
294- {
295- // noop
296- }
290+ public function stop(): void
291+ {
292+ // noop
293+ }
297294 }
298295
299296Receiver and Sender on the same Bus
@@ -306,6 +303,7 @@ middleware will know it should not route these messages again to a transport.
306303
307304Learn more
308305----------
306+
309307.. toctree ::
310308 :maxdepth: 1
311309 :glob:
0 commit comments