src/Controller/RegistrationController.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\RegistrationFormType;
  5. use App\Repository\UserRepository;
  6. use App\Security\LoginFormAuthenticator;
  7. use App\Service\MangoPayService;
  8. use DateTime;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  15. use App\Service\CalculNotation;
  16. use App\Service\BankInfoManage;
  17. use App\Service\ProfilePictureManage;
  18. use App\Service\LockManage;
  19. use App\Service\StatisticManagement;
  20. class RegistrationController extends AbstractController
  21. {
  22.     private UserPasswordHasherInterface $passwordEncoder;
  23.     private UserAuthenticatorInterface $guardHandler;
  24.     private LoginFormAuthenticator $authenticator;
  25.     private MangoPayService $mangopay;
  26.     private CalculNotation $notation;
  27.     private BankInfoManage $bankInfo;
  28.     private ProfilePictureManage $profilePicture;
  29.     private LockManage $lockManage;
  30.     private StatisticManagement $statistic;
  31.     private UserRepository $userRep;
  32.     public function __construct(
  33.         UserPasswordHasherInterface $passwordEncoder,
  34.         UserAuthenticatorInterface $guardHandler,
  35.         LoginFormAuthenticator $authenticator,
  36.         MangoPayService $mangopay,
  37.         CalculNotation $notation,
  38.         BankInfoManage $bankInfo,
  39.         ProfilePictureManage $profilePicture,
  40.         LockManage $lockManage,
  41.         StatisticManagement $statistic,
  42.         UserRepository $userRep
  43.     ) {
  44.         $this->passwordEncoder $passwordEncoder;
  45.         $this->guardHandler $guardHandler;
  46.         $this->authenticator $authenticator;
  47.         $this->mangopay $mangopay;
  48.         $this->notation $notation;
  49.         $this->bankInfo $bankInfo;
  50.         $this->profilePicture $profilePicture;
  51.         $this->lockManage $lockManage;
  52.         $this->statistic $statistic;
  53.         $this->userRep $userRep;
  54.     }
  55.     #[Route('/register'name'app_register')]
  56.     public function register(Request $request): Response
  57.     {
  58.         $user = new User();
  59.         $form $this->createForm(RegistrationFormType::class, $user);
  60.         $form->handleRequest($request);
  61.         if ($form->isSubmitted() && $form->isValid()) {
  62.             // encode the plain password
  63.             $checkMail $this->userRep->findBy(['email' => $form['email']->getData()]);
  64.             if (!$checkMail || empty($checkMail)) {
  65.                 $checkFirstUser $this->userRep->findAll();
  66.                 if (!$checkFirstUser) {
  67.                     $roles = ['ROLE_SUPER_ADMIN'];
  68.                 } else {
  69.                     $roles = ['ROLE_CLIENT'];
  70.                 }
  71.                 $user->setRoles($roles);
  72.                 $user->setUsername($form['email']->getData());
  73.                 $user->setPassword(
  74.                     $this->passwordEncoder->hashPassword(
  75.                         $user,
  76.                         $form->get('plainPassword')->getData()
  77.                     )
  78.                 );
  79.                 $registrationDate = new DateTime(date('d-m-Y H:i:s'));
  80.                 $user->setRegistrationDate($registrationDate);
  81.                 $this->userRep->add($usertrue);
  82.                 //initialisation compte mangopay
  83.                 $this->bankInfo->createUserBank($user);
  84.                 $this->statistic->createStatisticUser($user);
  85.                 $this->mangopay->setMangoClientUser($user);
  86.                 $this->profilePicture->createUserProfilePicture($user);
  87.                 $this->notation->createNotation($user);
  88.                 $this->lockManage->initiateStatut($user);
  89.                 return $this->guardHandler->authenticateUser(
  90.                     $user,
  91.                     $this->authenticator,
  92.                     $request
  93.                 );
  94.             } else {
  95.                 $type 'warning';
  96.                 $message "Adresse mail déjà utilisée par un autre utilisateur";
  97.                 $this->addFlash($type$message);
  98.                 return $this->redirectToRoute('app_register');
  99.             }
  100.         }
  101.         return $this->render('registration/register.html.twig', [
  102.             'registrationForm' => $form->createView(),
  103.         ]);
  104.     }
  105. }