<?php
declare(strict_types=1);
namespace Slivki\Security\Provider;
use Doctrine\ORM\EntityManagerInterface;
use Slivki\Entity\User;
use Slivki\Repository\User\UserRepositoryInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
final class UserProvider implements UserProviderInterface
{
private UserRepositoryInterface $userRepository;
private EntityManagerInterface $entityManager;
public function __construct(UserRepositoryInterface $userRepository, EntityManagerInterface $entityManager)
{
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
}
public function refreshUser(UserInterface $user): UserInterface
{
if (!$user instanceof User) {
throw new UnsupportedUserException(\sprintf('Instances of "%s" are not supported.', \get_debug_type($user)));
}
return $this->userRepository->getById($user->getID());
}
public function supportsClass(string $class): bool
{
return $class === User::class || \is_subclass_of($class, User::class);
}
/**
* @return UserInterface|User|null
*/
public function loadUserByUsername(string $username): ?UserInterface
{
return $this->entityManager->getRepository(User::class)->loadUserByUsername($username);
}
}