41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProcessorInterface;
|
|
use App\Entity\User;
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
|
|
final class UserPasswordProcessor implements ProcessorInterface
|
|
{
|
|
public function __construct(
|
|
private readonly UserPasswordHasherInterface $hasher,
|
|
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
|
private readonly ProcessorInterface $persistProcessor
|
|
) {}
|
|
|
|
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
|
{
|
|
if ($data instanceof User) {
|
|
$plain = $data->getPassword();
|
|
if ('' !== $plain) {
|
|
$data->setPassword($this->hasher->hashPassword(
|
|
$data,
|
|
$plain
|
|
));
|
|
}
|
|
}
|
|
|
|
return $this->persistProcessor->process(
|
|
$data,
|
|
$operation,
|
|
$uriVariables,
|
|
$context
|
|
);
|
|
}
|
|
}
|