src/Controller/Front/ReservationController.php line 60

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\CoachingSession;
  4. use App\Entity\CoursLangue;
  5. use App\Entity\Reservation;
  6. use App\Entity\User;
  7. use App\Form\ReservationType;
  8. use App\Services\EmailManager;
  9. use App\Services\ReservationManager;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class ReservationController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/reservation/{type}/{user}/{slug}", name="reservation")
  19.      * Method ({"GET", "POST"})
  20.      */
  21.     public function index(Request $requestReservationManager $reservationManagerEmailManager $emailManager$type$user $slug): Response
  22.     {
  23.         $em $this->getDoctrine()->getManager();
  24.         $user $em->getRepository(User::class)->findOneBySlug($user);
  25.         $cour $formation null;
  26.         if($type == 'cours-de-langue')
  27.             $cour $em->getRepository(CoursLangue::class)->findOneBySlug($slug);
  28.         else
  29.             $formation $em->getRepository(CoachingSession::class)->findOneBySlug($slug);
  30.         $reservation = new Reservation();
  31.         $form $this->createForm(ReservationType::class, $reservation, [
  32.             'action' => $this->generateUrl('reservation', [
  33.                 'type' => $type,
  34.                 'user' => $user->getSlug(),
  35.                 'slug' => $slug
  36.             ]),
  37.             'method' => 'POST',
  38.         ]);
  39.         $form->handleRequest($request);
  40.         if ($form->isSubmitted() && $form->isValid() && $request->isMethod('POST')) {
  41.             $reservation $reservationManager->addReservation($reservation$user$cour$formation);
  42.             $emailManager->sendEmailaddReservationToCoach($reservation$reservation->getCoach()->getUtilisateur());
  43.             $emailManager->sendEmailaddReservationToCandidate($reservation$user);
  44.             $em->persist($reservation);
  45.             $em->flush();
  46.             return new RedirectResponse($this->generateUrl('reservation_success', ['token' => $reservation->getToken()]));
  47.         }
  48.         return $this->render('front/reservation/index.html.twig', [
  49.             'formation' => $formation,
  50.             'cour' => $cour,
  51.             'user' => $user,
  52.             'form' => $form->createView()
  53.         ]);
  54.     }
  55.     /**
  56.      * @Route("/reservation-success/{token}", name="reservation_success")
  57.      * Method ({"GET", "POST"})
  58.      */
  59.     public function reservationSuccess($token): Response
  60.     {
  61.         $em $this->getDoctrine()->getManager();
  62.         $reservation $em->getRepository(Reservation::class)->findOneByToken($token);
  63.         return $this->render('front/reservation/reservation_success.html.twig', [
  64.             'cour' => $reservation->getCour(),
  65.             'formation' => $reservation->getFormation(),
  66.             'coach' => $reservation->getCoach()
  67.         ]);
  68.     }
  69. }