src/Controller/Front/AccountCreationController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\DataTransferObject\CreateUserRequest;
  4. use App\Entity\User;
  5. use App\Event\Account\UserCreationFinalizedEvent;
  6. use App\Exception\ExistingAccountException;
  7. use App\Exception\ExistingEmailException;
  8. use App\Exception\OldAccountException;
  9. use App\Exception\UserNotFoundException;
  10. use App\Form\Account\CreationType;
  11. use App\Manager\UserActivator;
  12. use App\Manager\UserManager;
  13. use App\Repository\App\UserRepository;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\Form\FormInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. /**
  23.  * @Route("/mon-compte")
  24.  */
  25. class AccountCreationController extends AbstractAccountController
  26. {
  27.     /** @var UserActivator */
  28.     protected $userActivator;
  29.     /** @var UserRepository */
  30.     protected $userRepository;
  31.     public function __construct(
  32.         UserManager $userManager,
  33.         EventDispatcherInterface $dispatcher,
  34.         TranslatorInterface $translator,
  35.         TokenStorageInterface $tokenStorage,
  36.         RequestStack $requestStack,
  37.         UserActivator $userActivator,
  38.         UserRepository $userRepository
  39.     ) {
  40.         parent::__construct($userManager$dispatcher$translator$tokenStorage$requestStack);
  41.         $this->userActivator $userActivator;
  42.         $this->userRepository $userRepository;
  43.     }
  44.     /**
  45.      * @Route("/initialisation", name="account_creation")
  46.      */
  47.     public function initializeCreation(Request $request): Response
  48.     {
  49.         $data = new CreateUserRequest();
  50.         $form $this->createForm(CreationType::class, $data);
  51.         $form->handleRequest($request);
  52.         if ($form->isSubmitted() && $form->isValid()) {
  53.             $user null;
  54.             try {
  55.                 $user $this->userManager->createFromRequest($data);
  56.             } catch (UserNotFoundException $e) {
  57.                 return $this->displayUserCreationError(
  58.                     $form,
  59.                     $this->transFront('account_creation.step1.message.user_not_found.html')
  60.                 );
  61.             } catch (ExistingEmailException $e) {
  62.                 return $this->displayUserCreationError(
  63.                     $form,
  64.                     $this->transFront('account_creation.step1.message.existing_email')
  65.                 );
  66.             } catch (OldAccountException $e) {
  67.                 return $this->displayUserCreationSuccess(
  68.                     $this->userManager->create($data),
  69.                     $this->transFront('account_creation.step1.message.old_account')
  70.                 );
  71.             } catch (ExistingAccountException $e) {
  72.                 return $this->manageExistingAccountCreation($form);
  73.             }
  74.             return $this->displayUserCreationSuccess($user$this->transFront('account_creation.step1.message.success'));
  75.         }
  76.         return $this->render('front/account/creation/form.html.twig', ['form' => $form->createView()]);
  77.     }
  78.     /**
  79.      * @Route("/finalisation/{passwordCreationToken}", name="account_creation_step2")
  80.      */
  81.     public function finalizeCreation(Request $requeststring $passwordCreationToken): Response
  82.     {
  83.         return $this->createPassword(
  84.             $request,
  85.             $passwordCreationToken,
  86.             $this->transFront('account_creation.step2.page_title'),
  87.             $this->transFront('account_creation.label_step'),
  88.             2,
  89.             2,
  90.             $this->transFront('account_creation.step2.message.success'),
  91.             UserCreationFinalizedEvent::class,
  92.             'account_creation.step2.form.submit',
  93.             true
  94.         );
  95.     }
  96.     /**
  97.      * @Route("/mise-a-jour", name="account_update")
  98.      */
  99.     public function initializeImport(): Response
  100.     {
  101.         return $this->render('front/account/import/message.html.twig');
  102.     }
  103.     protected function displayUserCreationSuccess(User $userstring $message): Response
  104.     {
  105.         $this->getDoctrine()->getManager()->persist($user);
  106.         $this->userActivator->sendActivation($user);
  107.         return $this->render('front/account/creation/step1_success.html.twig', [
  108.             'page_title'          => $this->transFront('account_creation.step1.page_title'),
  109.             'step_title'          => $this->transFront('account_creation.step1.title'),
  110.             'confirmationMessage' => $message,
  111.         ]);
  112.     }
  113.     protected function displayUserCreationError(FormInterface $formstring $message): Response
  114.     {
  115.         $this->addFlash('error'$message);
  116.         return $this->render('front/account/creation/form.html.twig', ['form' => $form->createView()]);
  117.     }
  118.     protected function manageExistingAccountCreation(FormInterface $form): Response
  119.     {
  120.         $user $this->userRepository->findOneBy([
  121.             'customerNumber' => $form->get('customerNumber')->getData(),
  122.             'activated'      => false,
  123.         ]);
  124.         if ($user instanceof User) {
  125.             return $this->reactivateUser($user$form);
  126.         }
  127.         return $this->displayUserCreationError(
  128.             $form,
  129.             $this->transFront(
  130.                 'account_creation.step1.message.existing_account',
  131.                 ['{url}' => $this->generateUrl('app_login')]
  132.             )
  133.         );
  134.     }
  135.     protected function reactivateUser(User $userFormInterface $form): Response
  136.     {
  137.         $email $form->get('email')->getData();
  138.         if (== $this->userRepository->countOtherCustomerWithEmail($user->getCustomerNumber(), $email)) {
  139.             $user->setEmail($email);
  140.             return $this->displayUserCreationSuccess(
  141.                 $user,
  142.                 $this->transFront(
  143.                     'account_creation.step1.message.existing_account_activation.html',
  144.                     ['{url}' => $this->generateUrl('app_login')]
  145.                 )
  146.             );
  147.         }
  148.         return $this->displayUserCreationError(
  149.             $form,
  150.             $this->transFront('account_creation.step1.message.existing_email')
  151.         );
  152.     }
  153. }