forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SetupCommand.php
220 lines (182 loc) · 7.55 KB
/
SetupCommand.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Bundle\CoreBundle\Command;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Webmozart\Assert\Assert;
final class SetupCommand extends AbstractInstallCommand
{
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setName('sylius:install:setup')
->setDescription('Sylius configuration setup.')
->setHelp(<<<EOT
The <info>%command.name%</info> command allows user to configure basic Sylius data.
EOT
)
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$currency = $this->getContainer()->get('sylius.setup.currency')->setup($input, $output, $this->getHelper('question'));
$locale = $this->getContainer()->get('sylius.setup.locale')->setup($input, $output, $this->getHelper('question'));
$this->getContainer()->get('sylius.setup.channel')->setup($locale, $currency);
$this->setupAdministratorUser($input, $output, $locale->getCode());
return 0;
}
protected function setupAdministratorUser(InputInterface $input, OutputInterface $output, string $localeCode): void
{
$outputStyle = new SymfonyStyle($input, $output);
$outputStyle->writeln('Create your administrator account.');
$userManager = $this->getContainer()->get('sylius.manager.admin_user');
$userFactory = $this->getContainer()->get('sylius.factory.admin_user');
try {
$user = $this->configureNewUser($userFactory->createNew(), $input, $output);
} catch (\InvalidArgumentException $exception) {
return;
}
$user->setEnabled(true);
$user->setLocaleCode($localeCode);
$userManager->persist($user);
$userManager->flush();
$outputStyle->writeln('<info>Administrator account successfully registered.</info>');
$outputStyle->newLine();
}
private function configureNewUser(
AdminUserInterface $user,
InputInterface $input,
OutputInterface $output
): AdminUserInterface {
/** @var UserRepositoryInterface $userRepository */
$userRepository = $this->getAdminUserRepository();
if ($input->getOption('no-interaction')) {
Assert::null($userRepository->findOneByEmail('sylius@example.com'));
$user->setEmail('sylius@example.com');
$user->setUsername('sylius');
$user->setPlainPassword('sylius');
return $user;
}
$email = $this->getAdministratorEmail($input, $output);
$user->setEmail($email);
$user->setUsername($this->getAdministratorUsername($input, $output, $email));
$user->setPlainPassword($this->getAdministratorPassword($input, $output));
return $user;
}
private function createEmailQuestion(): Question
{
return (new Question('E-mail: '))
->setValidator(
/**
* @param mixed $value
*/
function ($value): string {
/** @var ConstraintViolationListInterface $errors */
$errors = $this->getContainer()->get('validator')->validate((string) $value, [new Email(), new NotBlank()]);
foreach ($errors as $error) {
throw new \DomainException($error->getMessage());
}
return $value;
}
)
->setMaxAttempts(3)
;
}
private function getAdministratorEmail(InputInterface $input, OutputInterface $output): string
{
/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');
/** @var UserRepositoryInterface $userRepository */
$userRepository = $this->getAdminUserRepository();
do {
$question = $this->createEmailQuestion();
$email = $questionHelper->ask($input, $output, $question);
$exists = null !== $userRepository->findOneByEmail($email);
if ($exists) {
$output->writeln('<error>E-Mail is already in use!</error>');
}
} while ($exists);
return $email;
}
private function getAdministratorUsername(InputInterface $input, OutputInterface $output, string $email): string
{
/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');
/** @var UserRepositoryInterface $userRepository */
$userRepository = $this->getAdminUserRepository();
do {
$question = new Question('Username (press enter to use email): ', $email);
$username = $questionHelper->ask($input, $output, $question);
$exists = null !== $userRepository->findOneBy(['username' => $username]);
if ($exists) {
$output->writeln('<error>Username is already in use!</error>');
}
} while ($exists);
return $username;
}
private function getAdministratorPassword(InputInterface $input, OutputInterface $output): string
{
/** @var QuestionHelper $questionHelper */
$questionHelper = $this->getHelper('question');
$validator = $this->getPasswordQuestionValidator();
do {
$passwordQuestion = $this->createPasswordQuestion('Choose password:', $validator);
$confirmPasswordQuestion = $this->createPasswordQuestion('Confirm password:', $validator);
$password = $questionHelper->ask($input, $output, $passwordQuestion);
$repeatedPassword = $questionHelper->ask($input, $output, $confirmPasswordQuestion);
if ($repeatedPassword !== $password) {
$output->writeln('<error>Passwords do not match!</error>');
}
} while ($repeatedPassword !== $password);
return $password;
}
private function getPasswordQuestionValidator(): \Closure
{
return
/** @param mixed $value */
function ($value): string {
/** @var ConstraintViolationListInterface $errors */
$errors = $this->getContainer()->get('validator')->validate($value, [new NotBlank()]);
foreach ($errors as $error) {
throw new \DomainException($error->getMessage());
}
return $value;
}
;
}
private function createPasswordQuestion(string $message, \Closure $validator): Question
{
return (new Question($message))
->setValidator($validator)
->setMaxAttempts(3)
->setHidden(true)
->setHiddenFallback(false)
;
}
private function getAdminUserRepository(): UserRepositoryInterface
{
return $this->getContainer()->get('sylius.repository.admin_user');
}
}