@@ -14,15 +14,15 @@ such as:
14
14
- If the message is dispatched to a different bus, then the dispatched message can still
15
15
be handled even if the original handler encounters an exception.
16
16
17
- An Example ``SignUpUser `` Process
18
- ---------------------------------
17
+ An Example ``RegisterUser `` Process
18
+ -----------------------------------
19
19
20
20
Let's take the example of an application with both a *command * and an *event * bus. The application
21
- dispatches a command named ``SignUpUser `` to the command bus. The command is handled by the
22
- ``SignUpUserHandler `` which creates a ``User `` object, stores that object to a database and
23
- dispatches a ``UserSignedUp `` event to the event bus.
21
+ dispatches a command named ``RegisterUser `` to the command bus. The command is handled by the
22
+ ``RegisterUserHandler `` which creates a ``User `` object, stores that object to a database and
23
+ dispatches a ``UserRegistered `` event to the event bus.
24
24
25
- There are many subscribers to the ``UserSignedUp `` event, one subscriber may send
25
+ There are many subscribers to the ``UserRegistered `` event, one subscriber may send
26
26
a welcome email to the new user. We are using the ``DoctrineTransactionMiddleware ``
27
27
to wrap all database queries in one database transaction.
28
28
@@ -41,9 +41,9 @@ to `only` be handled after the handler finishes. This can be by using the
41
41
``DispatchAfterCurrentBusMiddleware `` middleware and adding a ``DispatchAfterCurrentBusStamp ``
42
42
stamp to `the message Envelope </components/messenger#adding-metadata-to-messages-envelopes >`_.
43
43
44
- Referencing the above example, this means that the ``UserSignedUp `` event would not be handled
45
- until *after * the ``SignUpUserHandler `` had completed and the new ``User `` was persisted to the
46
- database. If the ``SignUpUserHandler `` encounters an exception, the ``UserSignedUp `` event will
44
+ Referencing the above example, this means that the ``UserRegistered `` event would not be handled
45
+ until *after * the ``RegisterUserHandler `` had completed and the new ``User `` was persisted to the
46
+ database. If the ``RegisterUserHandler `` encounters an exception, the ``UserRegistered `` event will
47
47
never be handled and if an exception is thrown while sending the welcome email, the Doctrine
48
48
transaction will not be rolled back.
49
49
@@ -122,33 +122,33 @@ buses. For the example, the middleware must be loaded for both the command and e
122
122
namespace App\Messenger\CommandHandler;
123
123
124
124
use App\Entity\User;
125
- use App\Messenger\Command\SignUpUser ;
126
- use App\Messenger\Event\UserSignedUp ;
125
+ use App\Messenger\Command\RegisterUser ;
126
+ use App\Messenger\Event\UserRegistered ;
127
127
use Doctrine\ORM\EntityManagerInterface;
128
128
use Symfony\Component\Messenger\Envelope;
129
129
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
130
130
use Symfony\Component\Messenger\MessageBusInterface;
131
131
132
- class SignUpUserHandler
132
+ class RegisterUserHandler
133
133
{
134
- private $em;
135
134
private $eventBus;
135
+ private $em;
136
136
137
137
public function __construct(MessageBusInterface $eventBus, EntityManagerInterface $em)
138
138
{
139
139
$this->eventBus = $eventBus;
140
140
$this->em = $em;
141
141
}
142
142
143
- public function __invoke(SignUpUser $command)
143
+ public function __invoke(RegisterUser $command)
144
144
{
145
145
$user = new User($command->getUuid(), $command->getName(), $command->getEmail());
146
146
$this->em->persist($user);
147
147
148
148
// The DispatchAfterCurrentBusStamp marks the event message to be handled
149
149
// only if this handler does not throw an exception.
150
150
151
- $event = new UserSignedUp ($command->getUuid());
151
+ $event = new UserRegistered ($command->getUuid());
152
152
$this->eventBus->dispatch(
153
153
(new Envelope($event))
154
154
->with(new DispatchAfterCurrentBusStamp())
@@ -161,28 +161,32 @@ buses. For the example, the middleware must be loaded for both the command and e
161
161
namespace App\Messenger\EventSubscriber;
162
162
163
163
use App\Entity\User;
164
- use App\Messenger\Event\UserSignedUp ;
164
+ use App\Messenger\Event\UserRegistered ;
165
165
use Doctrine\ORM\EntityManagerInterface;
166
+ use Symfony\Component\Mailer\MailerInterface;
167
+ use Symfony\Component\Mime\RawMessage;
166
168
167
- class WhenUserSignedUpThenSendWelcomeEmail
169
+ class WhenUserRegisteredThenSendWelcomeEmail
168
170
{
169
- private $em;
170
171
private $mailer;
172
+ private $em;
171
173
172
- public function __construct(MyMailer $mailer, EntityManagerInterface $em)
174
+ public function __construct(MailerInterface $mailer, EntityManagerInterface $em)
173
175
{
174
176
$this->mailer = $mailer;
175
177
$this->em = $em;
176
178
}
177
179
178
- public function __invoke(UserSignedUp $event)
180
+ public function __invoke(UserRegistered $event)
179
181
{
180
182
$user = $this->em->getRepository(User::class)->find(new User($event->getUuid()));
181
183
182
- $this->mailer->sendWelcomeEmail( $user);
184
+ $this->mailer->send(new RawMessage('Welcome '. $user->getFirstName()) );
183
185
}
184
186
}
185
187
186
- **Note: ** If ``WhenUserSignedUpThenSendWelcomeEmail `` throws an exception, that exception
187
- will be wrapped into a ``DelayedMessageHandlingException ``. Using ``DelayedMessageHandlingException::getExceptions ``
188
- will give you all exceptions that are thrown while handing a message with the ``DispatchAfterCurrentBusStamp ``.
188
+ .. note ::
189
+
190
+ If ``WhenUserRegisteredThenSendWelcomeEmail `` throws an exception, that exception
191
+ will be wrapped into a ``DelayedMessageHandlingException ``. Using ``DelayedMessageHandlingException::getExceptions ``
192
+ will give you all exceptions that are thrown while handing a message with the ``DispatchAfterCurrentBusStamp ``.
0 commit comments