vendor/sylius/sylius/src/Sylius/Bundle/UserBundle/Provider/AbstractUserProvider.php line 35

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\UserBundle\Provider;
  12. use Sylius\Component\User\Canonicalizer\CanonicalizerInterface;
  13. use Sylius\Component\User\Model\UserInterface as SyliusUserInterface;
  14. use Sylius\Component\User\Repository\UserRepositoryInterface;
  15. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  16. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. abstract class AbstractUserProvider implements UserProviderInterface
  19. {
  20.     /**
  21.      * @param string $supportedUserClass FQCN
  22.      */
  23.     public function __construct(
  24.         protected string $supportedUserClass,
  25.         protected UserRepositoryInterface $userRepository,
  26.         protected CanonicalizerInterface $canonicalizer,
  27.     ) {
  28.     }
  29.     public function loadUserByUsername($username): UserInterface
  30.     {
  31.         $username $this->canonicalizer->canonicalize($username);
  32.         $user $this->findUser($username);
  33.         if (null === $user) {
  34.             throw new UsernameNotFoundException(
  35.                 sprintf('Username "%s" does not exist.'$username),
  36.             );
  37.         }
  38.         return $user;
  39.     }
  40.     public function refreshUser(UserInterface $user): UserInterface
  41.     {
  42.         if (!$user instanceof SyliusUserInterface) {
  43.             throw new UnsupportedUserException(
  44.                 sprintf('User must implement "%s".'SyliusUserInterface::class),
  45.             );
  46.         }
  47.         if (!$this->supportsClass($user::class)) {
  48.             throw new UnsupportedUserException(
  49.                 sprintf('Instances of "%s" are not supported.'$user::class),
  50.             );
  51.         }
  52.         /** @var UserInterface|null $reloadedUser */
  53.         $reloadedUser $this->userRepository->find($user->getId());
  54.         if (null === $reloadedUser) {
  55.             throw new UsernameNotFoundException(
  56.                 sprintf('User with ID "%d" could not be refreshed.'$user->getId()),
  57.             );
  58.         }
  59.         return $reloadedUser;
  60.     }
  61.     abstract protected function findUser(string $uniqueIdentifier): ?UserInterface;
  62.     public function supportsClass($class): bool
  63.     {
  64.         return $this->supportedUserClass === $class || is_subclass_of($class$this->supportedUserClass);
  65.     }
  66. }