Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| UserProvider | |
100.00% |
12 / 12 |
|
100.00% |
4 / 4 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| loadUserByIdentifier | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| refreshUser | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| supportsClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Identity\Infrastructure\Security; |
| 6 | |
| 7 | use App\Identity\Domain\UserRepository; |
| 8 | use App\Identity\Domain\ValueObject\Email; |
| 9 | use InvalidArgumentException; |
| 10 | use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
| 11 | use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
| 12 | use Symfony\Component\Security\Core\User\UserInterface; |
| 13 | use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 14 | |
| 15 | /** |
| 16 | * @implements UserProviderInterface<SecurityUser> |
| 17 | */ |
| 18 | final class UserProvider implements UserProviderInterface |
| 19 | { |
| 20 | public function __construct(private readonly UserRepository $users) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | public function loadUserByIdentifier(string $identifier): UserInterface |
| 25 | { |
| 26 | try { |
| 27 | $email = new Email($identifier); |
| 28 | } catch (InvalidArgumentException) { |
| 29 | throw new UserNotFoundException(); |
| 30 | } |
| 31 | |
| 32 | $user = $this->users->ofEmail($email); |
| 33 | |
| 34 | if ($user === null) { |
| 35 | throw new UserNotFoundException(); |
| 36 | } |
| 37 | |
| 38 | return SecurityUser::fromDomain($user); |
| 39 | } |
| 40 | |
| 41 | public function refreshUser(UserInterface $user): UserInterface |
| 42 | { |
| 43 | if (!$user instanceof SecurityUser) { |
| 44 | throw new UnsupportedUserException(sprintf('Unsupported user class "%s".', $user::class)); |
| 45 | } |
| 46 | |
| 47 | return $this->loadUserByIdentifier($user->getUserIdentifier()); |
| 48 | } |
| 49 | |
| 50 | public function supportsClass(string $class): bool |
| 51 | { |
| 52 | return $class === SecurityUser::class || is_subclass_of($class, SecurityUser::class); |
| 53 | } |
| 54 | } |