@@ -61,6 +61,7 @@ protected function configure()
6161 ->addArgument ('username ' , InputArgument::OPTIONAL , 'The username of the new user ' )
6262 ->addArgument ('password ' , InputArgument::OPTIONAL , 'The plain password of the new user ' )
6363 ->addArgument ('email ' , InputArgument::OPTIONAL , 'The email of the new user ' )
64+ ->addArgument ('full-name ' , InputArgument::OPTIONAL , 'The full name of the new user ' )
6465 ->addOption ('admin ' , null , InputOption::VALUE_NONE , 'If set, the user is created as an administrator ' )
6566 ;
6667 }
@@ -90,7 +91,7 @@ protected function initialize(InputInterface $input, OutputInterface $output)
9091 */
9192 protected function interact (InputInterface $ input , OutputInterface $ output )
9293 {
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 ' ) ) {
9495 return ;
9596 }
9697
@@ -163,6 +164,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
163164 } else {
164165 $ output ->writeln (' > <info>Email</info>: ' .$ email );
165166 }
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+ }
166180 }
167181
168182 /**
@@ -176,13 +190,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
176190 $ username = $ input ->getArgument ('username ' );
177191 $ plainPassword = $ input ->getArgument ('password ' );
178192 $ email = $ input ->getArgument ('email ' );
193+ $ fullName = $ input ->getArgument ('full-name ' );
179194 $ isAdmin = $ input ->getOption ('admin ' );
180195
181196 // make sure to validate the user data is correct
182- $ this ->validateUserData ($ username , $ plainPassword , $ email );
197+ $ this ->validateUserData ($ username , $ plainPassword , $ email, $ fullName );
183198
184199 // create the user and encode its password
185200 $ user = new User ();
201+ $ user ->setFullName ($ fullName );
186202 $ user ->setUsername ($ username );
187203 $ user ->setEmail ($ email );
188204 $ user ->setRoles ([$ isAdmin ? 'ROLE_ADMIN ' : 'ROLE_USER ' ]);
@@ -244,7 +260,22 @@ public function emailValidator($email)
244260 return $ email ;
245261 }
246262
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 )
248279 {
249280 $ userRepository = $ this ->entityManager ->getRepository (User::class);
250281
@@ -258,6 +289,7 @@ private function validateUserData($username, $plainPassword, $email)
258289 // validate password and email if is not this input means interactive.
259290 $ this ->passwordValidator ($ plainPassword );
260291 $ this ->emailValidator ($ email );
292+ $ this ->fullNameValidator ($ fullName );
261293
262294 // check if a user with the same email already exists.
263295 $ existingEmail = $ userRepository ->findOneBy (['email ' => $ email ]);
0 commit comments