<?php
declare(strict_types=1);
namespace App\Infrastructure\Auth\Symfony\Security\Provider;
use App\Domain\Shared\Exception\NotFoundException;
use App\Domain\Site\Model\Site;
use App\Domain\Site\Repository\SiteRepositoryInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Uid\Uuid;
class SiteLoginProvider implements UserProviderInterface
{
public function __construct(
private readonly SiteRepositoryInterface $siteRepository
) {
}
/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user): Site
{
return $this->loadUserByIdentifier($user->getUserIdentifier());
}
public function supportsClass(string $class): bool
{
return Site::class === $class;
}
public function loadUserByIdentifier(string $identifier): Site
{
if (!Uuid::isValid($identifier)) {
throw new UserNotFoundException('Site not found');
}
try {
$site = $this->siteRepository->findById(Uuid::fromString($identifier));
} catch (NotFoundException $exception) {
throw new UserNotFoundException('Site not found');
}
return $site;
}
}