Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | n/a |
0 / 0 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
|||
| CreateAdmin | n/a |
0 / 0 |
n/a |
0 / 0 |
13 | n/a |
0 / 0 |
|||
| handle | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| addFirstname | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| addLastname | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| addEmail | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| addPassword | n/a |
0 / 0 |
n/a |
0 / 0 |
3 | |||||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Models\User; |
| 6 | use Illuminate\Console\Command; |
| 7 | use Illuminate\Support\Facades\Validator; |
| 8 | use Illuminate\Validation\ValidationException; |
| 9 | |
| 10 | /** |
| 11 | * Creates a new user |
| 12 | * |
| 13 | * @codeCoverageIgnore |
| 14 | */ |
| 15 | class CreateAdmin extends Command |
| 16 | { |
| 17 | /** |
| 18 | * The name and signature of the console command. |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $signature = 'user:create'; |
| 23 | |
| 24 | /** |
| 25 | * The console command description. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $description = 'Create a backend admin user'; |
| 30 | |
| 31 | /** |
| 32 | * The firstname of the new user. |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected $firstname; |
| 37 | |
| 38 | /** |
| 39 | * The lastname of the new user. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $lastname; |
| 44 | |
| 45 | /** |
| 46 | * The email of the new user. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | protected $email; |
| 51 | |
| 52 | /** |
| 53 | * The password of the new user. |
| 54 | * |
| 55 | * @var string |
| 56 | */ |
| 57 | protected $password; |
| 58 | |
| 59 | /** |
| 60 | * Execute the console command. |
| 61 | * |
| 62 | * @return integer |
| 63 | */ |
| 64 | public function handle(): int |
| 65 | { |
| 66 | $this->firstname = null; |
| 67 | $this->addFirstname(); |
| 68 | |
| 69 | $this->lastname = null; |
| 70 | $this->addLastname(); |
| 71 | |
| 72 | $this->email = null; |
| 73 | $this->addEmail(); |
| 74 | |
| 75 | $this->password = null; |
| 76 | $this->addPassword(); |
| 77 | |
| 78 | $user = new User([ |
| 79 | 'firstname' => $this->firstname, |
| 80 | 'lastname' => $this->lastname, |
| 81 | 'email' => $this->email, |
| 82 | 'password' => $this->password, |
| 83 | 'remember_token' => null, |
| 84 | ]); |
| 85 | $user->saveOrFail(); |
| 86 | |
| 87 | $this->info('User created 👌'); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Add a firstname to the user. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | private function addFirstname(): void |
| 98 | { |
| 99 | while (is_null($this->firstname)) { |
| 100 | try { |
| 101 | $tmp = $this->ask('Type the wanted user firstname', 'Admin'); |
| 102 | $validator = Validator::make(['firstname' => $tmp], [ |
| 103 | 'firstname' => 'required|min:3|max:255' |
| 104 | ]); |
| 105 | $validator->validate(); |
| 106 | $this->firstname = $tmp; |
| 107 | } catch (ValidationException $e) { |
| 108 | $this->error(sprintf( |
| 109 | 'Please provide a valid firstname %s', |
| 110 | \implode(',', collect($e->errors())->flatten()->all()) |
| 111 | )); |
| 112 | continue; |
| 113 | } //end try |
| 114 | }; //end while |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Add a lastname to the user. |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | private function addLastname(): void |
| 123 | { |
| 124 | while (is_null($this->lastname)) { |
| 125 | try { |
| 126 | $tmp = $this->ask('Type the wanted user lastname', 'Saïtama'); |
| 127 | $validator = Validator::make(['lastname' => $tmp], [ |
| 128 | 'lastname' => 'required|min:3|max:255' |
| 129 | ]); |
| 130 | $validator->validate(); |
| 131 | $this->lastname = $tmp; |
| 132 | } catch (ValidationException $e) { |
| 133 | $this->error(sprintf( |
| 134 | 'Please provide a valid lastname %s', |
| 135 | \implode(',', collect($e->errors())->flatten()->all()) |
| 136 | )); |
| 137 | continue; |
| 138 | } //end try |
| 139 | }; //end while |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Add an email to the user. |
| 144 | * |
| 145 | * @return void |
| 146 | */ |
| 147 | private function addEmail(): void |
| 148 | { |
| 149 | while (is_null($this->email)) { |
| 150 | try { |
| 151 | $tmp = $this->ask('Type the wanted user email', 'equipe.web@ldc.fr'); |
| 152 | $validator = Validator::make(['email' => $tmp], [ |
| 153 | 'email' => 'required|unique:users,email|email:rfc,dns' |
| 154 | ]); |
| 155 | $validator->validate(); |
| 156 | $this->email = $tmp; |
| 157 | } catch (ValidationException $e) { |
| 158 | $this->error(sprintf( |
| 159 | 'Please provide a valid email %s', |
| 160 | \implode(',', collect($e->errors())->flatten()->all()) |
| 161 | )); |
| 162 | continue; |
| 163 | } //end try |
| 164 | }; //end while |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Add a password to the user. |
| 169 | * |
| 170 | * @return void |
| 171 | */ |
| 172 | private function addPassword(): void |
| 173 | { |
| 174 | while ( |
| 175 | ($this->password = $this->secret( |
| 176 | (!\is_null($this->password) ? 'Failed to confirm, ' : '') . 'Type the wanted user password' |
| 177 | )) !== $this->secret('Confirm the password') |
| 178 | ) { |
| 179 | continue; |
| 180 | }; //end while |
| 181 | } |
| 182 | } |