src/Controller/CustomerController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\SessionService;
  4. use App\Form\RegistrationFormType;
  5. use App\Form\CustomerProfilFormType;
  6. use App\Entity\Customer\Customer;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. // use App\Security\EmailVerifier;
  13. use App\Security\CustomerFormAuthenticator;
  14. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  15. use Symfony\Component\Mime\Address;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  18. use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface;
  19. use Symfony\Component\Form\FormError;
  20. use App\Service\Tools;
  21. class CustomerController extends AbstractController
  22. {
  23.     private $requestStack;
  24.     private $url_bridge_shop 'https://boutique.1055.fr/bridge-customers.php';
  25.     private $centre_prefere 'besacon';
  26.     // private $emailVerifier;
  27.     public function __construct(RequestStack $requestStack /*, EmailVerifier $emailVerifier*/
  28.     {
  29.         $this->requestStack $requestStack;
  30.         // $this->emailVerifier = $emailVerifier;
  31.     }
  32.     /**
  33.      * @Route("/mon-compte", name="account")
  34.      */
  35.     public function dashboardCustomer(Request $request): Response
  36.     {
  37.         // $em = $this->getDoctrine()->getManager('customer');
  38.         $session $this->requestStack->getSession();
  39.         $cart $session->get('cart');
  40.         if($this->container->get('security.token_storage')->getToken() != null) {
  41.             $time = new \DateTime(date("Y-m-d H:i:s"));
  42.             $customer $this->container->get('security.token_storage')->getToken()->getUser();
  43.             $form $this->createForm(CustomerProfilFormType::class, $customer);
  44.             $form->handleRequest($request);
  45.             if ($form->isSubmitted() && $form->isValid()) {
  46.                 $customer->setDateUpd($time);
  47.                 $entityManager $this->getDoctrine()->getManager('customer');
  48.                 $entityManager->persist($customer);
  49.                 $entityManager->flush();
  50.             }
  51.             return $this->render('mon-compte/dashboard.html.twig', [
  52.                 'customerProfilForm' => $form->createView(),
  53.                 'cart' => $cart,
  54.                 'controller_name' => 'CustomerController',
  55.             ]);
  56.         }
  57.         return $this->render('mon-compte/dashboard.html.twig', [
  58.             'cart' => $cart,
  59.             'controller_name' => 'CustomerController',
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/inscription", name="app_register")
  64.      */
  65.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherInterfaceGuardAuthenticatorHandler $guardHandlerCustomerFormAuthenticator $authenticator): Response
  66.     {
  67.         $time = new \DateTime(date("Y-m-d H:i:s"));
  68.         $user = new Customer();
  69.         $form $this->createForm(RegistrationFormType::class, $user);
  70.         $form->handleRequest($request);
  71.         if($form->isSubmitted()) {
  72.             $query = array( 'check_customer' => true'mail' => $request->request->get('registration_form')['email'], 'secret' => '''centre' => $this->centre_prefere );
  73.             $check_shop_customer json_decode(Tools::CallAPI('POST'$this->url_bridge_shop$query));
  74.             if($check_shop_customer->exist) {
  75.                 $form['email']->addError(new FormError('Un compte est déjà associé à ce mail'));
  76.             }
  77.         }
  78.         if ($form->isSubmitted() && $form->isValid()) {
  79.             $phone $user->getPhone();
  80.             $phoneMobile $user->getPhoneMobile();
  81.             $user->setPhone(str_replace('±','+',$phone));
  82.             $user->setPhoneMobile(str_replace('±','+',$phoneMobile));
  83.             // encode the plain password
  84.             $user->setPassword(
  85.             $userPasswordHasherInterface->hashPassword(
  86.                     $user,
  87.                     $form->get('plainPassword')->getData()
  88.                 )
  89.             );
  90.             $user->setDateAdd($time);
  91.             $user->setDateUpd($time);
  92.             // $user->setDateConnexion($time);
  93.             // $user->setActive(false);
  94.             $entityManager $this->getDoctrine()->getManager('customer');
  95.             $entityManager->persist($user);
  96.             $entityManager->flush();
  97.             // $number =  (int)$user->getId() + 100000;
  98.             $number =  str_pad((int)$user->getId(), 6'0'STR_PAD_LEFT);
  99.             $user->setCustomerNumber($number);
  100.             $user->setStatut('Actif');
  101.             $user->setActive(1);
  102.             $entityManager->persist($user);
  103.             $entityManager->flush();   
  104.             // // generate a signed url and email it to the user
  105.             // $this->emailVerifier->sendEmailConfirmation('app_verify_email', $user,
  106.             //     (new TemplatedEmail())
  107.             //         ->from(new Address('noreply@1055.fr', 'Team 1055'))
  108.             //         ->to($user->getMail())
  109.             //         ->subject('Veuillez confirmer votre email')
  110.             //         ->htmlTemplate('registration/confirmation_email.html.twig')
  111.             // );
  112.             // // do anything else you need here, like send an email
  113.             
  114.             $guardHandler->authenticateUserAndHandleSuccess(
  115.                 $user,
  116.                 $request,
  117.                 $authenticator,
  118.                 'main' // firewall name in security.yaml
  119.             );
  120.             $query = array( 'create_customer_shop' => true'mail' => $request->request->get('registration_form')['email'], 'secret' => '''centre' => $this->centre_prefere );
  121.             $check_shop_customer json_decode(Tools::CallAPI('POST'$this->url_bridge_shop$query));
  122.             return $this->redirectToRoute('home');
  123.         }
  124.         return $this->render('mon-compte/register.html.twig', [
  125.             'registrationForm' => $form->createView(),
  126.         ]);
  127.     }
  128.     /**
  129.      * @Route("/verify/email", name="app_verify_email")
  130.      */
  131.     public function verifyUserEmail(Request $request): Response
  132.     {
  133.         // dd($this->getUser());
  134.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  135.         // validate email confirmation link, sets Customer::isVerified=true and persists
  136.         try {
  137.             $this->emailVerifier->handleEmailConfirmation($request$this->getUser());
  138.         } catch (VerifyEmailExceptionInterface $exception) {
  139.             $this->addFlash('verify_email_error'$exception->getReason());
  140.             return $this->redirectToRoute('app_register');
  141.         }
  142.         // @TODO Change the redirect on success and handle or remove the flash message in your templates
  143.         $this->addFlash('success''Votre adresse e-mail a été vérifiée.');
  144.         return $this->redirectToRoute('customer_login');
  145.     }
  146.     /**
  147.      * @Route("/send_verify/email", name="app_resend_verify_email")
  148.      */
  149.     public function resendEmail(Request $request): Response 
  150.     {
  151.         $customer $this->getDoctrine()
  152.             ->getRepository(Customer::class)
  153.             ->find((int)$request->query->get('id'));
  154.         $this->emailVerifier->sendEmailConfirmation('app_verify_email'$customer,
  155.             (new TemplatedEmail())
  156.                 ->from(new Address('noreply@1055.fr''Team 1055'))
  157.                 ->to($customer->getEmail())
  158.                 ->subject('Veuillez confirmer votre email')
  159.                 ->htmlTemplate('registration/confirmation_email.html.twig')
  160.         );
  161.         $this->addFlash('success''Un mail d\'activation à été envoyé à l\'adresse mail '.$customer->getMail());
  162.         return $this->redirectToRoute('customer_login');
  163.     }
  164. }