src/Controller/RegistrationController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Form\RegistrationFormType;
  5. use App\Security\AppAuthenticator;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  14. use App\Repository\CustomerRepository;
  15. class RegistrationController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/register", name="app_register")
  19.      */
  20.     public function register(Request $requestUserPasswordHasherInterface $userPasswordHasherUserAuthenticatorInterface $userAuthenticatorAppAuthenticator $authenticatorEntityManagerInterface $entityManagerManagerRegistry $registry): Response
  21.     {
  22.         $user = new Customer();
  23.         $customer_repository = new CustomerRepository($registry);
  24.         $form $this->createForm(RegistrationFormType::class, $user);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             // encode the plain password
  28.             $user->setPassword(
  29.             $userPasswordHasher->hashPassword(
  30.                     $user,
  31.                     $form->get('plainPassword')->getData()
  32.                 )
  33.             );
  34.             $this->setCustomerNumber($user,$customer_repository);
  35.             $this->setCreatedAt($user);
  36.             $entityManager->persist($user);
  37.             $entityManager->flush();
  38.             // do anything else you need here, like send an email
  39.             return $userAuthenticator->authenticateUser(
  40.                 $user,
  41.                 $authenticator,
  42.                 $request
  43.             );
  44.         }
  45.         return $this->render('registration/register.html.twig', [
  46.             'registrationForm' => $form->createView(),
  47.         ]);
  48.     }
  49.     public function setCreatedAt($entity)
  50.     {
  51.         if (property_exists($entity"createdAt")) {
  52.             $now = new \DateTime("now");
  53.             $entity->setCreatedAt($now);
  54.         }
  55.     }
  56.     public function setCustomerNumber($entity$customer_repository)
  57.     {
  58.         if (!$entity instanceof Customer) {
  59.             return;
  60.         }
  61.         $customer_number '';
  62.         for ($i 0$i 12$i++) {
  63.             $customer_number .= mt_rand(09);
  64.         }
  65.         $customer_number implode('-'str_split($customer_number3));
  66.         $customer $customer_repository->findOneByCustomerNumber($customer_number);
  67.         if ($customer) {
  68.             return $this->setCustomerNumber($entity,$customer_repository);
  69.         } else {
  70.             $entity->setCustomerNumber($customer_number);
  71.         }
  72.     }
  73. }