src/Controller/RegistrationController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. class RegistrationController extends AbstractController
  13. {
  14.     private ManagerRegistry $doctrine;
  15.     public function __construct(ManagerRegistry $doctrine)
  16.     {
  17.         $this->doctrine $doctrine;
  18.     }
  19.     #[Route('/register'name'register')]
  20.     public function register(
  21.         Request $request
  22.         UserPasswordHasherInterface $userPasswordHasher
  23.         EntityManagerInterface $entityManager
  24.         ): Response
  25.     {
  26.         $user = new User();
  27.         $form $this->createForm(RegistrationFormType::class, $user);
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             // encode the plain password
  31.             $user->setPassword(
  32.                 $userPasswordHasher->hashPassword(
  33.                     $user,
  34.                     $form->get('plainPassword')->getData()
  35.                 )
  36.             );
  37.             $entityManager->persist($user);
  38.             // Itt kéne az account.accountba írni
  39.            
  40.             $this->makeGameAccount($user$form->get('plainPassword')->getData());
  41.             $entityManager->flush();
  42.             return $this->redirectToRoute('index');
  43.         }
  44.         return $this->render('registration/register.html.twig', [
  45.             'registrationForm' => $form->createView(),
  46.         ]);
  47.     }
  48.     private function makeGameAccount(User $userstring $plainPassword): void {
  49.         $accountDatabaseConnection $this->doctrine->getConnection('account');
  50.         $insertSql "
  51.             INSERT INTO `account` (`login`, `password`, `social_id`, `email`, `channel_company`, `last_play`) 
  52.             VALUES (:username, :password, :socialId, :email, '', '0000-00-00 00:00:00.000000');
  53.         ";
  54.         $passwordSql "SELECT PASSWORD(:plainPassword)";
  55.         
  56.         $passwordStatement $accountDatabaseConnection->prepare($passwordSql);
  57.         $passwordStatement->bindValue('plainPassword'$plainPassword);
  58.         $insertStatement $accountDatabaseConnection->prepare($insertSql);
  59.         $insertStatement->bindValue('username'$user->getUsername());
  60.         $insertStatement->bindValue('password'$passwordStatement->executeQuery()->fetchAllNumeric()[0][0]);
  61.         $insertStatement->bindValue('email'$user->getEmail());
  62.         $insertStatement->bindValue('socialId'rand(9999999999999));
  63.         $insertStatement->executeQuery();
  64.     }
  65. }