<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\UserAddress;
use App\Entity\UserInformation;
use App\Entity\UserProfile;
use App\Form\UserType;
use App\Repository\UserRepository;
use App\Service\MailService;
use App\Service\SalesforceService;
use App\Service\UserService;
use App\Service\Wordpress\UserService as WordpressUserService;
use App\Service\Xero\ContactService;
use App\String\Constant;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class RegistrationController extends AbstractController
{
private $passwordHasher;
private $em;
private MailService $mailService;
private const HONEYPOT_EMAIL_NAME = 'emailAddress';
private const HONEYPOT_FULLNAME_NAME = 'fullName';
public function __construct(UserPasswordHasherInterface $passwordHasher, ManagerRegistry $doctrine, MailService $mailService)
{
$this->passwordHasher = $passwordHasher;
$this->em = $doctrine->getManager();
$this->mailService = $mailService;
}
/**
* @Route("/registration", name="registration")
*/
public function index(Request $request, SalesforceService $sfService, ContactService $xeroContactService, UserService $userService, WordpressUserService $wpUserService, UserRepository $userRepository)
{
$user = (new User())
->setUserProfile(new UserProfile())
->setUserAddress(new UserAddress())
->setUserInformation(new UserInformation())
;
if (!empty($_GET['email'])) {
$user->setEmail(urldecode($_GET['email']));
}
if (!empty($_GET['biography'])) {
$userProfile = $user->getUserProfile();
$userProfile->setShortDescription(urldecode($_GET['biography']));
}
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// Check honeypot spam first
$formData = $form->getData();
$emailHoneypot = $form[UserType::HONEYPOT_EMAIL_NAME]->getData();
$fullnameHoneypot = $form[UserType::HONEYPOT_FULLNAME_NAME]->getData();
// REMOVED THIS FUNCTIONALITY BECAUSE IT HINDERS SOME OF THE USER REGISTRATION THAT USE GOOGLE AUTOFILL
// if (!empty($emailHoneypot) || !empty($fullnameHoneypot)) {
// $this->addFlash('notice', 'SPAM DETECTED: Sorry, you can\'t register during this time. Please try again.');
// return $this->redirectToRoute('registration');
// }
// Replaced honeypot with roles detection since bot can't select any roles
if (count($user->getRoles()) <= 2) {
$this->addFlash('notice', 'SPAM DETECTED: Sorry, you can\'t register during this time. Please try again.');
return $this->redirectToRoute('registration');
}
$userObj = $userService->findUserByEmail($user->getEmail());
if ($userObj) {
$this->addFlash('notice', 'User account with this email already exists');
return $this->redirectToRoute('registration');
}
$userObj = $userRepository->findOneBy(['phone' => $user->getPhone()]);
if ($userObj) {
$this->addFlash('notice', 'Please log into your account or use a different phone number.');
return $this->redirectToRoute('registration');
}
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $this->passwordHasher->hashPassword(
$user,
$user->getPassword()
);
$user->setPassword($hashedPassword);
$roles = $user->getRoles();
$roles = array_merge($roles, ['ROLE_PORTALUSER']);
$user->setRoles($roles); // Set their role
// Save
$this->em->persist($user);
$this->em->flush();
// Create salesforce objs
$this->setContactSalesforce($sfService, $user);
// Create xero contact
$this->setContactXero($xeroContactService, $user);
// Create wordpress user
$this->setContactWordpress($wpUserService, $user);
$user->setIsSuccess(true);
$this->em->persist($user);
$this->em->flush();
// Send email
$this->mailService->sendEmail(Constant::MAIL_REGISTRATION, $user);
if ($request->query->get('returnUrl')) {
$returnUrl = $request->query->get('returnUrl');
return $this->redirectToRoute('app_login', [
'returnUrl' => $returnUrl,
'email' => urlencode($user->getEmail()),
]);
}
return $this->redirectToRoute('app_registration_thankyou');
}
return $this->render('registration/index.html.twig', [
'form' => $form->createView(),
]);
}
/**
* Set contact salesforce for the first time
*
* @param SalesforceService $sfService
* @param User $user
* @return void
*/
private function setContactSalesforce(SalesforceService $sfService ,User $user)
{
if ($user->getOrganisation()) {
$accountSalesforceId = $sfService->createAccount($user->getOrganisation());
if ($accountSalesforceId) {
$user->setAccountSalesforceId($accountSalesforceId);
}
}
$salesforceId = $sfService->createContact($user);
if ($salesforceId) {
$userInformation = $user->getUserInformation();
$userInformation->setSalesforceId($salesforceId);
}
$this->em->persist($userInformation);
$this->em->flush();
}
/**
* Set xero contact for the first time
*
* @param ContactService $xeroContactService
* @param User $user
* @return void
*/
private function setContactXero(ContactService $xeroContactService, User $user)
{
$xeroContactId = $xeroContactService->createContact($user);
if ($xeroContactId) {
$userInformation = $user->getUserInformation();
$userInformation->setXeroId($xeroContactId);
}
$this->em->persist($userInformation);
$this->em->flush();
}
private function setContactWordpress(WordpressUserService $userService, User $user)
{
$wordpressId = $userService->createUser($user);
if ($wordpressId) {
$userInformation = $user->getUserInformation();
$userInformation->setWordpressId($wordpressId);
}
$this->em->persist($userInformation);
$this->em->flush();
}
}