ekart/backend/app/Actions/User/CreateUserAction.php
2026-03-25 17:17:35 +05:30

34 lines
892 B
PHP

<?php
namespace App\Actions\User;
use App\Data\User\RegisterDTO;
use App\Models\User;
final readonly class CreateUserAction
{
/**
* Rather than using facades, we use DI ( Dependency Injection ).
* That's why User model is injected and new user is created by
* set the properties rather that model's static `create()` method.
*
* This will create problems if using Octane ot FrankenPHP.
*/
public function __construct(private User $user) {}
public function execute(RegisterDTO $dto): User
{
$this->user->email = $dto->email;
$this->user->name = $dto->name;
$this->user->city = $dto->city;
$this->user->mobile_number = $dto->mobileNumber;
// We already have casts to auto hash passwords.
$this->user->password = $dto->password;
$this->user->save();
return $this->user;
}
}