src/Service/ProUserRegistrationSession.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\ProUser;
  4. use App\Security\ADTokenAuthenticator;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  9. use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
  10. use Symfony\Component\Serializer\Serializer;
  11. class ProUserRegistrationSession
  12. {
  13.     public const SESSION_REGISTRATION_USER_KEY 'pro_registration_user';
  14.     /** @var Serializer */
  15.     private $serializer;
  16.     public function __construct(private readonly SessionInterface $session, private readonly UserManagerInterface $userManager, private readonly LoggerInterface $logger)
  17.     {
  18.         $this->serializer = new Serializer(
  19.             [new GetSetMethodNormalizer()],
  20.             [new JsonEncoder()]
  21.         );
  22.     }
  23.     public function create()
  24.     {
  25.         /* @var ProUser $user */
  26.         $user $this->userManager->createUser(ProUser::class);
  27.         $user->setEnabled(true);
  28.         $user->setEmail($this->session->get(ADTokenAuthenticator::SESSION_REGISTRATION_USER_EMAIL));
  29.         $user->setAdId($this->session->get(ADTokenAuthenticator::SESSION_REGISTRATION_USER_AD_ID));
  30.         $this->logger->info('ProUserRegistrationSession:40 User '.$user->getId().' enabled');
  31.         $user->setNeedsTransaction(false);
  32.         return $user;
  33.     }
  34.     public function exists(): bool
  35.     {
  36.         return $this->session->has(self::SESSION_REGISTRATION_USER_KEY);
  37.     }
  38.     public function load()
  39.     {
  40.         $user $this->session->get(self::SESSION_REGISTRATION_USER_KEY);
  41.         return null === $user $this->create() : $this->serializer->deserialize($userProUser::class, 'json');
  42.     }
  43.     public function save(ProUser $user): void
  44.     {
  45.         $this->session->set(self::SESSION_REGISTRATION_USER_KEY$this->serializer->serialize($user'json'));
  46.     }
  47.     public function remove(): void
  48.     {
  49.         $this->session->remove(self::SESSION_REGISTRATION_USER_KEY);
  50.         $this->session->remove(ADTokenAuthenticator::SESSION_REGISTRATION_USER_EMAIL);
  51.         $this->session->remove(ADTokenAuthenticator::SESSION_REGISTRATION_USER_AD_ID);
  52.     }
  53. }