<?php
namespace App\Controller\Front;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Address;
use App\Entity\Booking\Booking;
use App\Form\Booking\BookingType;
use App\Service\BookingService;
use App\Repository\Course\PlanRepository;
use App\Repository\Article\ServiceRepository;
class BookingController extends AbstractController
{
public function booking(
Request $request,
BookingService $bookingService,
TranslatorInterface $translator,
MailerInterface $mailer,
PlanRepository $planRepository,
ServiceRepository $serviceRepository
): Response
{
$booking = new Booking();
$form = $this->createForm(BookingType::class, $booking);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$serviceId = $request->request->get('service-id');
$serviceTitle = $request->request->get('service-title');
if (null !== $serviceId) {
[$type, $id] = explode('-', $serviceId);
if ('plan' === $type) {
$plan = $planRepository->find($id);
if (null !== $plan) {
$booking->setPlan($plan);
}
}
if ('service' === $type) {
$service = $serviceRepository->find($id);
if (null !== $service) {
$booking->setService($service);
}
}
}
$bookingService->create($booking);
$templatedEmail = new TemplatedEmail();
$contactMail = $this->getParameter('contact_email');
$from = new Address($contactMail, 'Ranch Family Horse');
$subject = $translator->trans('app.booking.email.subject') . ' | Ranch Family Horse';
if (null !== $serviceTitle) {
$subject .= ' | ' . $serviceTitle;
}
$email = $templatedEmail->from($from)
->to($contactMail)
->subject($subject)
->htmlTemplate('front/booking/mail.html.twig')
->context(compact('booking', 'contactMail', 'serviceTitle'));
$mailer->send($email);
return $this->redirect($request->headers->get('referer'));
}
$bookingForm = $form->createView();
return $this->render('front/booking/form.html.twig', compact('bookingForm'));
}
}