Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 426aeca

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: Add versionadded directive init doc
2 parents a6d3b73 + 8606870 commit 426aeca

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

notifier/events.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
.. index::
2+
single: Notifier; Events
3+
4+
Using Events
5+
============
6+
7+
.. versionadded:: 5.4
8+
9+
The ``MessageEvent``, ``FailedMessageEvent`` and ``SentMessageEvent`` were
10+
introduced in Symfony 5.4.
11+
12+
The class:``...\\..\\Transport`` of the Notifier component allows you to optionally hook
13+
into the lifecycle via events.
14+
15+
The ``MessageEvent::class`` Event
16+
---------------------------------
17+
18+
**Typical Purposes**: Doing something before the message is send (like logging
19+
which message is going to be send, or displaying something about the event
20+
to be executed.
21+
22+
Just before send the message, the event class ``MessageEvent`` is
23+
dispatched. Listeners receive a
24+
:class:`Symfony\\Component\\Notifier\\Event\\MessageEvent` event::
25+
26+
use Symfony\Component\Notifier\Event\MessageEvent;
27+
28+
$dispatcher->addListener(MessageEvent::class, function (MessageEvent $event) {
29+
// gets the message instance
30+
$message = $event->getMessage();
31+
32+
// log something
33+
$this->logger(sprintf('Message with subject: %s will be send to %s, $message->getSubject(), $message->getRecipientId()'));
34+
});
35+
36+
The ``FailedMessageEvent`` Event
37+
--------------------------------
38+
39+
**Typical Purposes**: Doing something before the exception is thrown (Retry to send the message or log additional information).
40+
41+
Whenever an exception is thrown while sending the message, the event class ``FailedMessageEvent`` is
42+
dispatched. A listener can do anything useful before the exception is thrown.
43+
44+
Listeners receive a
45+
:class:`Symfony\\Component\\Notifier\\Event\\FailedMessageEvent` event::
46+
47+
use Symfony\Component\Notifier\Event\FailedMessageEvent;
48+
49+
$dispatcher->addListener(FailedMessageEvent::class, function (FailedMessageEvent $event) {
50+
// gets the message instance
51+
$message = $event->getMessage();
52+
53+
// gets the error instance
54+
$error = $event->getError();
55+
56+
// log something
57+
$this->logger(sprintf('The message with subject: %s has not been sent successfully. The error is: %s, $message->getSubject(), $error->getMessage()'));
58+
});
59+
60+
61+
The ``SentMessageEvent`` Event
62+
------------------------------
63+
64+
**Typical Purposes**: To perform some action when the message is successfully sent (like retrieve the id returned
65+
when the message is sent).
66+
67+
After the message has been successfully sent, the event class ``SentMessageEvent`` is
68+
dispatched. Listeners receive a
69+
:class:`Symfony\\Component\\Notifier\\Event\\SentMessageEvent` event::
70+
71+
use Symfony\Component\Notifier\Event\SentMessageEvent;
72+
73+
$dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) {
74+
// gets the message instance
75+
$message = $event->getOriginalMessage();
76+
77+
// log something
78+
$this->logger(sprintf('The message has been successfully sent and have id: %s, $message->getMessageId()'));
79+
});

0 commit comments

Comments
 (0)