msgphp / symfony-demo-app

A Symfony demo application with basic user management
https://github.com/msgphp/msgphp
MIT License
0 stars 0 forks source link

Question: why controllers do not check for command result? #48

Closed danaki closed 6 years ago

danaki commented 6 years ago

An example code from RegisterController:

$bus->handle(new CreateUserCommand($data = $form->getData()));
$flashBag->add('success', sprintf('Hi %s, you\'re successfully registered. We\'ve send you a confirmation link.', $data['email']));
return new RedirectResponse($urlGenerator->generate('index'));

If I understand CreateUserCommand() doesn't imply an intermediate result (or throws an Exception?). And even can be handled delayed in asynchronous way, then how to deal with this situation from a web application point of view? Should controller be aware is it setup to be asynchronous or should a client request flash messages repeatedly in a separate REST call?

ro0NL commented 6 years ago

HI @danaki

Your application is either aware a call is made async or sync, i tend to believe this is a design-choice of your app. Typically user registration happens in sync.

On the technical side, yes we can benefit from return values coming from message handlers when we use Symfony Messenger (i never did that though using SimpleBus). I.e.

$userId = $bus->handle(new CreateUserCommand($data = $form->getData()));

Cosmetically we can add return values anyway, to be leveraged when using SF Messenger. It's on my list to switch the demo app to do so.

Alternatively, and this is my common approach, is to generate the user UUID upfront.

$userId = new UserId();
$bus->handle(new CreateUserCommand(['id' => $userId] + $data = $form->getData()));
ro0NL commented 6 years ago

Assuming we can close but feel free to re-open :)