<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
class RegistrationController extends AbstractController
{
private ManagerRegistry $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
#[Route('/register', name: 'register')]
public function register(
Request $request,
UserPasswordHasherInterface $userPasswordHasher,
EntityManagerInterface $entityManager
): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$entityManager->persist($user);
// Itt kéne az account.accountba írni
$this->makeGameAccount($user, $form->get('plainPassword')->getData());
$entityManager->flush();
return $this->redirectToRoute('index');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
private function makeGameAccount(User $user, string $plainPassword): void {
$accountDatabaseConnection = $this->doctrine->getConnection('account');
$insertSql = "
INSERT INTO `account` (`login`, `password`, `social_id`, `email`, `channel_company`, `last_play`)
VALUES (:username, :password, :socialId, :email, '', '0000-00-00 00:00:00.000000');
";
$passwordSql = "SELECT PASSWORD(:plainPassword)";
$passwordStatement = $accountDatabaseConnection->prepare($passwordSql);
$passwordStatement->bindValue('plainPassword', $plainPassword);
$insertStatement = $accountDatabaseConnection->prepare($insertSql);
$insertStatement->bindValue('username', $user->getUsername());
$insertStatement->bindValue('password', $passwordStatement->executeQuery()->fetchAllNumeric()[0][0]);
$insertStatement->bindValue('email', $user->getEmail());
$insertStatement->bindValue('socialId', rand(999999, 9999999));
$insertStatement->executeQuery();
}
}