Skip to content

Commit

Permalink
Registration handler: Return user object
Browse files Browse the repository at this point in the history
  • Loading branch information
y3n4 committed Apr 25, 2024
1 parent d908dc5 commit b545369
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
16 changes: 13 additions & 3 deletions src/Handler/RegistrationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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
Expand Down

0 comments on commit b545369

Please sign in to comment.