<?php
namespace App\Controller;
use App\Entity\Appointment;
use App\Entity\Booking;
use App\Event\AppointmentEvent;
use App\Form\AppointmentType;
use App\Form\BookingType;
use Boab\CmsBundle\Controller\BaseController;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class AppointmentController extends BaseController
{
#[Route('/appointment/book', name: 'book_appointment')]
public function index(Request $request, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger): Response
{
$form = $this->createForm(AppointmentType::class, new Appointment,[
"action" => $this->router->generate('book_appointment'),
"method" => "POST"
]);
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid()){
$data = $form->getData();
try{
$this->entityManager->persist($data);
$this->entityManager->flush();
$this->eventDispatcher->dispatch(new AppointmentEvent($data), AppointmentEvent::SUMMITED);
$this->addFlash('success', 'Appointment has been submitted successfully');
}catch(\Exception $e){
dump($e);
$logger->error($e->getMessage(), ['exception'=>$e]);
$this->addFlash('danger', 'Something went wrong while processing submitting the appointment');
}
return $this->redirect($this->router->generate('book_appointment',['status'=>'success']));
}
return $this->render('booking/index.html.twig', [
'form' => $form->createView(),
'pageTitle' => 'Book Appointment'
]);
}
}