<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\UserRetailer;
use App\Form\Registration\RegistrationFormFlow;
use App\Service\EmailService;
use App\Service\PageService;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class RegistrationController extends AbstractController
{
/**
* @Route("/registration", name="registration")
*/
public function register(Request $request, RegistrationFormFlow $formFlow, UserPasswordHasherInterface $passwordEncoder, EventDispatcherInterface $eventDispatcher, EntityManagerInterface $entityManager, PageService $pageService, EmailService $emailService, EventDispatcherInterface $event = null): Response
{
$user = $this->getUser();
$session = $request->getSession();
if(!$user){
$user = new User();
}else{
// Check if user is being redirected due to tax form expiration
$taxFormExpired = $session->get('tax_form_expired', false);
if($user->getCompleted() && !$taxFormExpired){
return $this->redirectToRoute('submission.list');
}
// If tax form has expired, set W8 form date to null to force re-completion
if ($taxFormExpired && $user->getCountry() === 'CA') {
$user->setW8FormDate(null);
$entityManager->persist($user);
$entityManager->flush();
$session->remove('tax_form_expired');
}
}
$formFlow->bind($user);
$form = $formFlow->createForm();
$form->handleRequest($request);
#dump($user);die;
if($form->isSubmitted() && $form->isValid()){
$formFlow->saveCurrentStepData($form);
if ($formFlow->nextStep()) {
$stepLabel = $formFlow->getStep($formFlow->getCurrentStep())->getLabel();
$form = $formFlow->createForm();
}else{
if(!$user->getId()){
// encode the plain password
$user->setPassword(
$passwordEncoder->hashPassword(
$user,
$user->plainPassword
)
);
}
$user->setCompleted(true);
if ($user->retailer){
// Set the selected brand in the UserRetailer
if ($user->brand) {
$user->retailer->setBrand($user->brand);
}
$user->retailer->setRetailer($user->retailer->getStore()->getRetailer());
// if user is complete registration, set manage status to approved
if($user->getId()){
$user->retailer->setManageStatus(UserRetailer::MANAGE_STATUS_APPROVED);
$user->retailer->setReviewedAt(new \DateTime());
}
$user->addUserRetailer($user->retailer);
}
if ($user->invitationCode){
$userInvitationCode = $user->invitationCode;
$userRetailer = new UserRetailer();
$userRetailer
->setRetailer($userInvitationCode->getInvitationCode()->getRetailer())
->setInvitationCode($userInvitationCode->getInvitationCode());
$user->addUserRetailer($userRetailer);
}
$user->setUserInvitationCodes([]);
$entityManager->persist($user);
$entityManager->flush();
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$session = $request->getSession();
$session->set('_security_main', serialize($token));
/** Fire the login event manually */
$event = new InteractiveLoginEvent($request, $token);
$eventDispatcher->dispatch($event, 'security.interactive_login');
if(!$user->getId()) {
$emailService->registrationConfirmation($user);
}
$this->addFlash('success', 'You have successfully registered and logged in.');
if ($user->brand && $user->brand->isNeedApprove()){
$approvals = $user->brand->getApprovals() ?? [];
if (in_array('corporate', $approvals)) {
$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');
} else {
$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');
}
}
// do anything else you need here, like send an email
return $this->redirectToRoute('homepage');
}
}
return $this->render('registration/register.html.twig', [
'form' => $form->createView(),
'event' => $event,
'user' => $user,
'flow' => $formFlow,
'termsAndConditions' => $pageService->getTermsPage(),
]);
}
}