add command to create a user
This commit is contained in:
parent
99a2cc87d3
commit
4f6e4eaa3f
73
core/Command/UserCreateCommand.php
Normal file
73
core/Command/UserCreateCommand.php
Normal file
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\Command;
|
||||
|
||||
use App\Core\Factory\UserFactory;
|
||||
use App\Core\Manager\EntityManager;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Question\ConfirmationQuestion;
|
||||
use Symfony\Component\Console\Question\Question;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class UserCreateCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'murph:user:create';
|
||||
protected static $defaultDescription = 'Creates a user';
|
||||
protected UserFactory $userFactory;
|
||||
protected EntityManager $entityManager;
|
||||
|
||||
public function __construct(UserFactory $userFactory, EntityManager $entityManager)
|
||||
{
|
||||
$this->userFactory = $userFactory;
|
||||
$this->entityManager = $entityManager;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription(self::$defaultDescription)
|
||||
->addArgument('email', InputArgument::OPTIONAL, 'E-mail')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$helper = $this->getHelper('question');
|
||||
|
||||
$emailQuestion = new Question('E-mail: ');
|
||||
$emailQuestion->setValidator(function ($value) {
|
||||
return !empty($value);
|
||||
});
|
||||
|
||||
$passwordQuestion = new Question('Password (leave empty to generate a random password): ');
|
||||
$passwordQuestion->setHidden(true);
|
||||
$isAdminQuestion = new ConfirmationQuestion('Is admin? [y/n] ', false);
|
||||
$isWriterQuestion = new ConfirmationQuestion('Is writer? [y/n] ', false);
|
||||
|
||||
$email = $input->getArgument('email');
|
||||
|
||||
if (empty($email)) {
|
||||
$email = $helper->ask($input, $output, $emailQuestion);
|
||||
}
|
||||
|
||||
$password = $helper->ask($input, $output, $passwordQuestion);
|
||||
$isAdmin = $helper->ask($input, $output, $isAdminQuestion);
|
||||
$isWriter = $helper->ask($input, $output, $isWriterQuestion);
|
||||
|
||||
$user = $this->userFactory->create($email, $password);
|
||||
$user->setIsAdmin($isAdmin);
|
||||
$user->setIsWriter($isWriter);
|
||||
|
||||
$this->entityManager->create($user);
|
||||
|
||||
$io->success('User created!');
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
|
@ -22,13 +22,17 @@ class UserFactory
|
|||
$this->encoder = $encoder;
|
||||
}
|
||||
|
||||
public function create(): User
|
||||
public function create(?string $email = null, ?string $password = null): User
|
||||
{
|
||||
$entity = new User();
|
||||
|
||||
if (!empty($email)) {
|
||||
$entity->setEmail($email);
|
||||
}
|
||||
|
||||
$entity->setPassword($this->encoder->encodePassword(
|
||||
$entity,
|
||||
$this->tokenGenerator->generateToken()
|
||||
!empty($password) ? $password : $this->tokenGenerator->generateToken()
|
||||
));
|
||||
|
||||
return $entity;
|
||||
|
|
Loading…
Reference in a new issue