<?php 
 
namespace App\EventSubscriber; 
 
use ApiPlatform\Symfony\EventListener\EventPriorities; 
use App\Entity\User; 
use App\Repository\UserRepository; 
use App\User\UserActivation; 
use Doctrine\ORM\EntityManagerInterface; 
use Psr\Log\LoggerInterface; 
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; 
use Symfony\Component\Security\Core\Security; 
use Symfony\Component\String\ByteString; 
 
final class UserSubscriber implements EventSubscriberInterface 
{ 
    public function __construct( 
        private UserActivation $userActivation, 
        private Security $security, 
        private LoggerInterface $logger, 
        private EntityManagerInterface $entityManager, 
        private UserPasswordHasherInterface $passwordHasher) 
    { 
    } 
 
    public static function getSubscribedEvents() 
    { 
        return [ 
            KernelEvents::VIEW => [ 
                ['postWriteUser', EventPriorities::POST_WRITE], 
                ['preWriteUser', EventPriorities::PRE_WRITE] 
            ], 
        ]; 
    } 
 
    public function preWriteUser(ViewEvent $event): void 
    { 
        $user = $event->getControllerResult(); 
        $method = $event->getRequest()->getMethod(); 
        if (!($user instanceof User)) { 
            return; 
        } 
 
        $password = null; 
        if (Request::METHOD_PUT === $method) { 
            $unitOfWork = $this->entityManager->getUnitOfWork(); 
            $originalEntityData = $unitOfWork->getOriginalEntityData($user); 
            // If the user went from disabled to enabled 
            if (!empty($originalEntityData) && 
                isset($originalEntityData['isEnabled']) && 
                $originalEntityData['isEnabled'] === false && 
                $user->isIsEnabled() === true) { 
 
                // set unlocked to null and reset attempts 
                $user->setLockedAt(null); 
                $user->setAttempts(0); 
            } 
        } 
        if (Request::METHOD_POST == $method){ 
            $password = ByteString::fromRandom(10)->toString(); 
            $user->setPlainPassword($password); 
            // $password = "testtest"; 
            $user->setIsEnabled(true); 
//            $user->addRole(User::ROLE_USER); 
        } 
        else if ($user->getPlainPassword()) { 
            $password = $user->getPlainPassword(); 
        } 
 
        if ($password) { 
            $hashedPassword = $this->passwordHasher->hashPassword( 
                $user, 
                $password 
            ); 
            $user->setPassword($hashedPassword); 
        } 
    } 
 
    public function postWriteUser(ViewEvent $event): void 
    { 
        $user = $event->getControllerResult(); 
        $method = $event->getRequest()->getMethod(); 
        if (!($user instanceof User)|| (Request::METHOD_POST !== $method)) { 
            return; 
        } 
        $this->userActivation->sendActivationEmail($user); 
    } 
}