@@ -61,6 +61,7 @@ protected function configure()
61
61
->addArgument ('username ' , InputArgument::OPTIONAL , 'The username of the new user ' )
62
62
->addArgument ('password ' , InputArgument::OPTIONAL , 'The plain password of the new user ' )
63
63
->addArgument ('email ' , InputArgument::OPTIONAL , 'The email of the new user ' )
64
+ ->addArgument ('full-name ' , InputArgument::OPTIONAL , 'The full name of the new user ' )
64
65
->addOption ('admin ' , null , InputOption::VALUE_NONE , 'If set, the user is created as an administrator ' )
65
66
;
66
67
}
@@ -90,7 +91,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
90
91
*/
91
92
protected function interact (InputInterface $ input , OutputInterface $ output )
92
93
{
93
- if (null !== $ input ->getArgument ('username ' ) && null !== $ input ->getArgument ('password ' ) && null !== $ input ->getArgument ('email ' )) {
94
+ if (null !== $ input ->getArgument ('username ' ) && null !== $ input ->getArgument ('password ' ) && null !== $ input ->getArgument ('email ' ) && null !== $ input -> getArgument ( ' full-name ' ) ) {
94
95
return ;
95
96
}
96
97
@@ -163,6 +164,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
163
164
} else {
164
165
$ output ->writeln (' > <info>Email</info>: ' .$ email );
165
166
}
167
+
168
+ // Ask for the full name if it's not defined
169
+ $ fullName = $ input ->getArgument ('full-name ' );
170
+ if (null === $ fullName ) {
171
+ $ question = new Question (' > <info>Full Name</info>: ' );
172
+ $ question ->setValidator ([$ this , 'fullNameValidator ' ]);
173
+ $ question ->setMaxAttempts (self ::MAX_ATTEMPTS );
174
+
175
+ $ fullName = $ console ->ask ($ input , $ output , $ question );
176
+ $ input ->setArgument ('full-name ' , $ fullName );
177
+ } else {
178
+ $ output ->writeln (' > <info>Full Name</info>: ' .$ fullName );
179
+ }
166
180
}
167
181
168
182
/**
@@ -176,13 +190,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
176
190
$ username = $ input ->getArgument ('username ' );
177
191
$ plainPassword = $ input ->getArgument ('password ' );
178
192
$ email = $ input ->getArgument ('email ' );
193
+ $ fullName = $ input ->getArgument ('full-name ' );
179
194
$ isAdmin = $ input ->getOption ('admin ' );
180
195
181
196
// make sure to validate the user data is correct
182
- $ this ->validateUserData ($ username , $ plainPassword , $ email );
197
+ $ this ->validateUserData ($ username , $ plainPassword , $ email, $ fullName );
183
198
184
199
// create the user and encode its password
185
200
$ user = new User ();
201
+ $ user ->setFullName ($ fullName );
186
202
$ user ->setUsername ($ username );
187
203
$ user ->setEmail ($ email );
188
204
$ user ->setRoles ([$ isAdmin ? 'ROLE_ADMIN ' : 'ROLE_USER ' ]);
@@ -244,7 +260,22 @@ public function emailValidator($email)
244
260
return $ email ;
245
261
}
246
262
247
- private function validateUserData ($ username , $ plainPassword , $ email )
263
+ /**
264
+ * This internal method should be private, but it's declared as public to
265
+ * maintain PHP 5.3 compatibility when using it in a callback.
266
+ *
267
+ * @internal
268
+ */
269
+ public function fullNameValidator ($ fullName )
270
+ {
271
+ if (empty ($ fullName )) {
272
+ throw new \Exception ('The full name can not be empty. ' );
273
+ }
274
+
275
+ return $ fullName ;
276
+ }
277
+
278
+ private function validateUserData ($ username , $ plainPassword , $ email , $ fullName )
248
279
{
249
280
$ userRepository = $ this ->entityManager ->getRepository (User::class);
250
281
@@ -258,6 +289,7 @@ private function validateUserData($username, $plainPassword, $email)
258
289
// validate password and email if is not this input means interactive.
259
290
$ this ->passwordValidator ($ plainPassword );
260
291
$ this ->emailValidator ($ email );
292
+ $ this ->fullNameValidator ($ fullName );
261
293
262
294
// check if a user with the same email already exists.
263
295
$ existingEmail = $ userRepository ->findOneBy (['email ' => $ email ]);
0 commit comments