|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: User |
| 5 | + * Date: 14/07/2019 |
| 6 | + * Time: 20:50 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace App\AuthModule\Command; |
| 10 | + |
| 11 | +use App\AuthModule\Entity\User; |
| 12 | +use App\AuthModule\Provider\DatabaseUserProvider; |
| 13 | +use Keiryo\Validation\Validator; |
| 14 | +use Keiryo\Helper\Str; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Helper\SymfonyQuestionHelper; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | +use Symfony\Component\Console\Question\Question; |
| 21 | + |
| 22 | +class CreateUserCommand extends Command |
| 23 | +{ |
| 24 | + |
| 25 | + /** |
| 26 | + * @var DatabaseUserProvider |
| 27 | + */ |
| 28 | + private $provider; |
| 29 | + /** |
| 30 | + * @var Validator |
| 31 | + */ |
| 32 | + private $validator; |
| 33 | + |
| 34 | + public function __construct(DatabaseUserProvider $provider, Validator $validator) |
| 35 | + { |
| 36 | + parent::__construct('auth:user-create'); |
| 37 | + $this->provider = $provider; |
| 38 | + $this->validator = $validator; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * {@inheritdoc} |
| 43 | + */ |
| 44 | + protected function configure() |
| 45 | + { |
| 46 | + $this->setDefinition([ |
| 47 | + new InputArgument('username', InputArgument::REQUIRED, 'User username'), |
| 48 | + new InputArgument('email', InputArgument::REQUIRED, 'User email'), |
| 49 | + new InputArgument('password', InputArgument::REQUIRED, 'User password'), |
| 50 | + ]) |
| 51 | + ->setDescription('Create a new admin user'); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param InputInterface $input |
| 56 | + * @param OutputInterface $output |
| 57 | + */ |
| 58 | + protected function interact(InputInterface $input, OutputInterface $output) |
| 59 | + { |
| 60 | + $helper = new SymfonyQuestionHelper(); |
| 61 | + $username = $helper->ask($input, $output, (new Question('Username')) |
| 62 | + ->setValidator(function (string $username) { |
| 63 | + $validation = $this->validator |
| 64 | + ->validate(compact('username'), ['username' => 'alpha_num']); |
| 65 | + return $validation->passes() |
| 66 | + ? $validation->getValidData()['username'] |
| 67 | + : null; |
| 68 | + })); |
| 69 | + |
| 70 | + $email = $helper->ask($input, $output, (new Question('Email')) |
| 71 | + ->setValidator(function (string $email) { |
| 72 | + $validation = $this->validator |
| 73 | + ->validate(compact('email'), ['email' => 'email']); |
| 74 | + return $validation->passes() |
| 75 | + ? $validation->getValidData()['email'] |
| 76 | + : null; |
| 77 | + })); |
| 78 | + |
| 79 | + $password = $helper->ask($input, $output, (new Question('Password')) |
| 80 | + ->setHidden(true) |
| 81 | + ->setValidator(function (string $password) { |
| 82 | + $validation = $this->validator |
| 83 | + ->validate(compact('password'), ['password' => 'min:8']); |
| 84 | + return $validation->passes() |
| 85 | + ? $validation->getValidData()['password'] |
| 86 | + : null; |
| 87 | + })); |
| 88 | + |
| 89 | + $helper->ask($input, $output, (new Question('Confirm password')) |
| 90 | + ->setHidden(true) |
| 91 | + ->setValidator(function (string $confirm) use ($password) { |
| 92 | + return $password === $confirm ? true : null; |
| 93 | + })); |
| 94 | + |
| 95 | + $input->setArgument('username', $username); |
| 96 | + $input->setArgument('email', $email); |
| 97 | + $input->setArgument('password', password_hash($password, PASSWORD_BCRYPT)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param InputInterface $input |
| 102 | + * @param OutputInterface $output |
| 103 | + * @return int|void|null |
| 104 | + * @throws \Exception |
| 105 | + */ |
| 106 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 107 | + { |
| 108 | + $token = Str::random(28); |
| 109 | + $user = new User($name = $input->getArgument('username'), $token, $input->getArgument('password')); |
| 110 | + $user->setEmail($input->getArgument('email')); |
| 111 | + $output->writeln("Creating user '$name'"); |
| 112 | + $this->provider->insert($user); |
| 113 | + $output->writeln("User '$name' successfully created"); |
| 114 | + } |
| 115 | +} |
0 commit comments