src/Controller/Front/RegisterController.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\{ClientUser};
  4. use App\Form\ClientType;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use App\Service\AccessClientService;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. class RegisterController extends AbstractController
  15. {
  16.     private $tokenStorage;
  17.     public function __construct(
  18.         TokenStorageInterface $tokenStorage
  19.     )
  20.     {
  21.         $this->tokenStorage $tokenStorage;
  22.     }
  23.     /**
  24.      * @Route("/client/register", name="app_front_register")
  25.      */
  26.     public function register(AccessClientService $accessClientServiceRequest $requestEntityManagerInterface $entityManagerUserPasswordEncoderInterface $passwordEncoder): Response
  27.     {
  28.         $accessClientService->handleAccessControl();
  29.         $slug $request->getSession()->get('register_info_slug'); 
  30.         
  31.         $user = new User();
  32.         $client = new Client();
  33.         
  34.         $form $this->createForm(ClientType::class, $client);
  35.         $form->handleRequest($request);
  36.         if ($form->isSubmitted() && $form->isValid()) {
  37.             $civility $form->get('user')->get('civility')->getData();
  38.             $lastname $form->get('user')->get('lastname')->getData();
  39.             $firstname $form->get('user')->get('firstname')->getData();
  40.             $email $form->get('user')->get('email')->getData();
  41.             $mobilePhone $form->get('mobilePhone')->getData();
  42.             $plainPassword $form->get('user')->get('password')->getData();
  43.             $agreeTerms $form->get('agreeTerms')->getData();
  44.             $user->setCivility($civility);
  45.             if ($civility === 'Monsieur' || $civility === 'Madame') {
  46.                 $client->setEntrepriseName(null);  
  47.                 $client->setSiret(null);        
  48.             } 
  49.             $user->setLastname($lastname);
  50.             $user->setFirstname($firstname);
  51.             $user->setEmail($email);
  52.             $user->setRoles(['ROLE_CLIENT']); 
  53.             $encodedPassword $passwordEncoder->encodePassword($user$plainPassword);
  54.             $user->setPassword($encodedPassword);
  55.             $user->setStatus(User::ACTIVE);
  56.             $user->setCreatedAtValue();
  57.             $user->preUpdate();
  58.             $client->setUser($user);
  59.             $client->setMobilePhone($mobilePhone);
  60.             $client->setAgreeTerms($agreeTerms);
  61.             $entityManager->persist($user);
  62.             $entityManager->persist($client);
  63.             $entityManager->flush();
  64.             if ($slug) {
  65.                 $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  66.                 $this->tokenStorage->setToken($token);
  67.                 return $this->redirectToRoute('app_client_service_login_info', ['slug' => $slug]);
  68.             } else {
  69.                 $this->addFlash('success''Succès ! Votre compte client a été bien créé.');
  70.                 return $this->redirectToRoute('app_front_login');
  71.             }
  72.         }
  73.         return $this->render('front/register/client.html.twig', [
  74.             'form' => $form->createView(),            
  75.         ]);
  76.     }
  77. }