<?php
namespace App\Controller\Front;
use Stripe\Stripe;
use App\Service\BrevoService;
use App\Repository\OfferRepository;
use App\Service\InternationalService;
use App\Repository\WebsiteInfoRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[Route(path: '{_locale}')]
class ProRegistrationController extends AbstractController
{
private $apiKey;
private $stripe;
private $request;
private $internationalService;
public function __construct(RequestStack $requestStack, InternationalService $internationalService, private BrevoService $brevoService)
{
$this->apiKey = $_ENV['STRIPE_SECRET_KEY'];
$this->request = $requestStack->getCurrentRequest();
$this->stripe = $this->stripe = new \Stripe\StripeClient($this->apiKey);
$this->internationalService = $internationalService;
}
private function generateString(int $length = 64)
{
$length = ($length < 4) ? 4 : $length;
return bin2hex(random_bytes(($length - ($length % 2)) / 2));
}
#[Route(path: '/membership-presentation', name: 'membership_presentation')]
public function membershipPresentation(OfferRepository $offerRepo, WebsiteInfoRepository $websiteInfoRepository, Request $request)
{
$locale = ($request->getLocale() ? $request->getLocale() : 'en');
$monthlyOffers = $offerRepo->findBy(['billing' => 'monthly']);
$yearlyOffers = $offerRepo->findBy(['billing' => 'yearly']);
$websiteinfos = $websiteInfoRepository->getWebsiteInfo($locale);
$standard[] = $monthlyOffers[0];
$standard[] = $yearlyOffers[0];
$vip[] = $vipPlus[] = $monthlyOffers[1];
$vip[] = $vipPlus[] = $yearlyOffers[1];
if (isset($_POST['type'])) {
$type = $_POST['type'];
$routeName = $type . '_register';
return $this->redirectToRoute($routeName);
}
return $this->render('/front/pro/membership-presentation.html.twig', [
'monthlyOffers' => $monthlyOffers,
'yearlyOffers' => $yearlyOffers,
'standard' => $standard,
'vip' => $vip,
'vipPlus' => $vipPlus,
'sportsCount' => 0,
'free' => false,
'websiteinfos' => $websiteinfos
]);
}
/*
#[Route(path: '/registration-informations', name: 'pro_registration_informations')]
public function proRegistrationInformations(Request $request, EntityManagerInterface $manager, RoleRepository $roleRepository, UserPasswordHasherInterface $passwordHasher, ToPro $toPro, UserRepository $userRepository)
{
$user = $this->getUser();
if ($user != null) {
return $this->redirectToRoute('homepage');
}
$timeZone = $this->internationalService->findVisitorTimeZone();
$session = $request->getSession();
$proType = $session->get('registrationProType');
$email = $session->get('registrationEmail');
$password = $session->get('registrationPassword');
$phone = $session->get('registrationPhone');
if ($proType == null || $email == null || $password == null || $phone == null) {
return $this->redirectToRoute('pro_registration_type');
}
if ($proType == 'coach') {
$pro = new Coach();
$form = $this->createForm(CoachRegisterType::class, $pro);
}
if ($proType == 'club') {
$pro = new Club();
$form = $this->createForm(ClubRegisterType::class, $pro);
}
if ($proType == 'operator') {
$pro = new Operator();
$form = $this->createForm(OperatorRegisterType::class, $pro);
}
$form->handleRequest($request);
$slugger = new AsciiSlugger();
if ($form->isSubmitted() && $form->isValid()) {
$hashed = $passwordHasher->hashPassword($pro, $password);
$proInfo = $pro->getProInfo();
$proInfo->setFullname($pro->getFirstname() . ' ' . $pro->getLastname())
->setPageVisible(false)
->setCoverPhoto('cover.png')
->setHaveSubscription(false)
->setContactEmail($email);
$sports = $proInfo->getSports();
foreach ($sports as $sport) {
$sport->addPro($proInfo);
$manager->persist($sport);
}
$slug = strtolower($slugger->slug($pro->getFirstname() . '-' . $pro->getLastname() . '-' . rand(10000, 98765)));
$role = $roleRepository->findOneBy(['title' => 'ROLE_PRO']);
$pro->setRegisteredAt(new DateTime())
->setToken($this->generateString(32))
->setTimezone($timeZone)
->addUserRole($role)
->setIsPro(true)
->setPhoto('default-profile.png')
->setSlug($slug)
->setNotification(true)
->setActivated(false)
->setHasAccess(false)
->setPassword($hashed)
->setStatus('online')
->setLanguage($this->request->getLocale());
$coachInfo = new CoachInfo();
$coachInfo->setOwner($pro)
->setName($proInfo->getFullname())
->setAddress($pro->getAddress());
try {
$manager->persist($proInfo);
$manager->persist($pro);
$manager->persist($coachInfo);
$manager->flush();
} catch (\Throwable $th) {
$this->addFlash(
'success',
"Une erreur s'est produite. Veillez réessayer"
);
return $this->redirectToRoute('coach_register');
}
if ($pro->getNewsletter()) {
$this->brevoService->createBrevoContact($pro->getFirstname(), $pro->getLastname(), $pro->getEmail(), 'pro');
}
$toPro->confirmEmail($pro);
$this->addFlash(
'registration',
""
);
return $this->redirectToRoute('homepage');
}
return $this->render('front/pro/registration/coach-registration.html.twig', [
'form' => $form->createView(),
'type' => 'coach'
]);
}
#[Route(path: '/coach-registration', name: 'coach_register')]
public function coachRegistration(Request $request, EntityManagerInterface $manager, RoleRepository $roleRepository, UserPasswordHasherInterface $passwordHasher, ToPro $toPro, UserRepository $userRepository)
{
$user = $this->getUser();
if ($user != null) {
return $this->redirectToRoute('homepage');
}
$timeZone = $this->internationalService->findVisitorTimeZone();
$coach = new Coach();
$slugger = new AsciiSlugger();
$form = $this->createForm(CoachRegisterType::class, $coach);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($userRepository->findOneBy(['email' => $coach->getEmail()])) {
$form->get('email')->addError(new FormError("Un compte existe avec cet email"));
} else {
$password = $coach->getPassword();
$hashed = $passwordHasher->hashPassword($coach, $password);
$proInfo = $coach->getProInfo();
$proInfo->setFullname($coach->getFirstname() . ' ' . $coach->getLastname())
->setPageVisible(false)
->setCoverPhoto('cover.png')
->setHaveSubscription(false)
->setContactEmail($coach->getEmail());
$sports = $proInfo->getSports();
foreach ($sports as $sport) {
$sport->addPro($proInfo);
$manager->persist($sport);
}
$slug = strtolower($slugger->slug($coach->getFirstname() . '-' . $coach->getLastname() . '-' . rand(10000, 98765)));
$role = $roleRepository->findOneBy(['title' => 'ROLE_PRO']);
$coach->setRegisteredAt(new DateTime())
->setToken($this->generateString(32))
->setTimezone($timeZone)
->addUserRole($role)
->setIsPro(true)
->setPhoto('default-profile.png')
->setSlug($slug)
->setNotification(true)
->setActivated(false)
->setHasAccess(false)
->setPassword($hashed)
->setStatus('online')
->setLanguage($this->request->getLocale());
$coachInfo = new CoachInfo();
$coachInfo->setOwner($coach)
->setName($proInfo->getFullname())
->setAddress($coach->getAddress());
try {
$manager->persist($proInfo);
$manager->persist($coach);
$manager->persist($coachInfo);
$manager->flush();
} catch (\Throwable $th) {
$this->addFlash(
'success',
"Une erreur s'est produite. Veillez réessayer"
);
return $this->redirectToRoute('coach_register');
}
if ($coach->getNewsletter()) {
$this->brevoService->createBrevoContact($coach->getFirstname(), $coach->getLastname(), $coach->getEmail(), 'pro');
}
$toPro->confirmEmail($coach);
$this->addFlash(
'registration',
""
);
return $this->redirectToRoute('homepage');
}
}
return $this->render('front/pro/registration/coach-registration.html.twig', [
'form' => $form->createView(),
'type' => 'coach'
]);
}
#[Route(path: '/club-registration', name: 'club_register')]
public function clubRegister(Request $request, EntityManagerInterface $manager, RoleRepository $roleRepository, UserPasswordHasherInterface $passwordHasher, ToPro $toPro, UserRepository $userRepository)
{
$user = $this->getUser();
if ($user != null) {
return $this->redirectToRoute('homepage');
}
$timeZone = $this->internationalService->findVisitorTime();
$club = new Club();
$slugger = new AsciiSlugger();
$form = $this->createForm(ClubRegisterType::class, $club);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($userRepository->findOneBy(['email' => $club->getEmail()])) {
$form->get('email')->addError(new FormError("Un compte existe avec cet email"));
} else {
$password = $club->getPassword();
$hashed = $passwordHasher->hashPassword($club, $password);
$proInfo = $club->getProInfo();
$proInfo->setFullname($club->getClubInfo()->getName())
->setPageVisible(false)
->setCoverPhoto('cover.png')
->setContactEmail($club->getEmail());
$sports = $proInfo->getSports();
foreach ($sports as $sport) {
$sport->addPro($proInfo);
$availability = new Availability();
$availability->setSport($sport)
->setClub($club);
$manager->persist($availability);
$manager->persist($club);
$manager->persist($sport);
}
$slug = strtolower($slugger->slug($club->getClubInfo()->getName() . '-' . rand(10000, 98765)));
$role = $roleRepository->findOneBy(['title' => 'ROLE_PRO']);
$club->setRegisteredAt(new DateTime())
->setToken($this->generateString(32))
->setIsPro(true)
->addUserRole($role)
->setTimezone($timeZone)
->setPhoto('default-profile.png')
->setSlug($slug)
->setNotification(true)
->setActivated(false)
->setHasAccess(false)
->setPassword($hashed)
->setStatus('online')
->setLanguage($this->request->getLocale());
$clubInfo = $club->getClubInfo();
$clubInfo->setOwner($club)
->setName($proInfo->getFullname())
->setAddress($club->getAddress());
try {
$manager->persist($proInfo);
$manager->persist($club);
$manager->persist($clubInfo);
$manager->flush();
} catch (\Throwable $th) {
$this->addFlash(
'success',
"Une erreur s'est produite. Veillez réessayer"
);
return $this->redirectToRoute('club_register');
}
if ($club->getNewsletter()) {
$this->brevoService->createBrevoContact($club->getFirstname(), $club->getLastname(), $club->getEmail(), 'pro');
}
$toPro->confirmEmail($club);
$this->addFlash(
'registration',
""
);
return $this->redirectToRoute('homepage');
}
}
return $this->render('front/pro/registration/club-registration.html.twig', [
'form' => $form->createView(),
'type' => 'club'
]);
}
#[Route(path: '/operator-registration', name: 'operator_register')]
public function operatorRegister(Request $request, EntityManagerInterface $manager, RoleRepository $roleRepository, UserPasswordHasherInterface $passwordHasher, ToPro $toPro, UserRepository $userRepository)
{
$user = $this->getUser();
if ($user != null) {
return $this->redirectToRoute('homepage');
}
$timeZone = $this->internationalService->findVisitorTimeZone();
$operator = new Operator();
$slugger = new AsciiSlugger();
$form = $this->createForm(OperatorRegisterType::class, $operator);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($userRepository->findOneBy(['email' => $operator->getEmail()])) {
$form->get('email')->addError(new FormError("Un compte existe avec cet email"));
} else {
$password = $operator->getPassword();
$hashed = $passwordHasher->hashPassword($operator, $password);
$proInfo = $operator->getProInfo();
$proInfo->setFullname($operator->getOperatorInfo()->getName())
->setPageVisible(false)
->setCoverPhoto('cover.png')
->setContactEmail($operator->getEmail());
$sports = $proInfo->getSports();
foreach ($sports as $sport) {
$sport->addPro($proInfo);
$manager->persist($sport);
}
$slug = strtolower($slugger->slug($operator->getOperatorInfo()->getName() . '-' . rand(10000, 98765)));
$role = $roleRepository->findOneBy(['title' => 'ROLE_PRO']);
$operator->setRegisteredAt(new DateTime())
->setToken($this->generateString(32))
->addUserRole($role)
->setIsPro(true)
->setSlug($slug)
->setPhoto('default-profile.png')
->setTimezone($timeZone)
->setNotification(true)
->setActivated(false)
->setPassword($hashed)
->setHasAccess(false)
->setStatus('online')
->setLanguage($this->request->getLocale());
$operatorInfo = $operator->getOperatorInfo();
$operatorInfo->setOwner($operator);
$proInfo->setFullname($operatorInfo->getName())
->setContactEmail($operator->getEmail());
try {
$manager->persist($proInfo);
$manager->persist($operator);
$manager->persist($operatorInfo);
$manager->flush();
} catch (\Throwable $th) {
$this->addFlash(
'success',
"Une erreur s'est produite. Veillez réessayer"
);
return $this->redirectToRoute('operator_register');
}
if ($operator->getNewsletter()) {
$this->brevoService->createBrevoContact($operator->getFirstname(), $operator->getLastname(), $operator->getEmail(), 'pro');
}
$toPro->confirmEmail($operator);
$this->addFlash(
'registration',
""
);
return $this->redirectToRoute('homepage');
}
}
return $this->render('front/pro/registration/operator-registration.html.twig', [
'form' => $form->createView(),
'type' => 'operator'
]);
}
*/
}