src/EventSubscriber/Identity/NewPasswordSubscriber.php line 23
<?php
namespace App\EventSubscriber\Identity;
use App\Entity\Identity\Password;
use App\Event\Identity\NewPasswordEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final readonly class NewPasswordSubscriber implements EventSubscriberInterface
{
public function __construct(
private EntityManagerInterface $entityManager,
) {}
public static function getSubscribedEvents(): array
{
return [
NewPasswordEvent::IDENTIFIER => 'process'
];
}
public function process(NewPasswordEvent $event): void
{
$user = $event->getUser();
// Expire previous password
$entities = $this->entityManager->getRepository(Password::class)->findBy([
'user' => $user,
'expired' => false
]);
foreach ($entities as $entity) {
$entity->setExpired(true);
$this->entityManager->persist($entity);
}
// Log new password
$entity = new Password();
$entity->setUser($user);
$entity->setValue($user->getPassword());
$this->entityManager->persist($entity);
$this->entityManager->flush();
}
}