<?php

declare(strict_types=1);

namespace KupShop\TwoFactorBundle\Security;

use KupShop\UserBundle\Security\User;
use KupShop\UserBundle\Security\UserProvider;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;

class TwoFactorUserProvider extends UserProvider
{
    public function loadUserByUsername($username)
    {
        if (!$email = $this->getEmailByUsername($username)) {
            throw new UserNotFoundException();
        }

        $user = \User::createFromLogin($email);
        if (!isset($user)) {
            throw new UserNotFoundException();
        }

        return new TwoFactorUser($user->id, $user->email, $user->passw, ['ROLE_USER'], $user);
    }

    public function supportsClass($class): bool
    {
        return $class === TwoFactorUser::class || $class === User::class;
    }
}
