From b545369ed3ec0fa08a878afedd55da6a4e699bc3 Mon Sep 17 00:00:00 2001 From: yena Date: Thu, 25 Apr 2024 17:26:24 +0200 Subject: [PATCH] Registration handler: Return user object --- src/Controller/RegistrationController.php | 8 +++----- src/Handler/RegistrationHandler.php | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Controller/RegistrationController.php b/src/Controller/RegistrationController.php index 1cbba3fe..42e1cc14 100644 --- a/src/Controller/RegistrationController.php +++ b/src/Controller/RegistrationController.php @@ -85,12 +85,10 @@ public function register(Request $request, string $voucher = ''): Response $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $this->registrationHandler->handle($registration); + $user = $this->registrationHandler->handle($registration); - if (null !== $user = $this->manager->getRepository(User::class)->findByEmail($registration->getEmail())) { - $token = new UsernamePasswordToken($user, 'default', $user->getRoles()); - $this->tokenStorage->setToken($token); - } + $token = new UsernamePasswordToken($user, 'default', $user->getRoles()); + $this->tokenStorage->setToken($token); $recoveryToken = $user->getPlainRecoveryToken(); diff --git a/src/Handler/RegistrationHandler.php b/src/Handler/RegistrationHandler.php index 349f2f61..261a1416 100644 --- a/src/Handler/RegistrationHandler.php +++ b/src/Handler/RegistrationHandler.php @@ -20,14 +20,22 @@ class RegistrationHandler /** * Constructor. */ - public function __construct(private readonly EntityManagerInterface $manager, private readonly DomainGuesser $domainGuesser, private readonly EventDispatcherInterface $eventDispatcher, private readonly PasswordUpdater $passwordUpdater, private readonly MailCryptKeyHandler $mailCryptKeyHandler, private readonly RecoveryTokenHandler $recoveryTokenHandler, private readonly bool $registrationOpen, private readonly bool $mailCrypt) - { + public function __construct( + private readonly EntityManagerInterface $manager, + private readonly DomainGuesser $domainGuesser, + private readonly EventDispatcherInterface $eventDispatcher, + private readonly PasswordUpdater $passwordUpdater, + private readonly MailCryptKeyHandler $mailCryptKeyHandler, + private readonly RecoveryTokenHandler $recoveryTokenHandler, + private readonly bool $registrationOpen, + private readonly bool $mailCrypt + ) { } /** * @throws Exception */ - public function handle(Registration $registration): void + public function handle(Registration $registration): User { if (!$this->isRegistrationOpen()) { throw new Exception('The Registration is closed!'); @@ -53,6 +61,8 @@ public function handle(Registration $registration): void $this->manager->flush(); $this->eventDispatcher->dispatch(new UserEvent($user), Events::MAIL_ACCOUNT_CREATED); + + return $user; } public function isRegistrationOpen(): bool