src/Controller/UserController.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\User\Compte\ReferentType;
  4. use App\Form\User\Compte\UserAddress;
  5. use App\Form\User\Compte\UserBrand;
  6. use App\Form\User\Compte\UserGeneral;
  7. use App\Form\User\Compte\UserIbans;
  8. use App\Form\User\Compte\UserPassword;
  9. use App\Form\User\Compte\UserPhone;
  10. use App\Form\User\Compte\UserWholesaler;
  11. use App\Form\User\DeliveryType;
  12. use App\Form\User\UserRegisterType;
  13. use App\Manager\SponsorshipRequestManager;
  14. use App\Manager\User\UserManager;
  15. use App\Model\User\Identity;
  16. use App\Model\User\User;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  22. use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
  23. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  24. use Symfony\Component\Serializer\Serializer;
  25. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  26. class UserController extends APIController
  27. {
  28.     /**
  29.      * @Route("/user", name="user")
  30.      */
  31.     public function index()
  32.     {
  33.         return $this->render('user/index.html.twig', [
  34.             'controller_name' => 'UserController',
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/inscription", name="kiwi_register_user")
  39.      */
  40.     public function register(Request $requestSponsorshipRequestManager $sponsorshipRequestManager)
  41.     {
  42.         $referralCode $request->request->get('user_register')['referralCode'] ?? $request->query->get('referralCode') ?? null;
  43.         $user = new User();
  44.         $identity = new Identity();
  45.         if ($facebookUser =  $this->requestStack->getSession()->get('kiwi_facebook_user')) {
  46.             $facebookUser json_decode$this->requestStack->getSession()->get('kiwi_facebook_user'), true);
  47.             $identity->setFirstName($facebookUser['data']['first_name']);
  48.             $identity->setLastName($facebookUser['data']['last_name']);
  49.             $user->setIdentity($identity);
  50.             $user->setEmail($facebookUser['data']['email']);
  51.         }
  52.         $form $this->createForm(UserRegisterType::class, $user);
  53.         $user->setIdentity(
  54.             (new Identity())
  55.                 ->setFirstName('')
  56.                 ->setLastName('')
  57.         );
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted() && $form->isValid()) {
  60.             $encoders = [new JsonEncoder()];
  61.             $normalizers = [new ObjectNormalizer()];
  62.             $serializer = new Serializer($normalizers$encoders);
  63.             $json $serializer->serialize($form->getData(), 'json', [AbstractObjectNormalizer::SKIP_NULL_VALUES => true]);
  64.             $response $this->APIKiwi->register($json);
  65.             try {
  66.                 $response->getContent();
  67.                  $this->requestStack->getSession()->remove('kiwi_facebook_user');
  68.                 if ($form->getData()->getReferralCode() !== null) {
  69.                     $email $form->getData()->getEmail();
  70.                     $refferalCode $form->getData()->getReferralCode();
  71.                     $sponsorshipRequestManager->postAndCheckGodChildExist($email$refferalCode);
  72.                 }
  73.                 return $this->redirectToRoute('kiwi_congratulation_register');
  74.             } catch (ClientExceptionInterface $e) {
  75.                 $content json_decode($response->getContent(false), true);
  76.                 if( isset($content['@type']) && $content['@type'] === 'ConstraintViolationList' && isset($content['violations'])) {
  77.                     foreach ($content['violations'] as $violation) {
  78.                         $this->addFlash(
  79.                             'warning',
  80.                             sprintf("%s %s",
  81.                                 $this->translator->trans(sprintf('api.validation.properties.%s'$violation['propertyPath']), [], 'api'),
  82.                                 $this->translator->trans(sprintf('api.validation.codes.%s'$violation['code']), [], 'api')
  83.                             )
  84.                         );
  85.                     }
  86.                 } else {
  87.                     $this->logger->error(sprintf("%s : %s"'ClientException'$e->getMessage()), $e->getTrace());
  88.                     $this->addFlash('warning'$this->translator->trans("api.validation.errors.generic", [], "api"));
  89.                 }
  90.             } catch (\Exception $e) {
  91.                 $this->logger->error(sprintf("%s : %s"get_class($e),$e->getMessage()), $e->getTrace());
  92.                 $this->addFlash('warning'$this->translator->trans("api.validation.errors.generic", [], "api"));
  93.             }
  94.         }
  95.         return $this->render('user/register.html.twig', [
  96.             'form' => $form->createView(),
  97.             'referralCode' => $referralCode,
  98.         ]);
  99.     }
  100.     /**
  101.      * @Route("/mon-compte", name="kiwi_account")
  102.      * @param Request $request
  103.      * @param UserManager $userManager
  104.      * @return Response
  105.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  106.      */
  107.     public function account(Request $requestUserManager $userManagerSponsorshipRequestManager $sponsorshipRequestManager)
  108.     {
  109.         $userInterface $this->getUser();
  110.         $user $this->getUserData();
  111.         $email $user->getEmail();
  112.         $referer $request->request->get('referer');
  113.         $formGeneral $this->createForm(UserGeneral::class, $user);
  114.         $formGeneral->handleRequest($request);
  115.         if ($formGeneral->isSubmitted() && $formGeneral->isValid()) {
  116.             $data $userManager->prepareUserGeneral($user$request->request->get('user_general'), $userInterface);
  117.             if ($email != $formGeneral->getData()->getEmail()) {
  118.                 $userManager->requestUser($userInterface'/users/' $userInterface->getId(), $data'put_user_general');
  119.                 return $this->redirectToRoute('app_logout');
  120.             } else {
  121.                 $userManager->sendCreateOrUpdateDataCustomerToSalesforce($userInterface);
  122.             }
  123.             $userManager->requestUser($userInterface'/users/' $userInterface->getId(), $data'put_user_general');
  124.             if ($referer && in_array('catalog'explode('/'$referer))) {
  125.                 return $this->redirect($referer);
  126.             }
  127.         }
  128.         $formAddress $this->createForm(UserAddress::class, $user);
  129.         $formAddress->handleRequest($request);
  130.         if ($formAddress->isSubmitted() && $formAddress->isValid()) {
  131.             $data $userManager->prepareUserGeneral($user$request->request->get('user_address'), $userInterface);
  132.             $userManager->requestUser($userInterface'/users/' $userInterface->getId(), $data'put_user_general');
  133.             $userManager->sendCreateOrUpdateDataCustomerToSalesforce($userInterface);
  134.             if ($referer && in_array('catalog'explode('/'$referer))) {
  135.                 return $this->redirect($referer);
  136.             }
  137.         }
  138.         $formPassword $this->createForm(UserPassword::class, $user, ['validation_groups' => 'post_user_register_godparent']);
  139.         $formPassword->handleRequest($request);
  140.         if ($formPassword->isSubmitted() && $formPassword->isValid()) {
  141.             $data $userManager->preparePasswordChange($user$request->request->get('user_password'));
  142.             $userManager->requestUser($userInterface,'/users/passwordchange/' $user->getId(), $data'put_user_password');
  143.         }
  144.         $formBrand $this->createForm(UserBrand::class, $user);
  145.         $formBrand->handleRequest($request);
  146.         if ($formBrand->isSubmitted() && $formBrand->isValid()) {
  147.             $data $userManager->prepareOptinBrand($user$request->request->get('user_brand'));
  148.             $userManager->sendCreateOrUpdateDataCustomerToSalesforce($userInterface);
  149.             $userManager->requestUser($userInterface'/users/'.$userInterface->getId(), $data'put_optin_brand');
  150.         }
  151.         $formPhone $this->createForm(UserPhone::class, $user);
  152.         $formPhone->handleRequest($request);
  153.         if ($formPhone->isSubmitted() && $formPhone->isValid()) {
  154.             $data $userManager->prepareOptinPhone($user$request->request->get('user_phone'));
  155.             $userManager->sendCreateOrUpdateDataCustomerToSalesforce($userInterface);
  156.             $userManager->requestUser($userInterface'/users/'.$userInterface->getId(), $data'put_optin_phone');
  157.         }
  158.         $formWholesaler $this->createForm(UserWholesaler::class, $user->getAdditionalInformation());
  159.         $formWholesaler->handleRequest($request);
  160.         if ($formWholesaler->isSubmitted() && $formWholesaler->isValid()) {
  161.             $dataRequest $request->request->all();
  162.             $userData $request->request->get('user');
  163.             $newFormat = [];
  164.             foreach ($userData['additionalInformation'] as $info) {
  165.                 if (!empty($info)) {
  166.                     $newFormat[] = ["information" => $info];
  167.                 }
  168.             }
  169.             $data $userManager->prepareWholesaler($user$newFormat);
  170.             $userManager->requestUser($userInterface'/users/' $userInterface->getId(), $data'put_user_wholesaler');
  171.             return $this->redirectToRoute('kiwi_account');
  172.         }
  173.         $formReferent $this->createForm(ReferentType::class, $user);
  174.         $formReferent->handleRequest($request);
  175.         if ($formReferent->isSubmitted() && $formReferent->isValid()) {
  176.             $userManager->requestUser($userInterface'/users/' $user->getId(), $request->request->get('referent'), 'put_user_referent');
  177.         }
  178.         if (isset($_ENV['PROJECT_TYPE']) && 'puntos' === $_ENV['PROJECT_TYPE']) {
  179.             $template 'user/account_tabs.html.twig';
  180.             $formDelivery $this->createForm(DeliveryType::class, $user);
  181.             $formDelivery->handleRequest($request);
  182.             if ($formDelivery->isSubmitted() && $formDelivery->isValid()) {
  183.                 $userManager->prepareAddressDelivery($user$request->request->get('delivery'), $userInterface);
  184.                 if ($referer && in_array('catalog'explode('/'$referer))) {
  185.                     return $this->redirect($referer);
  186.                 }
  187.             }
  188.             return $this->render($template, [
  189.                 'user' => $this->getUser(),
  190.                 'email' => $user->getEmail(),
  191.                 'formGeneral' => $formGeneral->createView(),
  192.                 'formPassword' => $formPassword->createView(),
  193.                 'formBrand' => $formBrand->createView(),
  194.                 'formPhone' => $formPhone->createView(),
  195.                 'formDelivery' => $formDelivery->createView(),
  196.                 'formAddress' => $formAddress->createView(),
  197.                 'formReferent' => $formReferent->createView(),
  198.                 'formWholesaler' => $formWholesaler->createView(),
  199.             ]);
  200.         }
  201.         if (isset($_ENV['PROJECT_TYPE']) && 'step' === $_ENV['PROJECT_TYPE']) {
  202.             $template 'user/account_tabs.html.twig';
  203.             $formIban $this->createForm(UserIbans::class, $user);
  204.             $formIban->handleRequest($request);
  205.             if ($formIban->isSubmitted() && $formIban->isValid()) {
  206.                 $data $userManager->prepareIbanBic($user$request->request->get('user_ibans'));
  207.                 $userManager->requestUser($userInterface'/ibans/alone'$data->getIbans()[0], 'post_user_iban_bic''POST');
  208.             }
  209.             return $this->render($template, [
  210.                 'user' => $this->getUser(),
  211.                 'email' => $user->getEmail(),
  212.                 'formGeneral' => $formGeneral->createView(),
  213.                 'formPassword' => $formPassword->createView(),
  214.                 'formBrand' => $formBrand->createView(),
  215.                 'formPhone' => $formPhone->createView(),
  216.                 'formAddress' => $formAddress->createView(),
  217.                 'formReferent' => $formReferent->createView(),
  218.                 'formIbans' => $formIban->createView(),
  219.                 'formWholesaler' => $formWholesaler->createView(),
  220.             ]);
  221.         }
  222.         $template 'user/account.html.twig';
  223. //       ---- GESTION FORM GENERAL ----
  224.         return $this->render($template, [
  225.             'user' => $this->getUser(),
  226.             'email' => $user->getEmail(),
  227.             'formGeneral' => $formGeneral->createView(),
  228.             'formPassword' => $formPassword->createView(),
  229.             'formBrand' => $formBrand->createView(),
  230.             'formAddress' => $formAddress->createView(),
  231.             'formReferent' => $formReferent->createView(),
  232.         ]);
  233.     }
  234.     /**
  235.      * @Route("/felicitation-inscription", name="kiwi_congratulation_register")
  236.      */
  237.     public function congratulationRegister()
  238.     {
  239.         return $this->render('security/congratulation_register.html.twig');
  240.     }
  241.     /**
  242.      * @Route("/mon-espace", name="kiwi_user_space")
  243.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  244.      */
  245.     public function mySpace()
  246.     {
  247.         return $this->render('user/user_space.html.twig');
  248.     }
  249. }