46 lines
1.2 KiB
PHP
46 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\User\UseCases;
|
|
|
|
use App\Exceptions\BadRequestException;
|
|
use App\User\User;
|
|
use App\User\UserRepository;
|
|
use App\ValueObjects\EmailAddress;
|
|
|
|
class CreateUser
|
|
{
|
|
public function __construct(
|
|
private UserRepository $userRepo,
|
|
) {}
|
|
|
|
/**
|
|
* @throws BadRequestException
|
|
*/
|
|
public function execute(CreateUserRequest $dto): User
|
|
{
|
|
if ($dto->email === null) {
|
|
throw new BadRequestException('email is required');
|
|
}
|
|
|
|
if ($dto->password === null) {
|
|
throw new BadRequestException('password is required');
|
|
}
|
|
|
|
if (strlen($dto->password) < 8) {
|
|
throw new BadRequestException(
|
|
'password must be at least 8 characters'
|
|
);
|
|
}
|
|
|
|
$email = new EmailAddress($dto->email);
|
|
if ($this->userRepo->findByEmail($email) !== null) {
|
|
throw new BadRequestException('email already taken');
|
|
}
|
|
|
|
return $this->userRepo->create(new CreateUserDto(
|
|
email: $email,
|
|
passwordHash: password_hash($dto->password, PASSWORD_DEFAULT),
|
|
isAdmin: $dto->isAdmin,
|
|
));
|
|
}
|
|
}
|