ogcore/src/Handler/ResetPasswordHandler.php

30 lines
844 B
PHP

<?php
namespace App\Handler;
use App\Dto\Input\UserInput;
use App\Entity\User;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class ResetPasswordHandler
{
public function __construct(
Security $security,
private readonly UserPasswordHasherInterface $userPasswordHasher
)
{
}
public function handle(User $user, string $currentPassword, string $newPassword): User
{
$currentHashedPassword = $this->userPasswordHasher->isPasswordValid($user, $currentPassword);
if ($currentHashedPassword === false) {
throw new \InvalidArgumentException('The current password is invalid.');
}
$user->setPassword($this->userPasswordHasher->hashPassword($user, $newPassword));
return $user;
}
}