src/Controller/RegistrationController.php line 134

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\UserRetailer;
  5. use App\Form\Registration\RegistrationFormFlow;
  6. use App\Service\EmailService;
  7. use App\Service\PageService;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  17. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  18. class RegistrationController extends AbstractController
  19. {
  20. /**
  21. * @Route("/registration", name="registration")
  22. */
  23. public function register(Request $request, RegistrationFormFlow $formFlow, UserPasswordHasherInterface $passwordEncoder, EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager, PageService $pageService, EmailService $emailService, EventDispatcherInterface $event = null): Response
  24. {
  25. $user = $this->getUser();
  26. $session = $request->getSession();
  27. if(!$user){
  28. $user = new User();
  29. }else{
  30. // Check if user is being redirected due to tax form expiration
  31. $taxFormExpired = $session->get('tax_form_expired', false);
  32. if($user->getCompleted() && !$taxFormExpired){
  33. return $this->redirectToRoute('submission.list');
  34. }
  35. // If tax form has expired, set W8 form date to null to force re-completion
  36. if ($taxFormExpired && $user->getCountry() === 'CA') {
  37. $user->setW8FormDate(null);
  38. $entityManager->persist($user);
  39. $entityManager->flush();
  40. $session->remove('tax_form_expired');
  41. }
  42. }
  43. $formFlow->bind($user);
  44. $form = $formFlow->createForm();
  45. $form->handleRequest($request);
  46. #dump($user);die;
  47. if($form->isSubmitted() && $form->isValid()){
  48. $formFlow->saveCurrentStepData($form);
  49. if ($formFlow->nextStep()) {
  50. $stepLabel = $formFlow->getStep($formFlow->getCurrentStep())->getLabel();
  51. $form = $formFlow->createForm();
  52. }else{
  53. if(!$user->getId()){
  54. // encode the plain password
  55. $user->setPassword(
  56. $passwordEncoder->hashPassword(
  57. $user,
  58. $user->plainPassword
  59. )
  60. );
  61. }
  62. $user->setCompleted(true);
  63. if ($user->retailer){
  64. // Set the selected brand in the UserRetailer
  65. if ($user->brand) {
  66. $user->retailer->setBrand($user->brand);
  67. }
  68. $user->retailer->setRetailer($user->retailer->getStore()->getRetailer());
  69. // if user is complete registration, set manage status to approved
  70. if($user->getId()){
  71. $user->retailer->setManageStatus(UserRetailer::MANAGE_STATUS_APPROVED);
  72. $user->retailer->setReviewedAt(new \DateTime());
  73. }
  74. $user->addUserRetailer($user->retailer);
  75. }
  76. if ($user->invitationCode){
  77. $userInvitationCode = $user->invitationCode;
  78. $userRetailer = new UserRetailer();
  79. $userRetailer
  80. ->setRetailer($userInvitationCode->getInvitationCode()->getRetailer())
  81. ->setInvitationCode($userInvitationCode->getInvitationCode());
  82. $user->addUserRetailer($userRetailer);
  83. }
  84. $user->setUserInvitationCodes([]);
  85. $entityManager->persist($user);
  86. $entityManager->flush();
  87. $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
  88. $this->get('security.token_storage')->setToken($token);
  89. $session = $request->getSession();
  90. $session->set('_security_main', serialize($token));
  91. /** Fire the login event manually */
  92. $event = new InteractiveLoginEvent($request, $token);
  93. $eventDispatcher->dispatch($event, 'security.interactive_login');
  94. if(!$user->getId()) {
  95. $emailService->registrationConfirmation($user);
  96. }
  97. $this->addFlash('success', 'You have successfully registered and logged in.');
  98. if ($user->brand && $user->brand->isNeedApprove()){
  99. $approvals = $user->brand->getApprovals() ?? [];
  100. if (in_array('corporate', $approvals)) {
  101. $this->addFlash('success', 'The brand manager will receive an email to review your account. Once approved, you will be notified via email. Thank you for registering with Jewelry Rewards');
  102. } else {
  103. $this->addFlash('success', 'Your store manager will receive an email to review your account. Once approved, you will be notified via email. Thank you for registering with Jewelry Rewards');
  104. }
  105. }
  106. // do anything else you need here, like send an email
  107. return $this->redirectToRoute('homepage');
  108. }
  109. }
  110. return $this->render('registration/register.html.twig', [
  111. 'form' => $form->createView(),
  112. 'event' => $event,
  113. 'user' => $user,
  114. 'flow' => $formFlow,
  115. 'termsAndConditions' => $pageService->getTermsPage(),
  116. ]);
  117. }
  118. }