src/Controller/RegistrationController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\UserAddress;
  5. use App\Entity\UserInformation;
  6. use App\Entity\UserProfile;
  7. use App\Form\UserType;
  8. use App\Repository\UserRepository;
  9. use App\Service\MailService;
  10. use App\Service\SalesforceService;
  11. use App\Service\UserService;
  12. use App\Service\Wordpress\UserService as WordpressUserService;
  13. use App\Service\Xero\ContactService;
  14. use App\String\Constant;
  15. use Doctrine\Persistence\ManagerRegistry;
  16. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  20. class RegistrationController extends AbstractController
  21. {
  22.     private $passwordHasher;
  23.     private $em;
  24.     private MailService $mailService;
  25.     private const HONEYPOT_EMAIL_NAME 'emailAddress';
  26.     private const HONEYPOT_FULLNAME_NAME 'fullName';
  27.     public function __construct(UserPasswordHasherInterface $passwordHasherManagerRegistry $doctrineMailService $mailService)
  28.     {
  29.         $this->passwordHasher $passwordHasher;
  30.         $this->em $doctrine->getManager();
  31.         $this->mailService $mailService;
  32.     }
  33.     /**
  34.      * @Route("/registration", name="registration")
  35.      */
  36.     public function index(Request $requestSalesforceService $sfServiceContactService $xeroContactServiceUserService $userServiceWordpressUserService $wpUserServiceUserRepository $userRepository)
  37.     {
  38.         $user = (new User())
  39.             ->setUserProfile(new UserProfile())
  40.             ->setUserAddress(new UserAddress())
  41.             ->setUserInformation(new UserInformation())
  42.         ;
  43.         if (!empty($_GET['email'])) {
  44.             $user->setEmail(urldecode($_GET['email']));
  45.         }
  46.         if (!empty($_GET['biography'])) {
  47.             $userProfile $user->getUserProfile();
  48.             $userProfile->setShortDescription(urldecode($_GET['biography']));
  49.         }
  50.         $form $this->createForm(UserType::class, $user);
  51.         $form->handleRequest($request);
  52.         if ($form->isSubmitted() && $form->isValid()) {
  53.              // Check honeypot spam first
  54.             $formData $form->getData();
  55.             $emailHoneypot $form[UserType::HONEYPOT_EMAIL_NAME]->getData();
  56.             $fullnameHoneypot =  $form[UserType::HONEYPOT_FULLNAME_NAME]->getData();
  57.             // REMOVED THIS FUNCTIONALITY BECAUSE IT HINDERS SOME OF THE USER REGISTRATION THAT USE GOOGLE AUTOFILL
  58.             // if (!empty($emailHoneypot) || !empty($fullnameHoneypot)) {
  59.             //     $this->addFlash('notice', 'SPAM DETECTED: Sorry, you can\'t register during this time. Please try again.');
  60.             //     return $this->redirectToRoute('registration'); 
  61.             // }
  62.             // Replaced honeypot with roles detection since bot can't select any roles
  63.             if (count($user->getRoles()) <= 2) {
  64.                 $this->addFlash('notice''SPAM DETECTED: Sorry, you can\'t register during this time. Please try again.');
  65.                 return $this->redirectToRoute('registration'); 
  66.             }
  67.             $userObj $userService->findUserByEmail($user->getEmail());
  68.             if ($userObj) {
  69.                 $this->addFlash('notice''User account with this email already exists');
  70.                 return $this->redirectToRoute('registration'); 
  71.             }
  72.             $userObj $userRepository->findOneBy(['phone' => $user->getPhone()]);
  73.             if ($userObj) {
  74.                 $this->addFlash('notice''Please log into your account or use a different phone number.');
  75.                 return $this->redirectToRoute('registration'); 
  76.             }
  77.             // hash the password (based on the security.yaml config for the $user class)
  78.             $hashedPassword $this->passwordHasher->hashPassword(
  79.                 $user,
  80.                 $user->getPassword()
  81.             );
  82.             $user->setPassword($hashedPassword);
  83.             $roles $user->getRoles();
  84.             $roles array_merge($roles, ['ROLE_PORTALUSER']);
  85.             $user->setRoles($roles); // Set their role
  86.             // Save
  87.             $this->em->persist($user);
  88.             $this->em->flush();
  89.             // Create salesforce objs
  90.             $this->setContactSalesforce($sfService$user);
  91.             // Create xero contact
  92.             $this->setContactXero($xeroContactService$user);
  93.             // Create wordpress user
  94.             $this->setContactWordpress($wpUserService$user);
  95.             $user->setIsSuccess(true);
  96.             $this->em->persist($user);
  97.             $this->em->flush();
  98.             // Send email
  99.             $this->mailService->sendEmail(Constant::MAIL_REGISTRATION$user);
  100.             if ($request->query->get('returnUrl')) {
  101.                 $returnUrl $request->query->get('returnUrl');
  102.                 return $this->redirectToRoute('app_login', [
  103.                     'returnUrl' => $returnUrl,
  104.                     'email' => urlencode($user->getEmail()),
  105.                 ]);
  106.             }  
  107.             return $this->redirectToRoute('app_registration_thankyou'); 
  108.         }
  109.         return $this->render('registration/index.html.twig', [
  110.             'form' => $form->createView(),
  111.         ]);
  112.     }
  113.     /**
  114.      * Set contact salesforce for the first time
  115.      *
  116.      * @param SalesforceService $sfService
  117.      * @param User $user
  118.      * @return void
  119.      */
  120.     private function setContactSalesforce(SalesforceService $sfService ,User $user)
  121.     {
  122.         if ($user->getOrganisation()) {
  123.             $accountSalesforceId $sfService->createAccount($user->getOrganisation());
  124.             if ($accountSalesforceId) {
  125.                 $user->setAccountSalesforceId($accountSalesforceId);
  126.             }
  127.         }
  128.         $salesforceId $sfService->createContact($user);
  129.         if ($salesforceId) {
  130.             $userInformation $user->getUserInformation();
  131.             $userInformation->setSalesforceId($salesforceId);
  132.         }
  133.         $this->em->persist($userInformation);
  134.         $this->em->flush();
  135.     }
  136.     /**
  137.      * Set xero contact for the first time
  138.      *
  139.      * @param ContactService $xeroContactService
  140.      * @param User $user
  141.      * @return void
  142.      */
  143.     private function setContactXero(ContactService $xeroContactServiceUser $user)
  144.     {
  145.         $xeroContactId $xeroContactService->createContact($user);
  146.         if ($xeroContactId) {
  147.             $userInformation $user->getUserInformation();
  148.             $userInformation->setXeroId($xeroContactId);
  149.         }
  150.         $this->em->persist($userInformation);
  151.         $this->em->flush();
  152.     }
  153.     private function setContactWordpress(WordpressUserService $userServiceUser $user)
  154.     {
  155.         $wordpressId $userService->createUser($user);
  156.         if ($wordpressId) {
  157.             $userInformation $user->getUserInformation();
  158.             $userInformation->setWordpressId($wordpressId);
  159.         }
  160.         $this->em->persist($userInformation);
  161.         $this->em->flush();
  162.     }
  163. }