<?php
/**
* This file is part of an Innatial Developers development or collaboration.
*
* @author Innatial Developers, S.L.
* @link https://innatial.com
* @copyright Copyright (c) 2020. Innatial Developers, S.L.
*
* All rights are reserved.
* Copyright (c) 2020. Innatial Developers, S.L.
*/
namespace App\EventSubscriber;
use App\Event\UserCreatedEvent;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Templating\EngineInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserCreatedSubscriber implements EventSubscriberInterface{
protected $mailer;
protected $twig;
protected $translator;
protected $dm;
public function __construct(\Swift_Mailer $mailer, EngineInterface $twig, TranslatorInterface $translator, EntityManagerInterface $dm)
{
$this->mailer = $mailer;
$this->twig = $twig;
$this->translator = $translator;
$this->dm = $dm;
}
/**
* @return array
* @throws \Exception
*/
public static function getSubscribedEvents()
{
return [
UserCreatedEvent::NAME => 'onUserCreatedEvent'
];
}
public function onUserCreatedEvent(UserCreatedEvent $event){
$user = $event->getUser();
$message = (new \Swift_Message($this->translator->trans('auth.mailer.templates.user_new.subject')))
->setTo($user->getEmail())
->setBody(
$this->twig->render(
'mailer/user_new.html.twig',
['user' => $user]
),
'text/html'
);
$this->mailer->send($message);
}
}