first_name = null; $this->addFirstName(); $this->last_name = null; $this->addLastName(); $this->email = null; $this->addEmail(); $this->password = null; $this->addPassword(); $this->role = null; $this->addRole(); $user = new User([ 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'email' => $this->email, 'password' => $this->password, 'role' => $this->role, 'published' => true, 'published_at' => Carbon::now(), ]); $user->saveOrFail(); $this->info('User created 👌'); return 0; } /** * Add a first name to the user. * * @return void */ private function addFirstName(): void { while (is_null($this->first_name)) { try { $tmp = $this->ask('Type the wanted user first name', 'Admin'); $validator = Validator::make(['first_name' => $tmp], [ 'first_name' => 'required|min:3|max:255' ]); $validator->validate(); $this->first_name = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid first name %s', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } //end try }; //end while } /** * Add a last name to the user. * * @return void */ private function addLastName(): void { while (is_null($this->last_name)) { try { $tmp = $this->ask('Type the wanted user last name', 'Saïtama'); $validator = Validator::make(['last_name' => $tmp], [ 'last_name' => 'required|min:3|max:255' ]); $validator->validate(); $this->last_name = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please provide a valid last_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', '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 } /** * Add a role to the user. * * @return void */ private function addRole(): void { while (is_null($this->role)) { try { $tmp = $this->choice( 'Select his role', [RoleEnum::conceptor->name, RoleEnum::admin->name, RoleEnum::visitor->name], RoleEnum::visitor->name, $maxAttempts = null, $allowMultipleSelections = false ); if ($tmp === RoleEnum::conceptor->name) { $tmp = RoleEnum::conceptor; } if ($tmp === RoleEnum::admin->name) { $tmp = RoleEnum::admin; } if ($tmp === RoleEnum::visitor->name) { $tmp = RoleEnum::visitor; } $validator = Validator::make(['role' => $tmp], [ 'role' => 'required' ]); $validator->validate(); $this->role = $tmp; } catch (ValidationException $e) { $this->error(sprintf( 'Please choose from the selection', \implode(',', collect($e->errors())->flatten()->all()) )); continue; } //end try }; //end while } }