I have a Mailer
service which is responsible to create and send emails. I also have Registration
service which confirm a user registration.
What are the benefits to dispatch a custom event, subscribe to it and then send the email with the Mailer
service instead of just using the Mailer
service in the Registration
by injecting it?
I'm a little bit confused. The first one seems to be harder to maintain but I've seeing it too many time that maybe I should consider it.
In other words. Should I do this :
class Registration
{
public function __construct(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
public function confirm()
{
// ...
$this->eventDispatcher->dispatch('myApp.registration.confirmed', new MyCustomEvent());
}
}
class RegistrationSubscriber implements EventSubscriberInterface
{
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
// ...
public function onRegistrationConfirmed(MyCustomEvent $event)
{
// ...
$this->mailer->sendRegistrationConfirmedEmail();
}
}
Or this :
class Registration
{
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function confirm()
{
// ...
$this->mailer->sendRegistrationConfirmedEmail();
}
}