<?php
namespace App\Controller\Front;
use App\Entity\CoachingSession;
use App\Entity\CoursLangue;
use App\Entity\Reservation;
use App\Entity\User;
use App\Form\ReservationType;
use App\Services\EmailManager;
use App\Services\ReservationManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ReservationController extends AbstractController
{
/**
* @Route("/reservation/{type}/{user}/{slug}", name="reservation")
* Method ({"GET", "POST"})
*/
public function index(Request $request, ReservationManager $reservationManager, EmailManager $emailManager, $type, $user , $slug): Response
{
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository(User::class)->findOneBySlug($user);
$cour = $formation = null;
if($type == 'cours-de-langue')
$cour = $em->getRepository(CoursLangue::class)->findOneBySlug($slug);
else
$formation = $em->getRepository(CoachingSession::class)->findOneBySlug($slug);
$reservation = new Reservation();
$form = $this->createForm(ReservationType::class, $reservation, [
'action' => $this->generateUrl('reservation', [
'type' => $type,
'user' => $user->getSlug(),
'slug' => $slug
]),
'method' => 'POST',
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && $request->isMethod('POST')) {
$reservation = $reservationManager->addReservation($reservation, $user, $cour, $formation);
$emailManager->sendEmailaddReservationToCoach($reservation, $reservation->getCoach()->getUtilisateur());
$emailManager->sendEmailaddReservationToCandidate($reservation, $user);
$em->persist($reservation);
$em->flush();
return new RedirectResponse($this->generateUrl('reservation_success', ['token' => $reservation->getToken()]));
}
return $this->render('front/reservation/index.html.twig', [
'formation' => $formation,
'cour' => $cour,
'user' => $user,
'form' => $form->createView()
]);
}
/**
* @Route("/reservation-success/{token}", name="reservation_success")
* Method ({"GET", "POST"})
*/
public function reservationSuccess($token): Response
{
$em = $this->getDoctrine()->getManager();
$reservation = $em->getRepository(Reservation::class)->findOneByToken($token);
return $this->render('front/reservation/reservation_success.html.twig', [
'cour' => $reservation->getCour(),
'formation' => $reservation->getFormation(),
'coach' => $reservation->getCoach()
]);
}
}