ldc-crm/app/Console/Commands/UserCreate.php

206 lines
5.5 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Console\Commands;
2023-10-06 15:00:42 +02:00
use App\Enums\Users\RoleEnum;
use App\Models\User;
2023-10-06 15:00:42 +02:00
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
2025-07-25 11:23:07 +02:00
use Illuminate\Support\Str;
2023-10-06 15:00:42 +02:00
class UserCreate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2023-10-06 15:00:42 +02:00
protected $signature = 'uwil:user:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a backend admin user';
/**
2023-10-06 15:00:42 +02:00
* The first name of the new user.
*
2025-07-25 11:23:07 +02:00
* @var string|null
*/
2026-10-22 16:37:28 +02:00
protected $firstName;
/**
2023-10-06 15:00:42 +02:00
* The last name of the new user.
*
2025-07-25 11:23:07 +02:00
* @var string|null
*/
2026-10-22 16:37:28 +02:00
protected $lastName;
/**
* The email of the new user.
*
2025-07-25 11:23:07 +02:00
* @var string|null
*/
protected $email;
2023-10-06 15:00:42 +02:00
/**
* The role of the new user.
*
2025-07-25 11:23:07 +02:00
* @var string|null
2023-10-06 15:00:42 +02:00
*/
protected $role;
/**
* Execute the console command.
*
2025-07-25 11:23:07 +02:00
* @return void
*/
2025-07-25 11:23:07 +02:00
public function handle(): void
{
2026-10-22 16:37:28 +02:00
$this->firstName = null;
2023-10-06 15:00:42 +02:00
$this->addFirstName();
2026-10-22 16:37:28 +02:00
$this->lastName = null;
2023-10-06 15:00:42 +02:00
$this->addLastName();
$this->email = null;
$this->addEmail();
2023-10-06 15:00:42 +02:00
$this->role = null;
$this->addRole();
$user = new User([
2026-10-22 16:37:28 +02:00
'first_name' => $this->firstName,
'last_name' => $this->lastName,
2025-07-25 11:23:07 +02:00
'email' => $this->email,
'role' => $this->role,
'published' => true,
2023-10-06 15:00:42 +02:00
'published_at' => Carbon::now(),
]);
$user->saveOrFail();
$this->info('User created 👌');
}
/**
2023-10-06 15:00:42 +02:00
* Add a first name to the user.
*
* @return void
*/
2023-10-06 15:00:42 +02:00
private function addFirstName(): void
{
2026-10-22 16:37:28 +02:00
while (is_null($this->firstName)) {
try {
2023-10-06 15:00:42 +02:00
$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();
2026-10-22 16:37:28 +02:00
$this->firstName = $tmp;
} catch (ValidationException $e) {
$this->error(sprintf(
2023-10-06 15:00:42 +02:00
'Please provide a valid first name %s',
\implode(',', collect($e->errors())->flatten()->all())
));
continue;
2026-10-22 16:37:28 +02:00
}
}
}
/**
2023-10-06 15:00:42 +02:00
* Add a last name to the user.
*
* @return void
*/
2023-10-06 15:00:42 +02:00
private function addLastName(): void
{
2026-10-22 16:37:28 +02:00
while (is_null($this->lastName)) {
try {
2023-10-06 15:00:42 +02:00
$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();
2026-10-22 16:37:28 +02:00
$this->lastName = $tmp;
} catch (ValidationException $e) {
$this->error(sprintf(
2023-10-06 15:00:42 +02:00
'Please provide a valid last_name %s',
\implode(',', collect($e->errors())->flatten()->all())
));
continue;
2026-10-22 16:37:28 +02:00
}
}
}
/**
* 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;
2026-10-22 16:37:28 +02:00
}
}
}
2023-10-06 15:00:42 +02:00
/**
* Add a role to the user.
*
* @return void
*/
private function addRole(): void
{
while (is_null($this->role)) {
try {
2025-07-25 11:23:07 +02:00
$tmp = $this->choice(
2023-10-06 15:00:42 +02:00
'Select his role',
2025-07-25 11:23:07 +02:00
[
Str::ucFirst(RoleEnum::conceptor->label()),
Str::ucFirst(RoleEnum::admin->label()),
Str::ucFirst(RoleEnum::marketing->label())
2025-07-25 11:23:07 +02:00
],
Str::ucFirst(RoleEnum::marketing->label()),
2023-10-06 15:00:42 +02:00
);
2025-07-25 11:23:07 +02:00
if ($tmp === Str::ucFirst(RoleEnum::conceptor->label())) {
2023-10-06 15:00:42 +02:00
$tmp = RoleEnum::conceptor;
}
2025-07-25 11:23:07 +02:00
if ($tmp === Str::ucFirst(RoleEnum::admin->label())) {
2023-10-06 15:00:42 +02:00
$tmp = RoleEnum::admin;
}
if ($tmp === Str::ucFirst(RoleEnum::marketing->label())) {
$tmp = RoleEnum::marketing;
}
2023-10-06 15:00:42 +02:00
$validator = Validator::make(['role' => $tmp], [
'role' => 'required'
]);
$validator->validate();
$this->role = $tmp;
} catch (ValidationException $e) {
$this->error(sprintf(
2025-07-25 11:23:07 +02:00
'Please choose from the selection %s',
2023-10-06 15:00:42 +02:00
\implode(',', collect($e->errors())->flatten()->all())
));
continue;
2026-10-22 16:37:28 +02:00
}
}
2023-10-06 15:00:42 +02:00
}
}