PersonRepository.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repository\Person;
  4. use App\Entity\Person\Person;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  10. /**
  11. * @method Person|null find($id, $lockMode = null, $lockVersion = null)
  12. * @method Person|null findOneBy(array $criteria, array $orderBy = null)
  13. * @method Person[] findAll()
  14. * @method Person[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15. */
  16. class PersonRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  17. {
  18. public function __construct(ManagerRegistry $registry)
  19. {
  20. parent::__construct($registry, Person::class);
  21. }
  22. /**
  23. * Used to upgrade (rehash) the user's password automatically over time.
  24. */
  25. public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newEncodedPassword): void
  26. {
  27. if (!$user instanceof Person) {
  28. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user)));
  29. }
  30. $user->setPassword($newEncodedPassword);
  31. $this->_em->persist($user);
  32. $this->_em->flush();
  33. }
  34. public function findOneByUsername(string $username): ?Person
  35. {
  36. return $this->findOneBy(['username' => $username]);
  37. }
  38. }