trypost/app/Actions/User/CreateUser.php

40 lines
1.1 KiB
PHP
Raw Normal View History

2026-03-31 00:18:07 +00:00
<?php
declare(strict_types=1);
namespace App\Actions\User;
use App\Models\Account;
2026-03-31 00:18:07 +00:00
use App\Models\User;
use Illuminate\Support\Facades\DB;
class CreateUser
{
/**
* @param array{name: string, email: string, password?: string, email_verified_at?: \DateTimeInterface|null, is_invite?: bool} $data
2026-03-31 00:18:07 +00:00
*/
public static function execute(array $data): User
{
return DB::transaction(function () use ($data): User {
$isInviteRegistration = data_get($data, 'is_invite', false);
$account = Account::create([
'name' => data_get($data, 'name')."'s Account",
'billing_email' => data_get($data, 'email'),
]);
2026-03-31 00:18:07 +00:00
$user = User::create([
'name' => data_get($data, 'name'),
'email' => data_get($data, 'email'),
'password' => data_get($data, 'password'),
'email_verified_at' => data_get($data, 'email_verified_at', $isInviteRegistration ? now() : null),
'account_id' => $account->id,
2026-03-31 00:18:07 +00:00
]);
$account->update(['owner_id' => $user->id]);
2026-03-31 00:18:07 +00:00
return $user;
});
}
}