<?php
namespace App\Controller;
use App\Entity\RequestModification;
use App\Entity\Users;
use App\Repository\RequestModificationStatusRepository;
use App\Service\MailerService;
use App\Repository\RequestModificationRepository;
use App\Repository\TVARepository;
use App\Repository\UsersRepository;
use App\Form\RegistrationFormType;
use App\Security\AppCustomAuthenticator;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class RegistrationController extends AbstractController
{
private $mailerService;
/**
* @var Status|MailerService
*/
public function __construct(
RequestModificationRepository $requestModificationRepository,
MailerService $mailerService,
TVARepository $TVARepository,
RequestModificationStatusRepository $requestModificationStatusRepository,
UsersRepository $usersRepository,
EntityManagerInterface $entityManager
)
{
$this->requestModificationRepository = $requestModificationRepository;
$this->mailerService = $mailerService;
$this->TVARepository = $TVARepository;
$this->requestModificationStatusRepository = $requestModificationStatusRepository;
$this->usersRepository = $usersRepository;
$this->entityManager = $entityManager;
}
/**
* @Route("/register", name="app_register")
*/
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserAuthenticatorInterface $userAuthenticator, AppCustomAuthenticator $authenticator): Response
{
$user = new Users();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$foundUser = $this->usersRepository->findOneBy(['deleted' => false, 'email' => $user->getEmail()]);
if($foundUser){
$this->addFlash('error', 'Cet e-mail est déjà utilisé. Veuillez en choisir un autre.');
// Rediriger ou rendre la vue du formulaire avec l'erreur
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
]);
}
$tvaStatus = $form->get('tvaStatus')->getData();
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
$randomHex = bin2hex(random_bytes(16));
$timestamp = time();
$token = $randomHex . $timestamp;
$shuffledToken = str_shuffle($token);
$token = substr($shuffledToken, 0, 32);
$user->setToken($token);
$user->setValidated(true);
$this->entityManager->persist($user);
$this->entityManager->flush();
$user->setEmailValidationToken($this->sendEmailVerification($user));
$this->sendRequest($request->get('isExistingEstablishment'),
$request->get('establishments'),
$user,
$request->get('establishmentsAddress'),
$request->get('description'),
$tvaStatus);
return $this->render('registration/register-success.html.twig');
}
else if($form->isSubmitted() && !$form->isValid()){
$this->addFlash('error', 'Veuillez vérifier les informations saisies.');
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
'registrationFormSubmitted' => 1,
'existing' => $request->get('isExistingEstablishment')
]);
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form->createView(),
'registrationFormSubmitted' => 0,
'existing' => 0,
]);
}
public function sendEmailVerification(Users $user){
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$token = '';
for ($i = 0; $i < 50; $i++) {
$token .= $characters[rand(0, $charactersLength - 1)];
}
$params = [
'link' => $_ENV['DOMAIN_EMAIL'].'email-verification/'.$token
];
$this->mailerService->load(
$params,
'd-27c384389d1a47169fc54a18f87199b3',
$user
);
return $token;
}
public function sendRequest(string $new, string $establishment, Users $user, ?string $address, ?string $description, string $tva)
{
$form = new RequestModification();
$form->setCreatedBy($user);
$form->setType(2);
$form->setResponsable($user->getMyResponsable());
$form->setTvaNumber($user->getTvaNumber());
$form->setCompanyName($establishment);
$form->setTvaSubject($this->TVARepository->find($tva));
$form->setRequestModificationStatus($this->requestModificationStatusRepository->find(2));
if ($new === "false") {
$form->setTitle('Demande de création');
$form->setText(
'Pouvez-vous créer mon établissement : ' . $establishment .
'<br>Adresse : ' . $address .
',<br>Numéro de TVA : ' . $user->getTvaNumber() .
'<br>Description : ' . $description
);
} else {
$form->setTitle('Demande de liaison');
$form->setText('Pouvez-vous me lier à mon établissement : ' . $establishment);
}
// Si tu veux persister ce formulaire, ajoute ici le persist/flush avec l'EntityManager
$this->entityManager->persist($form);
$this->entityManager->flush();
}
}