@@ -14,15 +14,15 @@ such as:
1414- If the message is dispatched to a different bus, then the dispatched message can still
1515 be handled even if the original handler encounters an exception.
1616
17- An Example ``SignUpUser `` Process
18- ---------------------------------
17+ An Example ``RegisterUser `` Process
18+ -----------------------------------
1919
2020Let'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.
2424
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
2626a welcome email to the new user. We are using the ``DoctrineTransactionMiddleware ``
2727to wrap all database queries in one database transaction.
2828
@@ -41,9 +41,9 @@ to `only` be handled after the handler finishes. This can be by using the
4141``DispatchAfterCurrentBusMiddleware `` middleware and adding a ``DispatchAfterCurrentBusStamp ``
4242stamp to `the message Envelope </components/messenger#adding-metadata-to-messages-envelopes >`_.
4343
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
4747never be handled and if an exception is thrown while sending the welcome email, the Doctrine
4848transaction will not be rolled back.
4949
@@ -122,33 +122,33 @@ buses. For the example, the middleware must be loaded for both the command and e
122122 namespace App\Messenger\CommandHandler;
123123
124124 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 ;
127127 use Doctrine\ORM\EntityManagerInterface;
128128 use Symfony\Component\Messenger\Envelope;
129129 use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
130130 use Symfony\Component\Messenger\MessageBusInterface;
131131
132- class SignUpUserHandler
132+ class RegisterUserHandler
133133 {
134- private $em;
135134 private $eventBus;
135+ private $em;
136136
137137 public function __construct(MessageBusInterface $eventBus, EntityManagerInterface $em)
138138 {
139139 $this->eventBus = $eventBus;
140140 $this->em = $em;
141141 }
142142
143- public function __invoke(SignUpUser $command)
143+ public function __invoke(RegisterUser $command)
144144 {
145145 $user = new User($command->getUuid(), $command->getName(), $command->getEmail());
146146 $this->em->persist($user);
147147
148148 // The DispatchAfterCurrentBusStamp marks the event message to be handled
149149 // only if this handler does not throw an exception.
150150
151- $event = new UserSignedUp ($command->getUuid());
151+ $event = new UserRegistered ($command->getUuid());
152152 $this->eventBus->dispatch(
153153 (new Envelope($event))
154154 ->with(new DispatchAfterCurrentBusStamp())
@@ -161,28 +161,32 @@ buses. For the example, the middleware must be loaded for both the command and e
161161 namespace App\Messenger\EventSubscriber;
162162
163163 use App\Entity\User;
164- use App\Messenger\Event\UserSignedUp ;
164+ use App\Messenger\Event\UserRegistered ;
165165 use Doctrine\ORM\EntityManagerInterface;
166+ use Symfony\Component\Mailer\MailerInterface;
167+ use Symfony\Component\Mime\RawMessage;
166168
167- class WhenUserSignedUpThenSendWelcomeEmail
169+ class WhenUserRegisteredThenSendWelcomeEmail
168170 {
169- private $em;
170171 private $mailer;
172+ private $em;
171173
172- public function __construct(MyMailer $mailer, EntityManagerInterface $em)
174+ public function __construct(MailerInterface $mailer, EntityManagerInterface $em)
173175 {
174176 $this->mailer = $mailer;
175177 $this->em = $em;
176178 }
177179
178- public function __invoke(UserSignedUp $event)
180+ public function __invoke(UserRegistered $event)
179181 {
180182 $user = $this->em->getRepository(User::class)->find(new User($event->getUuid()));
181183
182- $this->mailer->sendWelcomeEmail( $user);
184+ $this->mailer->send(new RawMessage('Welcome '. $user->getFirstName()) );
183185 }
184186 }
185187
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