<?php
namespace App\Controller\Marketplace;
use App\Entity\Specialist;
use App\Form\SpecialistLightType;
use App\Repository\SpecialistRepository;
use App\Security\SpecialistAuthenticator;
use App\Service\EmailService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Uid\Uuid;
/**
* @Route("/", name="app_expert_")
*/
class ExpertController extends AbstractController
{
/**
* @var emailService
*/
private $emailService;
/**
* @var UrlGeneratorInterface
*/
private $urlGenerator;
public function __construct(EmailService $emailService, UrlGeneratorInterface $urlGenerator)
{
$this->emailService = $emailService;
$this->urlGenerator = $urlGenerator;
}
/**
* @Route("devenir-expert", name="signup")
*/
public function index(Request $request, EntityManagerInterface $entityManager, SpecialistRepository $specialistRepo): Response
{
$success = false;
if (!empty($request->query->get('signup'))) {
$success = true;
}
$specialist = new Specialist();
$options = [
'pwd_required' => true,
'specialist_reg' => true,
];
$form = $this->createForm(SpecialistLightType::class, $specialist, $options);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$existingSpecialist = $specialistRepo->findOneBy(
[
'tel' => $specialist->getTel(),
]
);
if ($existingSpecialist == null) {
$validUuid = Uuid::v1();
$specialist->setIsVirtualEvent(true);
$specialist->getUser()->setActive(true);
$specialist->setSpecialistTags([]);
$specialist->setUuid($validUuid);
$specialist->setStatus(Specialist::STATUS_WAITING_MAIL_CONFIRMATION);
try {
$entityManager->persist($specialist);
$entityManager->flush();
$entityManager->refresh($specialist);
$this->emailService->sendSpecialistAccountConfirmation($specialist);
$this->addFlash('success', 'Un mail de confirmation vient de vous être envoyé. Afin de finaliser votre inscription veuillez cliquer sur le lien présent dans ce mail. A très vite sur ULTEAM pour découvrir nos opportunités');
return $this->redirectToRoute('marketplace_app_expert_signup', ['signup' => "success"]);
// return $this->generateUrl(SpecialistAuthenticator::LOGIN_ROUTE);
// return $this->redirect($this->getParameter('ULTEAM_LIVE_HOST').$this->generateUrl(SpecialistAuthenticator::LOGIN_ROUTE));
}catch (\Exception $e){
$this->addFlash('errorGlobal', 'Une erreur est survenue');
// return $this->redirectToRoute(SpecialistAuthenticator::LOGIN_ROUTE);
}
}elseif ($existingSpecialist != null && $existingSpecialist->getStatus() == Specialist::STATUS_REFUSED) {
$this->addFlash('errorGlobal', 'Désolé mais la connexion à ton compte n\'est pas possible actuellement');
// return $this->redirectToRoute(SpecialistAuthenticator::LOGIN_ROUTE);
}else {
$this->addFlash('errorGlobal', 'Un expert avec ce numero de téléphone existe déjà');
// return $this->redirectToRoute(SpecialistAuthenticator::LOGIN_ROUTE);
}
}
return $this->render('marketplace/expert/index.html.twig', [
'form' => $form->createView(),
'success' => $success
]);
}
}