<?php
namespace App\EventSubscriber;
use App\Entity\Site\User;
use App\Service\EmailService;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
class EasyAdminSubscriber implements EventSubscriberInterface
{
private $resetPasswordHelper, $emailService;
public function __construct(ResetPasswordHelperInterface $resetPasswordHelper, EmailService $emailService )
{
$this->emailService = $emailService;
$this->resetPasswordHelper = $resetPasswordHelper;
}
public static function getSubscribedEvents()
{
return [
AfterEntityPersistedEvent::class => ['sendUserPasswordEmail'],
];
}
public function sendUserPasswordEmail(AfterEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof User)) {
return;
}
$resetToken = $this->resetPasswordHelper->generateResetToken($entity);
$this->emailService->send([$entity->getEmail()], "email/accountCreated.html.twig", "Création de votre mot de passe", ['resetToken' => $resetToken, ] );
}
}