src/EventSubscriber/ApiPlatformEventSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. // api/src/EventSubscriber/BookMailSubscriber.php
  3. namespace App\EventSubscriber;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Entity\Like;
  6. use App\Entity\Profile;
  7. use App\Entity\User;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  16. final class ApiPlatformEventSubscriber implements EventSubscriberInterface
  17. {
  18.     
  19.     
  20.     public function __construct(UserPasswordHasherInterface $userPasswordHasher,EntityManagerInterface $entityManagerInterface)
  21.     {
  22.         $this->passwordHasher $userPasswordHasher;
  23.         $this->em $entityManagerInterface;
  24.     }
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::VIEW => ['setUserProfile'EventPriorities::POST_WRITE],
  29.         ];
  30.     }
  31.     public function setUserProfile(ViewEvent $event): void
  32.     {
  33.         
  34.         
  35.         $userProfile $event->getControllerResult();
  36.         $method $event->getRequest()->getMethod();
  37.         
  38.         
  39.         if (!$userProfile instanceof Profile || Request::METHOD_POST !== $method) {
  40.             return;
  41.         }
  42.         $user $userProfile->getUser();
  43.         $user->setPassword(
  44.             $this->passwordHasher->hashPassword(
  45.                 $user,
  46.                 $user->getPassword()
  47.             )
  48.         );
  49.         $this->em->persist($user);
  50.         $this->em->flush();
  51.         
  52.         
  53.         
  54.     }
  55. }