Describe entities as API Platform resources

This commit is contained in:
2024-06-05 17:37:53 +02:00
parent 48bc5fd8c3
commit 22be991b8c
16 changed files with 621 additions and 1 deletions

View 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);
}
}