33 lines
1016 B
PHP
33 lines
1016 B
PHP
<?php
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata as API;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\User;
|
|
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
|
|
#[AsDecorator('api_platform.doctrine.orm.state.persist_processor')]
|
|
class UserStateProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private ProcessorInterface $innerProcessor,
|
|
private UserPasswordHasherInterface $userPasswordHasher,
|
|
) {}
|
|
|
|
/**
|
|
* @return T2
|
|
*/
|
|
public function process(mixed $data, API\Operation $operation, array $uriVariables = [], array $context = [])
|
|
{
|
|
if ($data instanceof User && $data->getPlainPassword()) {
|
|
$data->setPassword(
|
|
$this->userPasswordHasher->hashPassword($data, $data->getPlainPassword())
|
|
);
|
|
}
|
|
|
|
return $this->innerProcessor->process($data, $operation, $uriVariables, $context);
|
|
}
|
|
}
|