Last active
February 2, 2019 22:57
-
-
Save rabbl/d28b347e06f0107b80c11fd36612d2d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// src/Command/CreateUserCommand.php | |
declare(strict_types=1); | |
namespace App\Command; | |
use App\Entity\User; | |
use Doctrine\ORM\EntityManagerInterface; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
final class CreateUserCommand extends Command | |
{ | |
protected static $defaultName = 'app:create-user'; | |
/** @var EntityManagerInterface */ | |
private $entityManager; | |
public function __construct(EntityManagerInterface $entityManager) | |
{ | |
$this->entityManager = $entityManager; | |
parent::__construct(); | |
} | |
protected function configure(): void | |
{ | |
$this | |
->setDescription('Creates a new user.') | |
->addArgument('username', InputArgument::REQUIRED, 'Username') | |
->addArgument('password', InputArgument::REQUIRED, 'User password') | |
->addArgument('role', InputArgument::OPTIONAL, 'Users role') | |
->setHelp('This command allows you to create a user...') | |
; | |
} | |
/** | |
* @param InputInterface $input | |
* @param OutputInterface $output | |
* @return int|void|null | |
* @throws \Exception | |
*/ | |
protected function execute(InputInterface $input, OutputInterface $output) | |
{ | |
$username = $input->getArgument('username'); | |
$password = $input->getArgument('password'); | |
$role = $input->getArgument('role') ?? 'ROLE_USER'; | |
if (strlen($username) < 5) { | |
$output->writeln('Username too short, length must be >= 5 !'); | |
return; | |
} | |
$userRepository = $this->entityManager->getRepository(User::class); | |
$user = $userRepository->findOneBy(['username' => $username]); | |
if ($user instanceof User) { | |
$output->writeln('Username already taken!'); | |
return; | |
} | |
$user = new User($username, $password, [$role]); | |
$this->entityManager->persist($user); | |
$this->entityManager->flush(); | |
$output->writeln('User successfully generated!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment