generated from dunglas/symfony-docker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCreateUserCommand.php
98 lines (81 loc) · 2.86 KB
/
CreateUserCommand.php
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace App\Command\Security;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Subscription\Plan\BusinessPlan;
use Doctrine\ORM\EntityManagerInterface;
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;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
/**
* Enables creation of a user via the command line.
*/
#[AsCommand(name: 'app:security:create-user')]
class CreateUserCommand extends Command
{
/**
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
private readonly UserRepository $userRepository,
private readonly EntityManagerInterface $entityManager,
private readonly UserPasswordHasherInterface $passwordHasher,
) {
parent::__construct();
}
protected function configure(): void
{
$this->setDescription('Creates a new Koalati user.')
->addArgument('email', InputArgument::OPTIONAL, 'Email address:')
->addArgument('first_name', InputArgument::OPTIONAL, 'First name:')
->addArgument('password', InputArgument::OPTIONAL, 'Password:')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$users = $this->userRepository->findAll();
$existingEmails = array_map(fn (User $user) => strtolower($user->getEmail()), $users);
$email = $input->getArgument("email");
$firstName = trim((string) $input->getArgument("first_name"));
$plainPassword = trim((string) $input->getArgument("password"));
if (!$email) {
$email = $io->ask("Email address", null, function (?string $email) use ($existingEmails) {
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new \RuntimeException("Invalid email address");
}
if (in_array($email, $existingEmails)) {
throw new \RuntimeException("A user with this email address already exists.");
}
return trim($email);
});
}
$requiredValidator = function (?string $value) {
if (!strlen(trim($value))) {
throw new \RuntimeException("This field is required.");
}
return trim($value);
};
if (!$firstName) {
$firstName = $io->ask("First name", null, $requiredValidator);
}
if (!$plainPassword) {
$plainPassword = $io->askHidden("Password", $requiredValidator);
}
$user = new User();
$user
->setSubscriptionPlan(BusinessPlan::UNIQUE_NAME)
->setEmail($email)
->setFirstName($firstName)
->setPassword($this->passwordHasher->hashPassword($user, $plainPassword))
->setIsVerified(true);
$this->entityManager->persist($user);
$this->entityManager->flush();
$io->success("The user has been created!");
return Command::SUCCESS;
}
}