name = null; $this->addName(); $this->email = null; $this->addEmail(); $this->password = null; $this->addPassword(); $this->role = null; $this->addRole(); $user = new User([ 'name' => $this->name, 'email' => $this->email, 'password' => $this->password, 'role' => $this->role ]); $user->saveOrFail(); $this->info('User created.'); return 0; } /** * Add a firstname to the user. * * @return void */ private function addName(): void { while (is_null($this->name)) { try { $tmp = $this->ask('Type the wanted user name', 'Admin'); $validator = Validator::make(['name' => $tmp], [ 'name' => 'required|min:3|max:255' ]); $validator->validate(); $this->name = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid name %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', 'digital@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 } /** * Add a password to the user. * * @return void */ private function addRole(): void { while (is_null($this->role)) { $tmp = $this->confirm('Is the user an admin?'); $validator = Validator::make(['role' => $tmp], [ 'role' => 'required|boolean' ]); $validator->validate(); $this->role = $tmp ? RoleEnum::admin : RoleEnum::client; }; } }