src/Controller/Front/VehicleController.php line 95

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Throwable;
  4. use App\Controller\PageController;
  5. use App\Form\VehicleType;
  6. use App\Model\Appointment;
  7. use App\Model\Client;
  8. use App\Model\Vehicle;
  9. use App\Repository\MenuRepository;
  10. use App\Service\AppointmentService;
  11. use App\Service\ValidatorService;
  12. use App\Service\VehicleService;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use Psr\Log\LoggerInterface;
  15. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Serializer\SerializerInterface;
  21. /**
  22.  * @Route("/vehicle", priority="1", options={"expose"=true})
  23.  */
  24. class VehicleController extends PageController
  25. {
  26.     use SecurityTokenTrait;
  27.     private VehicleService $vehicleService;
  28.     private LoggerInterface $logger;
  29.     public function __construct(ManagerRegistry $doctrineMenuRepository $menuRepositoryVehicleService $vehicleServiceLoggerInterface $logger)
  30.     {
  31.         parent::__construct($doctrine$menuRepository);
  32.         $this->vehicleService $vehicleService;
  33.         $this->logger $logger;
  34.     }
  35.     /**
  36.      * @Route("/all", name="all_vehicles")
  37.      */
  38.     public function vehicles(): JsonResponse
  39.     {
  40.         try {
  41.             $vehicles $this->vehicleService->all();
  42.         } catch (Throwable $exception) {
  43.             return $this->json([
  44.                 'message' => $exception->getMessage(),
  45.             ], Response::HTTP_INTERNAL_SERVER_ERROR);
  46.         }
  47.         return $this->json([
  48.             'cars' => $vehicles,
  49.         ]);
  50.     }
  51.     /**
  52.      * @Route("/delete/{id}", name="vehicle_delete")
  53.      *
  54.      * @return JsonResponse
  55.      */
  56.     public function delete(Request $requeststring $idAppointmentService $appointmentService): Response
  57.     {
  58.         $session $request->getSession();
  59.         /** @var Client $user */
  60.         $user $this->getUser();
  61.         $isUserConnected $user && !$session->get('gon');
  62.         $vehicles $appointmentService->deleteAppointmentVehicle($id$isUserConnected$session);
  63.         /** @var Appointment $appointment */
  64.         $appointment $session->get('appointment');
  65.         if ($appointment->getVehicle() && $appointment->getVehicle()->getId() == $id) {
  66.             $appointment->setVehicle(null);
  67.             $appointmentService->removeNextStep($appointment'vehicle');
  68.         }
  69.         return $this->json([
  70.             'success' => true,
  71.             'data' => [
  72.                 'html' => $this->renderView('olivier/elements/block_accordion_cars.html.twig', [
  73.                     'vehicles' => $vehicles,
  74.                 ]),
  75.             ],
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/{id}", name="appointment_vehicle", defaults={"id": null}, methods={"GET", "POST", "PUT"})
  80.      *
  81.      * @ParamConverter("selectedVehicle", class="App\Model\Vehicle")
  82.      */
  83.     public function vehicle(Request $request, ?Vehicle $selectedVehicleAppointmentService $appointmentServiceValidatorService $validatorVehicleService $vehicleServiceSerializerInterface $serializer): Response
  84.     {
  85.         $session $request->getSession();
  86.         /** @var Client $user */
  87.         $user $this->getUser();
  88.         $isUserConnected $user && !$session->get('gon');
  89.         $vehicles $vehicleService->getVehicles($isUserConnected$user$serializer$appointmentService$session);
  90.         $currentStep $request->query->get('currentStep'2);
  91.         $maxStep $request->query->get('maxStep'2);
  92.         $session->set('maxStep'$maxStep);
  93.         $form $this->createForm(VehicleType::class, $selectedVehicle);
  94.         $form->handleRequest($request);
  95.         if ($form->isSubmitted()) {
  96.             /*
  97.              * UPDATE/CREATE VEHICLE
  98.              */
  99.             if (!$form->isValid()) {
  100.                 $errors $validator->getErrors($form);
  101.                 $this->logger->error(json_encode($errorsJSON_THROW_ON_ERROR));
  102.                 return $this->json([
  103.                     'error' => $errors,
  104.                 ], Response::HTTP_INTERNAL_SERVER_ERROR);
  105.             }
  106.             $vehicleService->getOrderedVehicles($isUserConnected$selectedVehicle$vehicles);
  107.             if (!$isUserConnected) {
  108.                 $session->set('vehicles'$vehicles);
  109.             }
  110.             $appointmentService->needReinitVehicleStep($request->getSession(), $selectedVehicle->getId());
  111.             return $this->json([
  112.                 'success' => true,
  113.                 'data' => [
  114.                     'html' => $this->renderView('olivier/elements/block_accordion_cars.html.twig', [
  115.                         'vehicles' => $vehicles,
  116.                     ]),
  117.                 ],
  118.             ]);
  119.         }
  120.         /** @var Appointment $appointment */
  121.         $appointment $session->get('appointment');
  122.         $selectedVehicle null;
  123.         // Highlight vehicle by default if it already exists (ex of case: click on modify vehicle in resume step)
  124.         if ($appointment->getVehicle()) {
  125.             $appointmentService->addToVehicles($vehicles$appointment->getVehicle());
  126.             $selectedVehicle $appointment->getVehicle();
  127.             $session->set('vehicles'$vehicles);
  128.         }
  129.         return $this->json([
  130.             'success' => true,
  131.             'data' => [
  132.                 'html' => $this->renderView('front/appointment/vehicle.html.twig', [
  133.                     'maxStep' => $maxStep,
  134.                     'currentStep' => $currentStep,
  135.                     'vehicleForm' => $form->createView(),
  136.                     'vehicles' => $vehicles,
  137.                     'selectedVehicle' => $selectedVehicle,
  138.                     'appointment' => $appointment
  139.                 ]),
  140.             ],
  141.         ]);
  142.     }
  143. }