I'm trying to figure out how to best use events with a command bus. Does the following code look reasonable? It seems odd that the ResendConfirmationEmailHandler
doesn't actually send an email or even have a mailer
.
class ResendConfirmationEmailHandler
{
/**
* @var UserRepository
*/
private $users;
public function handle(ResendConfirmationEmail $command)
{
$user = $this->users->findById($command->getUserId());
// User entity raises a ConfirmationTokenChange event
// Listener will pick that up and do the actual sending
// after the new token is persisted
$user->setConfirmationToken(new ConfirmationToken());
$this->users->add($user);
}
}
class SendAccountConfirmationEmailListener
{
/**
* @var UserRepository
*/
private $users;
/**
* @var UserMailer
*/
private $mailer;
public function onUserRegistered(UserRegistered $event)
{
$this->sendEmail($event->getUserId());
}
public function onConfirmationTokenChange(ConfirmationTokenChanged $event)
{
$this->sendEmail($event->getUserId());
}
private sendEmail(UserId $userId)
{
$user = $this->users->findById($userId;
$this->mailer->sendAccountConfirmationEmail($user);
}
}