@@ -111,10 +111,10 @@ that will do the required processing for your message::
111
111
112
112
class MyMessageHandler
113
113
{
114
- public function __invoke(MyMessage $message)
115
- {
116
- // Message processing...
117
- }
114
+ public function __invoke(MyMessage $message)
115
+ {
116
+ // Message processing...
117
+ }
118
118
}
119
119
120
120
Adding Metadata to Messages (Envelopes)
@@ -199,13 +199,12 @@ transport will be responsible for communicating with your message broker or 3rd
199
199
Your own Sender
200
200
~~~~~~~~~~~~~~~
201
201
202
- Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
203
- you can create your own message sender.
204
202
Imagine that you already have an ``ImportantAction `` message going through the
205
203
message bus and being handled by a handler. Now, you also want to send this
206
204
message as an email.
207
205
208
- First, create your sender::
206
+ Using the :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Sender\\ SenderInterface `,
207
+ you can create your own message sender::
209
208
210
209
namespace App\MessageSender;
211
210
@@ -215,34 +214,34 @@ First, create your sender::
215
214
216
215
class ImportantActionToEmailSender implements SenderInterface
217
216
{
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
+ }
246
245
}
247
246
248
247
Your own Receiver
@@ -257,9 +256,7 @@ application but you can't use an API and need to use a shared CSV file with new
257
256
orders.
258
257
259
258
You 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::
263
260
264
261
namespace App\MessageReceiver;
265
262
@@ -270,30 +267,30 @@ First, create your receiver::
270
267
271
268
class NewOrdersFromCsvFileReceiver implements ReceiverInterface
272
269
{
273
- private $serializer;
274
- private $filePath;
270
+ private $serializer;
271
+ private $filePath;
275
272
276
- public function __construct(SerializerInterface $serializer, string $filePath)
277
- {
273
+ public function __construct(SerializerInterface $serializer, string $filePath)
274
+ {
278
275
$this->serializer = $serializer;
279
276
$this->filePath = $filePath;
280
- }
277
+ }
281
278
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');
285
282
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']);
288
285
289
- $handler(new Envelope($order));
290
- }
291
- }
286
+ $handler(new Envelope($order));
287
+ }
288
+ }
292
289
293
- public function stop(): void
294
- {
295
- // noop
296
- }
290
+ public function stop(): void
291
+ {
292
+ // noop
293
+ }
297
294
}
298
295
299
296
Receiver and Sender on the same Bus
@@ -306,6 +303,7 @@ middleware will know it should not route these messages again to a transport.
306
303
307
304
Learn more
308
305
----------
306
+
309
307
.. toctree ::
310
308
:maxdepth: 1
311
309
:glob:
0 commit comments