em = $em; } public function loadUserByUsername($username): UserInterface { try { $user = $this->em->createQuery(sprintf( 'SELECT u FROM %s u ' . 'WHERE u.usernameCanonical = UNACCENT(LOWER(:pattern)) ' . 'OR ' . 'u.emailCanonical = UNACCENT(LOWER(:pattern))', User::class )) ->setParameter('pattern', $username) ->getSingleResult(); } catch (NoResultException $e) { throw new UsernameNotFoundException( 'Bad credentials.', 0, $e ); } return $user; } public function refreshUser(UserInterface $user): UserInterface { if (!$user instanceof User) { throw new UnsupportedUserException('Unsupported user class: cannot reload this user'); } $reloadedUser = $this->em->getRepository(User::class)->find($user->getId()); if (null === $reloadedUser) { throw new UsernameNotFoundException(sprintf('User with ID "%s" could not be reloaded.', $user->getId())); } return $reloadedUser; } public function supportsClass($class): bool { return User::class === $class; } }