<?php
// api/src/EventSubscriber/BookMailSubscriber.php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Like;
use App\Entity\Profile;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
final class ApiPlatformEventSubscriber implements EventSubscriberInterface
{
public function __construct(UserPasswordHasherInterface $userPasswordHasher,EntityManagerInterface $entityManagerInterface)
{
$this->passwordHasher = $userPasswordHasher;
$this->em = $entityManagerInterface;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['setUserProfile', EventPriorities::POST_WRITE],
];
}
public function setUserProfile(ViewEvent $event): void
{
$userProfile = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$userProfile instanceof Profile || Request::METHOD_POST !== $method) {
return;
}
$user = $userProfile->getUser();
$user->setPassword(
$this->passwordHasher->hashPassword(
$user,
$user->getPassword()
)
);
$this->em->persist($user);
$this->em->flush();
}
}