2018-06-24 22:42:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\User;
|
|
|
|
|
|
|
|
use App\Entity\{Invite, User};
|
|
|
|
use App\Repository\{InviteRepository, UserRepository};
|
|
|
|
use App\User\Exception\InvalidInviteException;
|
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
|
|
|
|
|
|
|
class UserManager
|
|
|
|
{
|
|
|
|
private const DEFAULT_ROLES = ['ROLE_USER'];
|
|
|
|
|
|
|
|
/** @var UserRepository */
|
|
|
|
private $userRepo;
|
|
|
|
|
|
|
|
/** @var InviteRepository */
|
|
|
|
private $inviteRepo;
|
|
|
|
|
|
|
|
/** @var EncoderFactoryInterface */
|
|
|
|
private $encoderFactory;
|
|
|
|
|
|
|
|
public function __construct(EncoderFactoryInterface $encoderFactory, UserRepository $userRepo, InviteRepository $inviteRepo)
|
|
|
|
{
|
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
$this->inviteRepo = $inviteRepo;
|
|
|
|
$this->encoderFactory = $encoderFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function createUser(string $username, string $password, string $email, array $roles = self::DEFAULT_ROLES): User
|
|
|
|
{
|
|
|
|
$user = new User(
|
|
|
|
$username,
|
2020-01-20 17:01:51 +00:00
|
|
|
$this->encoderFactory->getEncoder(User::class),
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2020-01-20 17:01:51 +00:00
|
|
|
public function changePassword(User $user, string $rawPassword): void
|
|
|
|
{
|
|
|
|
$user->changePassword(
|
|
|
|
$this->encoderFactory->getEncoder(User::class),
|
|
|
|
$rawPassword
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|