1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Command; |
4
|
|
|
|
5
|
|
|
use App\Manager\UserManagerInterface; |
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
7
|
|
|
use Exception; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
12
|
|
|
use Symfony\Component\Console\Input\InputOption; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
15
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
16
|
|
|
|
17
|
|
|
class UserCreateCommand extends Command |
18
|
|
|
{ |
19
|
|
|
protected static $defaultName = 'app:user:create'; |
20
|
|
|
|
21
|
|
|
private EntityManagerInterface $entityManager; |
22
|
|
|
private UserPasswordEncoderInterface $passwordEncoder; |
23
|
|
|
private UserManagerInterface $userManager; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
UserPasswordEncoderInterface $passwordEncoder, |
27
|
|
|
EntityManagerInterface $entityManager, |
28
|
|
|
UserManagerInterface $userManager |
29
|
|
|
) { |
30
|
|
|
parent::__construct(); |
31
|
|
|
$this->passwordEncoder = $passwordEncoder; |
32
|
|
|
$this->entityManager = $entityManager; |
33
|
|
|
$this->userManager = $userManager; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setDescription('Create new user') |
40
|
|
|
->addArgument('email', InputArgument::REQUIRED, 'User email') |
41
|
|
|
->addArgument('password', InputArgument::REQUIRED, 'User password') |
42
|
|
|
->addArgument('firstName', InputArgument::REQUIRED, 'User first name') |
43
|
|
|
->addArgument('lastName', InputArgument::REQUIRED, 'User last name') |
44
|
|
|
->addOption('courses', null, InputOption::VALUE_REQUIRED, 'Courses assigned to user("," sperated by title)'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
$io = new SymfonyStyle($input, $output); |
50
|
|
|
$email = $input->getArgument('email'); |
51
|
|
|
|
52
|
|
|
if (!is_string($email)) { |
53
|
|
|
throw new InvalidArgumentException('Provided email must be string!'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$user = $this->userManager->getOrCreateUser($email); |
57
|
|
|
if (null !== $user->getId()) { |
58
|
|
|
throw new Exception('User with provided email already exists!'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$user->setEmail($email); |
62
|
|
|
$user->setRoles(['ROLE_USER']); |
63
|
|
|
$generatedPassword = $this->passwordEncoder->encodePassword($user, (string) $input->getArgument('password')); |
64
|
|
|
$user->setPassword($generatedPassword); |
65
|
|
|
$user->setFirstName((string) $input->getArgument('firstName')); |
66
|
|
|
$user->setLastName((string) $input->getArgument('lastName')); |
67
|
|
|
|
68
|
|
|
if ($input->hasOption('courses')) { |
69
|
|
|
$coursesTitles = explode(',', $input->getOption('courses')); |
70
|
|
|
foreach ($coursesTitles as $coursesTitle) { |
71
|
|
|
$this->userManager->addCourseByTitleOrSku($user, $coursesTitle); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->entityManager->persist($user); |
76
|
|
|
$this->entityManager->flush(); |
77
|
|
|
|
78
|
|
|
$io->success(sprintf('User was created successfully.')); |
79
|
|
|
$io->note('Use app:user:promote to add new roles for this user'); |
80
|
|
|
|
81
|
|
|
return 1; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|