<?php
namespace Witalink\StarterBundle\EventSubscriber\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Witalink\StarterBundle\EventSubscriber\Event\UserEvent;
use Witalink\StarterBundle\EventSubscriber\CustomEvents;
use Witalink\StarterBundle\Mailer\SecurityMailer;
class UserEmailSubscriber implements EventSubscriberInterface
{
/**
* @var SecurityMailer
*/
private $mailer;
public function __construct(SecurityMailer $mailer)
{
$this->mailer = $mailer;
}
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
CustomEvents::USER_REGISTRED => [
[
'dispatchSelfRegistrationEmail',
// Define the priority if necessary
],
],
CustomEvents::USER_CREATED => [
[
'dispatchRegistrationEmail',
// Define the priority if necessary
],
],
CustomEvents::USER_FORGET_PASSWORD => [
[
'dispatchForgetPasswordEmail',
// Define the priority if necessary
],
],
];
}
/**
* Dispatch an email once a user is created. This allow him/her to create a password and
* access the account.
*
* @param UserEvent $event
*/
public function dispatchRegistrationEmail(UserEvent $event)
{
$this->mailer->sendRegisterEmail($event->getUser(), false);
}
public function dispatchSelfRegistrationEmail(UserEvent $event)
{
$this->mailer->sendRegisterEmail($event->getUser(), true);
}
public function dispatchForgetPasswordEmail(UserEvent $event)
{
$this->mailer->sendForgetPasswordEmail($event->getUser());
}
}