63 lines
1.6 KiB
PHP
63 lines
1.6 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Repository;
|
||
|
|
|
||
|
|
use App\Entity\PasswordResetToken;
|
||
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||
|
|
use Doctrine\Persistence\ManagerRegistry;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @extends ServiceEntityRepository<PasswordResetToken>
|
||
|
|
*/
|
||
|
|
class PasswordResetTokenRepository extends ServiceEntityRepository
|
||
|
|
{
|
||
|
|
public function __construct(ManagerRegistry $registry)
|
||
|
|
{
|
||
|
|
parent::__construct($registry, PasswordResetToken::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function save(PasswordResetToken $entity, bool $flush = false): void
|
||
|
|
{
|
||
|
|
$this->getEntityManager()->persist($entity);
|
||
|
|
|
||
|
|
if ($flush) {
|
||
|
|
$this->getEntityManager()->flush();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function remove(PasswordResetToken $entity, bool $flush = false): void
|
||
|
|
{
|
||
|
|
$this->getEntityManager()->remove($entity);
|
||
|
|
|
||
|
|
if ($flush) {
|
||
|
|
$this->getEntityManager()->flush();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function findByToken(string $token): ?PasswordResetToken
|
||
|
|
{
|
||
|
|
return $this->findOneBy(['token' => $token]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function findValidByToken(string $token): ?PasswordResetToken
|
||
|
|
{
|
||
|
|
$tokenEntity = $this->findByToken($token);
|
||
|
|
|
||
|
|
if ($tokenEntity && $tokenEntity->isValid()) {
|
||
|
|
return $tokenEntity;
|
||
|
|
}
|
||
|
|
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public function removeExpiredTokens(): int
|
||
|
|
{
|
||
|
|
$qb = $this->createQueryBuilder('t');
|
||
|
|
$qb->delete()
|
||
|
|
->where('t.expiresAt < :now')
|
||
|
|
->setParameter('now', new \DateTime());
|
||
|
|
|
||
|
|
return $qb->getQuery()->execute();
|
||
|
|
}
|
||
|
|
}
|