src/Controller/Front/BookingController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Contracts\Translation\TranslatorInterface;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Mailer\MailerInterface;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\Mime\Address;
  10. use App\Entity\Booking\Booking;
  11. use App\Form\Booking\BookingType;
  12. use App\Service\BookingService;
  13. use App\Repository\Course\PlanRepository;
  14. use App\Repository\Article\ServiceRepository;
  15. class BookingController extends AbstractController
  16. {
  17.     public function booking(
  18.         Request $request,
  19.         BookingService $bookingService,
  20.         TranslatorInterface $translator,
  21.         MailerInterface $mailer,
  22.         PlanRepository $planRepository,
  23.         ServiceRepository $serviceRepository
  24.     ): Response
  25.     {
  26.         $booking = new Booking();
  27.         $form $this->createForm(BookingType::class, $booking);
  28.         $form->handleRequest($request);
  29.         if ($form->isSubmitted() && $form->isValid()) {
  30.             $serviceId $request->request->get('service-id');
  31.             $serviceTitle $request->request->get('service-title');
  32.             if (null !== $serviceId) {
  33.                 [$type$id] = explode('-'$serviceId);
  34.                 if ('plan' === $type) {
  35.                     $plan $planRepository->find($id);
  36.                     if (null !== $plan) {
  37.                         $booking->setPlan($plan);
  38.                     }
  39.                 }
  40.                 if ('service' === $type) {
  41.                     $service $serviceRepository->find($id);
  42.                     if (null !== $service) {
  43.                         $booking->setService($service);
  44.                     }
  45.                 }
  46.             }
  47.             $bookingService->create($booking);
  48.             $templatedEmail = new TemplatedEmail();
  49.             $contactMail $this->getParameter('contact_email');
  50.             $from = new Address($contactMail'Ranch Family Horse');
  51.             $subject $translator->trans('app.booking.email.subject') . ' | Ranch Family Horse';
  52.             if (null !== $serviceTitle) {
  53.                 $subject .= ' | ' $serviceTitle;
  54.             }
  55.             $email $templatedEmail->from($from)
  56.                 ->to($contactMail)
  57.                 ->subject($subject)
  58.                 ->htmlTemplate('front/booking/mail.html.twig')
  59.                 ->context(compact('booking''contactMail''serviceTitle'));
  60.             $mailer->send($email);
  61.             return $this->redirect($request->headers->get('referer'));
  62.         }
  63.         $bookingForm $form->createView();
  64.         return $this->render('front/booking/form.html.twig'compact('bookingForm'));
  65.     }
  66. }