trypost/app/Console/Commands/CreateUserCommand.php

44 lines
1.3 KiB
PHP
Raw Normal View History

2026-09-08 16:30:11 +00:00
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Actions\User\CreateUser;
use App\Models\User;
use Illuminate\Console\Command;
class CreateUserCommand extends Command
{
protected $signature = 'user:create {email? : The email address of the user} {--name= : The name of the user} {--password= : The password for the user}';
protected $description = 'Create a new user with account and default workspace';
public function handle(): int
{
$email = $this->argument('email') ?? $this->ask('User email');
if (User::where('email', $email)->exists()) {
$this->error("A user with email {$email} already exists.");
return self::FAILURE;
}
$name = $this->option('name') ?? $this->ask('User name', 'Admin');
$password = $this->option('password') ?? $this->secret('User password');
if (! $password) {
$this->error('Password cannot be empty.');
return self::FAILURE;
}
$user = CreateUser::execute([
'name' => $name,
'email' => $email,
'password' => $password,
]);
$this->info("User [{$user->email}] created successfully with personal account and workspace!");
return self::SUCCESS;
}
}