2018-06-24 22:42:26 +00:00
|
|
|
<?php
|
2022-07-10 13:51:26 +00:00
|
|
|
declare(strict_types=1);
|
2018-06-24 22:42:26 +00:00
|
|
|
|
|
|
|
namespace App\User;
|
|
|
|
|
|
|
|
use App\Entity\{Invite, User};
|
2022-07-10 13:51:26 +00:00
|
|
|
use App\Repository\UserRepository;
|
2018-06-24 22:42:26 +00:00
|
|
|
use App\User\Exception\InvalidInviteException;
|
2022-07-10 13:51:26 +00:00
|
|
|
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
|
2018-06-24 22:42:26 +00:00
|
|
|
|
|
|
|
class UserManager
|
|
|
|
{
|
|
|
|
private const DEFAULT_ROLES = ['ROLE_USER'];
|
|
|
|
|
2022-07-10 13:51:26 +00:00
|
|
|
public function __construct(
|
|
|
|
private readonly PasswordHasherFactoryInterface $hasherFactory,
|
|
|
|
private readonly UserRepository $userRepo,
|
|
|
|
) {
|
2018-06-24 22:42:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createUser(string $username, string $password, string $email, array $roles = self::DEFAULT_ROLES): User
|
|
|
|
{
|
|
|
|
$user = new User(
|
|
|
|
$username,
|
2022-07-10 13:51:26 +00:00
|
|
|
$this->hasherFactory->getPasswordHasher(User::class),
|
2020-01-20 17:01:51 +00:00
|
|
|
$password,
|
2018-06-24 22:42:26 +00:00
|
|
|
$email,
|
|
|
|
$roles
|
|
|
|
);
|
|
|
|
|
2018-06-28 22:40:14 +00:00
|
|
|
$this->userRepo->add($user);
|
|
|
|
|
2018-06-24 22:42:26 +00:00
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2018-06-28 23:14:36 +00:00
|
|
|
public function createUserByInvite(string $username, string $password, string $email, Invite $invite, array $roles = self::DEFAULT_ROLES): User
|
2018-06-24 22:42:26 +00:00
|
|
|
{
|
2018-06-28 23:14:36 +00:00
|
|
|
if (null !== $invite->getUsedBy()) {
|
2018-06-24 22:42:26 +00:00
|
|
|
throw new InvalidInviteException();
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:01:51 +00:00
|
|
|
$user = $this->createUser($username, $password, $email, $roles);
|
2018-06-24 22:42:26 +00:00
|
|
|
|
|
|
|
$invite->use($user);
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
}
|
2020-01-20 17:01:51 +00:00
|
|
|
}
|