Describe entities as API Platform resources
This commit is contained in:
44
src/State/UserPasswordHasher.php
Normal file
44
src/State/UserPasswordHasher.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
|
||||
/**
|
||||
* @implements ProcessorInterface<User, User>
|
||||
*/
|
||||
final readonly class UserPasswordHasher implements ProcessorInterface
|
||||
{
|
||||
/**
|
||||
* @param ProcessorInterface<User, User> $processor
|
||||
* @param UserPasswordHasherInterface $passwordHasher
|
||||
*/
|
||||
public function __construct(
|
||||
private ProcessorInterface $processor,
|
||||
private UserPasswordHasherInterface $passwordHasher,
|
||||
)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User $data
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): User
|
||||
{
|
||||
if (!$data->getPlainPassword()) {
|
||||
return $this->processor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$hashedPassword = $this->passwordHasher->hashPassword(
|
||||
$data,
|
||||
$data->getPlainPassword()
|
||||
);
|
||||
$data->setPassword($hashedPassword);
|
||||
$data->eraseCredentials();
|
||||
|
||||
return $this->processor->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user