-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserCreateCommand.php
More file actions
72 lines (62 loc) · 2.28 KB
/
Copy pathUserCreateCommand.php
File metadata and controls
72 lines (62 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace App\Command;
use App\Security\UserManager;
use Symfony\Component\Console\Attribute\AsCommand;
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\Style\SymfonyStyle;
/**
* Console command that creates a new application user.
*
* Thin adapter over {@see UserManager::createUser()}.
*/
#[AsCommand(
name: 'app:user:create',
description: 'Create a new application user with a hashed password.',
)]
final class UserCreateCommand extends Command
{
/**
* @param UserManager $userManager service that owns user creation
*/
public function __construct(private readonly UserManager $userManager)
{
parent::__construct();
}
/**
* Declare CLI arguments.
*/
protected function configure(): void
{
$this
->addArgument('email', InputArgument::REQUIRED, 'The user\'s e-mail address (must be unique).')
->addArgument('name', InputArgument::REQUIRED, 'The user\'s display name.')
->addArgument('password', InputArgument::REQUIRED, 'The user\'s password in clear-text — will be hashed.');
}
/**
* Adapt console arguments to the {@see UserManager} call.
*
* @param InputInterface $input CLI arguments
* @param OutputInterface $output console output stream
*
* @return int Command::SUCCESS on creation, Command::FAILURE on domain or validation error
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$email = (string) $input->getArgument('email');
$name = (string) $input->getArgument('name');
$password = (string) $input->getArgument('password');
try {
$user = $this->userManager->createUser($email, $name, $password);
} catch (\DomainException|\InvalidArgumentException $e) {
$io->error($e->getMessage());
return Command::FAILURE;
}
$io->success(\sprintf('Created user "%s" (id=%d).', $user->getUserIdentifier(), (int) $user->getId()));
return Command::SUCCESS;
}
}