Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadamssg/05ffd3a9cb17001b4061 to your computer and use it in GitHub Desktop.
Save dadamssg/05ffd3a9cb17001b4061 to your computer and use it in GitHub Desktop.

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);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment