34 lines
882 B
PHP
34 lines
882 B
PHP
<?php
|
|
|
|
namespace App\Actions;
|
|
|
|
use App\Data\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;
|
|
}
|
|
}
|