<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class UserRegisterSubscriber implements EventSubscriberInterface
{
private $userPasswordHasher;
public function __construct(
UserPasswordHasherInterface $userPasswordHasher
)
{
$this->userPasswordHasher = $userPasswordHasher;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['userRegistered', EventPriorities::PRE_WRITE],
];
}
public function userRegistered(ViewEvent $event)
{
$user = $event->getControllerResult();
$method = $event->getRequest()
->getMethod();
if (!$user instanceof User ||
!in_array($method, [Request::METHOD_POST])) {
return;
}
$user->setPassword(
$this->userPasswordHasher->hashPassword(
$user,
$user->getPassword()
)
);
}
}