<?php
namespace MLDev\AccountBundle\Event\Subscriber;
use MLDev\AccountBundle\Event\RecoveryAccount;
use Swift_Mailer;
use Swift_Message;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Twig\Environment;
class RecoverySubscriber implements EventSubscriberInterface
{
/**
* @var Swift_Mailer
*/
private $mailer;
/**
* @var Environment
*/
private $twig;
/**
* @var array
*/
private $parameters;
/**
* RecoverySubscriber constructor.
*/
public function __construct(Swift_Mailer $mailer, Environment $twig, array $parameters)
{
$this->parameters = $parameters;
$this->mailer = $mailer;
$this->twig = $twig;
}
public static function getSubscribedEvents()
{
return [
RecoveryAccount::NOTIFY_AFTER => [
['onNotifyAfter', 10]
]
];
}
public function onNotifyAfter(RecoveryAccount $event)
{
if (!filter_var($event->getAccount()->getEmail(), FILTER_VALIDATE_EMAIL)) {
return null;
}
$mldevAccountMailFrom = $this->parameters['from'] ?? 'info@mldev.ru';
$message = new Swift_Message('Восстановление пароля');
$message->setFrom($mldevAccountMailFrom);
$message->setTo($event->getAccount()->getEmail());
$message->setBody(
$this->twig->render(
'@MLDevAccount/event/recovery/letter.html.twig', [
'account' => $event->getAccount(),
]
), 'text/html'
);
$this->mailer->send($message);
}
}