firstname = null; $this->addFirstname(); $this->lastname = null; $this->addLastname(); $this->email = null; $this->addEmail(); $this->password = null; $this->addPassword(); $user = new User([ 'firstname' => $this->firstname, 'lastname' => $this->lastname, 'email' => $this->email, 'password' => $this->password, ]); $user->saveOrFail(); $this->info('User created 👌'); return 0; } /** * Add a firstname to the user. * * @return void */ private function addFirstname(): void { while (is_null($this->firstname)) { try { $tmp = $this->ask('Type the wanted user firstname', 'Admin'); $validator = Validator::make(['firstname' => $tmp], [ 'firstname' => 'required|min:3|max:255' ]); $validator->validate(); $this->firstname = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid firstname %s', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } //end try }; //end while } /** * Add a lastname to the user. * * @return void */ private function addLastname(): void { while (is_null($this->lastname)) { try { $tmp = $this->ask('Type the wanted user lastname', 'Saïtama'); $validator = Validator::make(['lastname' => $tmp], [ 'lastname' => 'required|min:3|max:255' ]); $validator->validate(); $this->lastname = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid lastname %s', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } //end try }; //end while } /** * Add an email to the user. * * @return void */ private function addEmail(): void { while (is_null($this->email)) { try { $tmp = $this->ask('Type the wanted user email', 'equipe.web@ldc.fr'); $validator = Validator::make(['email' => $tmp], [ 'email' => 'required|unique:users,email|email:rfc,dns' ]); $validator->validate(); $this->email = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid email %s', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } //end try }; //end while } /** * Add a password to the user. * * @return void */ private function addPassword(): void { while ( ($this->password = $this->secret( (!\is_null($this->password) ? 'Failed to confirm, ' : '') . 'Type the wanted user password' )) !== $this->secret('Confirm the password') ) { continue; }; //end while } }