$this->createForm(RegistrationFormType::class), self::REGISTER_STEP_TWO => $this->renderRegisterStepTwo(), default => $this->redirectToRoute('app_register_step', ['step' => self::REGISTER_STEP_ONE]), }; $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { return match(true) { $step === self::REGISTER_STEP_ONE => $this->handleRegisterStepOne($form, $request), $step === self::REGISTER_STEP_TWO => $this->handleRegisterStepTwo($form, $user), default => $this->redirectToRoute('app_register_step', ['step' => self::REGISTER_STEP_ONE]), }; } return $this->render(sprintf('registration/register-step-%s.html.twig', $step), [ 'form' => $form, 'data' => $form->getData(), 'admin' => $user ]); } private function handleRegisterStepOne(FormInterface $form, Request $request): Response { $user = new User(); $form = $this->createForm(RegistrationFormType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { // @var string $plainPassword $plainPassword = $form->get('plainPassword')->getData(); // encode the plain password $user->setPassword( $this->userPasswordHasher->hashPassword( $user, $plainPassword ) ); $user->setJob(JobType::ADMIN); $user->setRateType(RateType::FIXED); $user->setRate('0.00'); $user->setRoles(['ROLE_ADMIN']); $user->setLevel(CaseLevel::ADMIN); // save user $this->entityManager->persist($user); $this->entityManager->flush(); return $this->redirectToRoute('app_dashboard'); } return $this->redirectToRoute('app_register_step', ['step' => self::REGISTER_STEP_ONE]); } private function renderRegisterStepTwo(): FormInterface { $company = $this->requestStack->getSession()->get('register-form-step-two'); if (!$company instanceof CompanyDetailsDto) { $company = new CompanyDetailsDto(); } return $this->createForm(CompanyFormType::class, $company); } private function handleRegisterStepTwo(FormInterface $form, User $owner): Response { $company = $this->companyFactory->create($form->getData(), $owner); $owner->setCompany($company); $this->entityManager->persist($owner); $this->entityManager->persist($company); $this->entityManager->flush(); return $this->redirectToRoute('app_dashboard'); } #[Route('/new-user', name: 'app_new_user')] public function newUser(Request $request): Response { return $this->render('registration/new-user.html.twig'); } #[Route('/add-user', name: 'app_add_user')] public function addUser(Request $request, UserPasswordHasherInterface $userPasswordHasher, EntityManagerInterface $entityManager): Response { $user = new User(); $user->setUsername('new-user'); $user->setEmail('g6eK1@example.com'); $user->setName('New User'); $user->setPassword( $this->userPasswordHasher->hashPassword( $user, 'password' ) ); $user->setJob(JobType::ADMIN); $user->setRateType(RateType::FIXED); $user->setRate('0.00'); $entityManager->persist($user); $entityManager->flush(); return $this->redirectToRoute('dashboard'); } #[Route('/register', name: 'app_register')] public function register(): Response { return $this->redirectToRoute('app_register_step', ['step' => self::REGISTER_STEP_ONE]); } }