src/Controller/RegistrationController.php line 30

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Setting;
  4. use App\Entity\User;
  5. use App\Entity\UserProject;
  6. use App\Form\RegistrationFormType;
  7. use App\Repository\UserRepository;
  8. use App\Security\EmailVerifier;
  9. use App\Service\BillingService;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Mime\Address;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Contracts\Translation\TranslatorInterface;
  21. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  22. class RegistrationController extends AbstractController
  23. {
  24.     public function __construct(private readonly ManagerRegistry $doctrine, private EmailVerifier $emailVerifier) {}
  25.     #[Route('/register'name'app_register')]
  26.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherEntityManagerInterface $entityManagerBillingService $billingService): Response
  27.     {
  28.         if ($this->getUser()) {
  29.             return $this->redirectToRoute('app_user_platforms');
  30.         }
  31.         
  32.         $user = new User();
  33.         $form $this->createForm(RegistrationFormType::class, $user);
  34.         $form->handleRequest($request);
  35.         if ($form->isSubmitted() && $form->isValid()) {
  36.             // encode the plain password
  37.             $user->setPassword(
  38.                 $userPasswordHasher->hashPassword(
  39.                     $user,
  40.                     $form->get('plainPassword')->getData()
  41.                 )
  42.             );
  43.             $user->setRoles(['ROLE_USER']);
  44.             $user->setBalance(0);
  45.             $entityManager->persist($user);
  46.             $userProject = new UserProject();
  47.             $userProject->setName($this->doctrine->getRepository(Setting::class)->get('default_project_name') ?? 'Личныйй');
  48.             $userProject->setUser($user);
  49.             $userProject->setIsMain(true);
  50.             $entityManager->persist($userProject);
  51.             $entityManager->flush();
  52.             // Add a bonus
  53.             try {
  54.                 $billingService->updateUserBalance($user'bonus_registration'$this->doctrine->getRepository(Setting::class)->get('default_balance') ?? 0$userProject);
  55.             } catch (Exception $exception) {
  56.                 //todo log
  57.                 //$exception->getMessage();
  58.             }
  59.             // generate a signed url and email it to the user
  60.             $this->emailVerifier->sendEmailConfirmation('app_verify_email'$user,
  61.                 (new TemplatedEmail())
  62.                     ->from(new Address('my@eresh.space''noreply@ze.media'))
  63.                     ->to($user->getEmail())
  64.                     ->subject('Подтвердите свой E-mail адрес')
  65.                     ->htmlTemplate('registration/confirmation_email.html.twig')
  66.             );
  67.             // do anything else you need here, like send an email
  68.             return $this->redirectToRoute('app_login');
  69.         }
  70.         return $this->render('register/index.html.twig', [
  71.             'registrationForm' => $form->createView(),
  72.         ]);
  73.     }
  74.     #[Route('/verify/email'name'app_verify_email')]
  75.     public function verifyUserEmail(Request $requestTranslatorInterface $translatorUserRepository $userRepository): Response
  76.     {
  77.         $id $request->get('id');
  78.         if (null === $id) {
  79.             return $this->redirectToRoute('app_register');
  80.         }
  81.         $user $userRepository->find($id);
  82.         if (null === $user) {
  83.             return $this->redirectToRoute('app_register');
  84.         }
  85.         // validate email confirmation link, sets User::isVerified=true and persists
  86.         try {
  87.             $this->emailVerifier->handleEmailConfirmation($request$user);
  88.         } catch (VerifyEmailExceptionInterface $exception) {
  89.             $this->addFlash('verify_email_error'$translator->trans($exception->getReason(), [], 'VerifyEmailBundle'));
  90.             return $this->redirectToRoute('app_register');
  91.         }
  92.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  93.         $this->addFlash('success''Your email address has been verified.');
  94.         return $this->redirectToRoute('app_login');
  95.     }
  96.     
  97.     private function getErrorsFromForm(FormInterface $form)
  98.     {
  99.         $errors = array();
  100.         foreach ($form->getErrors() as $error) {
  101.             $errors[] = $error->getMessage();
  102.         }
  103.         foreach ($form->all() as $childForm) {
  104.             if ($childForm instanceof FormInterface) {
  105.                 if ($childErrors $this->getErrorsFromForm($childForm)) {
  106.                     $errors[$childForm->getName()] = $childErrors;
  107.                 }
  108.             }
  109.         }
  110.         return $errors;
  111.     }
  112. }