src/Controller/Auth/ResetSmsPasswordController.php line 53

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Auth;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use App\Entity\Auth\CodeSmsPassword;
  6. use App\Entity\Auth\ResetSmsPassword;
  7. use App\Form\Auth\UpdatePasswordType;
  8. use App\Form\Auth\CodeSmsPasswordType;
  9. use App\Form\Auth\ResetSmsPasswordType;
  10. use App\Utils\InfoBip\InfoBip;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use infobip\api\client\SendSingleTextualSms;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use infobip\api\configuration\BasicAuthConfiguration;
  16. use infobip\api\model\sms\mt\send\textual\SMSTextualRequest;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  19. /**
  20.  * @Route("/reset/sms/password")
  21.  */
  22. class ResetSmsPasswordController extends AbstractController
  23. {
  24.     protected $userRepository;
  25.     private $smsClient;
  26.     protected $entityManager;
  27.     /**
  28.      * @var InfoBip
  29.      */
  30.     protected $infoBip;
  31.     public function __construct(
  32.         UserRepository $userRepository
  33.         EntityManagerInterface $entityManager
  34.         string $smsInfobipUsername
  35.         string $smsInfobipPassword,
  36.         InfoBip $infoBip
  37.     )
  38.     {
  39.         $this->userRepository $userRepository;
  40.         $this->entityManager $entityManager;
  41.         $this->infoBip $infoBip;
  42.         //$this->smsClient = new SendSingleTextualSms(new BasicAuthConfiguration($smsInfobipUsername, $smsInfobipPassword));
  43.     }
  44.     /**
  45.      * @Route("/", name="reset_sms_password")
  46.      */
  47.     public function index(Request $request)
  48.     {
  49.         $searchForm = new ResetSmsPassword();
  50.         $formSearch $this->createForm(ResetSmsPasswordType::class, $searchForm);
  51.         $formSearch->handleRequest($request);
  52.         return $this->render('auth/reset_sms_password/index.html.twig', [
  53.             "formSearch"        => $formSearch->createView(),
  54.         ]);
  55.     }
  56.     /**
  57.      * @Route("/search/p", name="reset_sms_password.search")
  58.      */
  59.     public function Resultsearch(Request $request)
  60.     {
  61.         $searchForm = new CodeSmsPassword();
  62.         $formSearch $this->createForm(CodeSmsPasswordType::class, $searchForm);
  63.         $formSearch->handleRequest($request);
  64.         $agent $this->userRepository->findOneBy(["phoneNumber" => $searchForm->getQuery()]);
  65.         if(empty($agent)){
  66.     
  67.             $this->addFlash("notRespond","Ce numéro n'est pas réconnu par la plateforme Baadhi");
  68.             return $this->redirectToRoute("reset_sms_password");
  69.         }else{
  70.             // $phoneNumbers = array();
  71.             // $number = $agent->getPhoneNumber();
  72.             // $message = "Bonjour mr(me) ".$agent->getNames()." votre code est [ ". $agent->getAccountVerification()->getPhoneCode()." ]";
  73.             // if($number){
  74.             //     $number = str_replace(' ', '', $number);
  75.             //     // Grab the first number. 
  76.             //     $first_number = substr($number, 0, 1); 
  77.             //     if ($first_number == 0) {
  78.             //         // Check if the first number is 0.
  79.             //         // Get rid of the first number.
  80.             //         $number = substr($number, 1, 999); 
  81.             //     }
  82.             // }
  83.             // if(strlen($number) === 9){
  84.             //     $phone = '243' . $number;
  85.             // }elseif(strlen($number) === 12){
  86.             //     $phone = $number;
  87.             // }
  88.             // $phoneNumbers[] = $phone;
  89.             
  90.             // foreach ($phoneNumbers as $phoneNumber) {
  91.             //     $destination = new Destination();
  92.             //     $destination->setTo($phoneNumber);
  93.             //     $destinations[] = $destination;
  94.             // }
  95.             // $message = new Message();
  96.             // $message->setFrom("Support Baadhi");
  97.             // $message->setDestinations($destinations);
  98.             // $message->setText($message);
  99.             
  100.             // $requestBody = new SMSAdvancedTextualRequest();
  101.             // $requestBody->setMessages([$message]);
  102.     
  103.             // // Executing response
  104.             // $this->smsClient->execute($requestBody);
  105.             $this->messageTemplate($agent);
  106.         }
  107.         
  108.         return $this->render('auth/reset_sms_password/result.html.twig', [
  109.             "formSearch"        => $formSearch->createView(),
  110.             "agent"            => $agent
  111.         ]);
  112.     }
  113.     /**
  114.      * @Route("/confirm/{slug}", name="reset_sms_password.confirm-code")
  115.      */
  116.     public function confirmCode(Request $requestUser $agent)
  117.     {
  118.         $searchForm = new CodeSmsPassword();
  119.         $confirmSearch $this->createForm(CodeSmsPasswordType::class, $searchForm);
  120.         $confirmSearch->handleRequest($request);
  121.         if ($confirmSearch->isSubmitted() && $confirmSearch->isValid()) {
  122.             if(!empty($searchForm->getQuery())){
  123.                 $check $this->userRepository->findOneBySmsCode($agent->getSlug(),$searchForm->getQuery());
  124.                 
  125.                 if($check){
  126.                     return $this->redirectToRoute('reset_sms_password.new-password',["slug" => $agent->getSlug()]);
  127.                 }else{
  128.                     $this->addFlash("badCode","Votre code de confirmation ne correspond pas à votre compte");
  129.                     return $this->redirectToRoute('reset_sms_password.confirm-code',["slug" => $agent->getSlug()]);
  130.                 }
  131.             }
  132.         }
  133.         return $this->render('auth/reset_sms_password/confirm.html.twig', [
  134.             "confirmSearch"        => $confirmSearch->createView(),
  135.             "agent"            => $agent
  136.         ]);
  137.     }
  138.     /**
  139.      * @Route("/{slug}/update-email", name="reset_sms_password.new-password")
  140.      */
  141.     public function updateEmail(Request $request,User $userUserPasswordEncoderInterface $passwordEncoder)
  142.     {   
  143.         $form $this->createForm(UpdatePasswordType::class, $user);
  144.         $form->handleRequest($request);
  145.         if ($form->isSubmitted() && $form->isValid()) {
  146.             $user->setPassword(
  147.                 $passwordEncoder->encodePassword(
  148.                     $user,
  149.                     $form->get('password')->getData()
  150.                 )
  151.             );
  152.             $this->addFlash("resetPassword","Votre mot de passe a été modifié avec succès");
  153.             $this->entityManager->flush();
  154.             return $this->redirectToRoute('app.login');
  155.         }
  156.         return $this->render('auth/reset_sms_password/update-password.html.twig', [
  157.             'formUpdatePassword'          => $form->createView(),
  158.             "user"  => $user
  159.         ]);
  160.     }
  161.     public function messageTemplate($agent){
  162.         if ($agent->getSexe() == "male") {
  163.             $sexe "monsieur";
  164.         }else {
  165.             $sexe "madame";
  166.         }
  167.         $msg "Bonjour ".$sexe." ".$agent->getNames()." votre code de confirmation pour votre mot de passe est  [ "$agent->getAccountVerification()->getPhoneCode()." ]";
  168.         
  169.         try {
  170.             $this->infoBip->sendSMS($agent->getPhoneNumber(), $msg);
  171.         } catch (\Throwable $th) {
  172.             //throw $th;
  173.         }
  174.         return $this;
  175.     }
  176. }