-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Notifier] Add Documentation for Events #15597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
.. index:: | ||
single: Notifier; Events | ||
|
||
Using Events | ||
============ | ||
|
||
The class:``...\\..\\Transport`` of the Notifier component allows you to optionally hook | ||
into the lifecycle via events. | ||
|
||
The ``MessageEvent::class`` Event | ||
--------------------------------- | ||
|
||
**Typical Purposes**: Doing something before the message is send (like logging | ||
which message is going to be send, or displaying something about the event | ||
to be executed. | ||
|
||
Just before send the message, the event class ``MessageEvent`` is | ||
dispatched. Listeners receive a | ||
:class:`Symfony\\Component\\Notifier\\Event\\MessageEvent` event:: | ||
|
||
use Symfony\Component\Notifier\Event\MessageEvent; | ||
|
||
$dispatcher->addListener(MessageEvent::class, function (MessageEvent $event) { | ||
// gets the message instance | ||
$message = $event->getMessage(); | ||
|
||
// log something | ||
$this->logger(sprintf('Message with subject: %s will be send to %s, $message->getSubject(), $message->getRecipientId()')); | ||
}); | ||
|
||
The ``FailedMessageEvent`` Event | ||
-------------------------------- | ||
|
||
**Typical Purposes**: Doing something before the exception is thrown (Retry to send the message or log additional information). | ||
|
||
Whenever an exception is thrown while sending the message, the event class ``FailedMessageEvent`` is | ||
dispatched. A listener can do anything useful before the exception is thrown. | ||
|
||
Listeners receive a | ||
:class:`Symfony\\Component\\Notifier\\Event\\FailedMessageEvent` event:: | ||
|
||
use Symfony\Component\Notifier\Event\FailedMessageEvent; | ||
|
||
$dispatcher->addListener(FailedMessageEvent::class, function (FailedMessageEvent $event) { | ||
// gets the message instance | ||
$message = $event->getMessage(); | ||
|
||
// gets the error instance | ||
$error = $event->getError(); | ||
|
||
// log something | ||
$this->logger(sprintf('The message with subject: %s has not been sent successfully. The error is: %s, $message->getSubject(), $error->getMessage()')); | ||
}); | ||
|
||
|
||
The ``SentMessageEvent`` Event | ||
------------------------------ | ||
|
||
**Typical Purposes**: To perform some action when the message is successfully sent (like retrieve the id returned | ||
when the message is sent). | ||
|
||
After the message has been successfully sent, the event class ``SentMessageEvent`` is | ||
dispatched. Listeners receive a | ||
:class:`Symfony\\Component\\Notifier\\Event\\SentMessageEvent` event:: | ||
|
||
use Symfony\Component\Notifier\Event\SentMessageEvent; | ||
|
||
$dispatcher->addListener(SentMessageEvent::class, function (SentMessageEvent $event) { | ||
// gets the message instance | ||
$message = $event->getOriginalMessage(); | ||
|
||
// log something | ||
$this->logger(sprintf('The message has been successfully sent and have id: %s, $message->getMessageId()')); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.