<?php
namespace App\Controller\Auth;
use App\Entity\User;
use App\Repository\UserRepository;
use App\Entity\Auth\CodeSmsPassword;
use App\Entity\Auth\ResetSmsPassword;
use App\Form\Auth\UpdatePasswordType;
use App\Form\Auth\CodeSmsPasswordType;
use App\Form\Auth\ResetSmsPasswordType;
use App\Utils\InfoBip\InfoBip;
use Doctrine\ORM\EntityManagerInterface;
use infobip\api\client\SendSingleTextualSms;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use infobip\api\configuration\BasicAuthConfiguration;
use infobip\api\model\sms\mt\send\textual\SMSTextualRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* @Route("/reset/sms/password")
*/
class ResetSmsPasswordController extends AbstractController
{
protected $userRepository;
private $smsClient;
protected $entityManager;
/**
* @var InfoBip
*/
protected $infoBip;
public function __construct(
UserRepository $userRepository,
EntityManagerInterface $entityManager,
string $smsInfobipUsername,
string $smsInfobipPassword,
InfoBip $infoBip
)
{
$this->userRepository = $userRepository;
$this->entityManager = $entityManager;
$this->infoBip = $infoBip;
//$this->smsClient = new SendSingleTextualSms(new BasicAuthConfiguration($smsInfobipUsername, $smsInfobipPassword));
}
/**
* @Route("/", name="reset_sms_password")
*/
public function index(Request $request)
{
$searchForm = new ResetSmsPassword();
$formSearch = $this->createForm(ResetSmsPasswordType::class, $searchForm);
$formSearch->handleRequest($request);
return $this->render('auth/reset_sms_password/index.html.twig', [
"formSearch" => $formSearch->createView(),
]);
}
/**
* @Route("/search/p", name="reset_sms_password.search")
*/
public function Resultsearch(Request $request)
{
$searchForm = new CodeSmsPassword();
$formSearch = $this->createForm(CodeSmsPasswordType::class, $searchForm);
$formSearch->handleRequest($request);
$agent = $this->userRepository->findOneBy(["phoneNumber" => $searchForm->getQuery()]);
if(empty($agent)){
$this->addFlash("notRespond","Ce numéro n'est pas réconnu par la plateforme Baadhi");
return $this->redirectToRoute("reset_sms_password");
}else{
// $phoneNumbers = array();
// $number = $agent->getPhoneNumber();
// $message = "Bonjour mr(me) ".$agent->getNames()." votre code est [ ". $agent->getAccountVerification()->getPhoneCode()." ]";
// if($number){
// $number = str_replace(' ', '', $number);
// // Grab the first number.
// $first_number = substr($number, 0, 1);
// if ($first_number == 0) {
// // Check if the first number is 0.
// // Get rid of the first number.
// $number = substr($number, 1, 999);
// }
// }
// if(strlen($number) === 9){
// $phone = '243' . $number;
// }elseif(strlen($number) === 12){
// $phone = $number;
// }
// $phoneNumbers[] = $phone;
// foreach ($phoneNumbers as $phoneNumber) {
// $destination = new Destination();
// $destination->setTo($phoneNumber);
// $destinations[] = $destination;
// }
// $message = new Message();
// $message->setFrom("Support Baadhi");
// $message->setDestinations($destinations);
// $message->setText($message);
// $requestBody = new SMSAdvancedTextualRequest();
// $requestBody->setMessages([$message]);
// // Executing response
// $this->smsClient->execute($requestBody);
$this->messageTemplate($agent);
}
return $this->render('auth/reset_sms_password/result.html.twig', [
"formSearch" => $formSearch->createView(),
"agent" => $agent
]);
}
/**
* @Route("/confirm/{slug}", name="reset_sms_password.confirm-code")
*/
public function confirmCode(Request $request, User $agent)
{
$searchForm = new CodeSmsPassword();
$confirmSearch = $this->createForm(CodeSmsPasswordType::class, $searchForm);
$confirmSearch->handleRequest($request);
if ($confirmSearch->isSubmitted() && $confirmSearch->isValid()) {
if(!empty($searchForm->getQuery())){
$check = $this->userRepository->findOneBySmsCode($agent->getSlug(),$searchForm->getQuery());
if($check){
return $this->redirectToRoute('reset_sms_password.new-password',["slug" => $agent->getSlug()]);
}else{
$this->addFlash("badCode","Votre code de confirmation ne correspond pas à votre compte");
return $this->redirectToRoute('reset_sms_password.confirm-code',["slug" => $agent->getSlug()]);
}
}
}
return $this->render('auth/reset_sms_password/confirm.html.twig', [
"confirmSearch" => $confirmSearch->createView(),
"agent" => $agent
]);
}
/**
* @Route("/{slug}/update-email", name="reset_sms_password.new-password")
*/
public function updateEmail(Request $request,User $user, UserPasswordEncoderInterface $passwordEncoder)
{
$form = $this->createForm(UpdatePasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user->setPassword(
$passwordEncoder->encodePassword(
$user,
$form->get('password')->getData()
)
);
$this->addFlash("resetPassword","Votre mot de passe a été modifié avec succès");
$this->entityManager->flush();
return $this->redirectToRoute('app.login');
}
return $this->render('auth/reset_sms_password/update-password.html.twig', [
'formUpdatePassword' => $form->createView(),
"user" => $user
]);
}
public function messageTemplate($agent){
if ($agent->getSexe() == "male") {
$sexe = "monsieur";
}else {
$sexe = "madame";
}
$msg = "Bonjour ".$sexe." ".$agent->getNames()." votre code de confirmation pour votre mot de passe est [ ". $agent->getAccountVerification()->getPhoneCode()." ]";
try {
$this->infoBip->sendSMS($agent->getPhoneNumber(), $msg);
} catch (\Throwable $th) {
//throw $th;
}
return $this;
}
}