src/Controller/Website/DetailController.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Website;
  4. use App\Repository\Cruise\CruiseRepository;
  5. use App\Repository\Price\PriceRepository;
  6. use Croisiland\CommonBundle\Constants\Cruise\CruiseConstants;
  7. use Croisiland\CommonBundle\Entity\Cruise\Cruise;
  8. use DateMalformedStringException;
  9. use Doctrine\ORM\NonUniqueResultException;
  10. use Doctrine\ORM\NoResultException;
  11. use Sulu\Component\Content\Compat\StructureInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class DetailController extends BaseWebsiteController
  16. {
  17. const string BASE_PATH = '/croisieres/details';
  18. public function __construct(
  19. private readonly CruiseRepository $cruiseRepository,
  20. private readonly PriceRepository $priceRepository,
  21. ) {
  22. }
  23. /**
  24. * @throws DateMalformedStringException
  25. * @throws NonUniqueResultException
  26. * @throws NoResultException
  27. */
  28. public function indexAction(StructureInterface $structure, $preview = false, $partial = false): Response
  29. {
  30. $cruise = $this->resolveCruise();
  31. // TODO check if partner has access to the cruise with :
  32. // $this->>partnerHelper->currentPartnerHasAccessTo($cruise)
  33. // redirect to a 403 page
  34. $upComingCruiseIds = $this->cruiseRepository->getUpComingCruiseIds($cruise->getItinerary(), CruiseConstants::DATETIME_INTERVAL_NEXT_DEPARTURES);
  35. $bestUpComingPrice = $this->priceRepository->findBestPriceForCruiseIds($upComingCruiseIds);
  36. $attributes = array_merge($this->getSharedLayoutFormViews(), [
  37. 'cruise' => $cruise,
  38. 'bestUpComingPrice' => $bestUpComingPrice,
  39. ]);
  40. return $this->renderStructure(
  41. $structure,
  42. $attributes,
  43. $preview,
  44. $partial
  45. );
  46. }
  47. /**
  48. * Fragment HTML du bloc tarifs (vue tableau ou vue détaillée).
  49. *
  50. * @throws DateMalformedStringException
  51. * @throws NonUniqueResultException
  52. * @throws NoResultException
  53. */
  54. #[Route('/croisieres/details/fragments/pricing', name: 'website_detail_pricing_fragment', methods: ['GET'])]
  55. public function pricingFragmentAction(Request $request): Response
  56. {
  57. $cruiseId = $request->query->getInt('croisiere', 0);
  58. $view = $request->query->get('view', 'table');
  59. if ($cruiseId <= 0 || !in_array($view, ['table', 'detailed'], true)) {
  60. return new Response('Invalid parameters', Response::HTTP_BAD_REQUEST);
  61. }
  62. $cruise = $this->cruiseRepository->find($cruiseId);
  63. if (!$cruise instanceof Cruise) {
  64. return new Response('Cruise not found', Response::HTTP_NOT_FOUND);
  65. }
  66. $upComingCruiseIds = $this->cruiseRepository->getUpComingCruiseIds(
  67. $cruise->getItinerary(),
  68. CruiseConstants::DATETIME_INTERVAL_NEXT_DEPARTURES
  69. );
  70. $bestUpComingPrice = $this->priceRepository->findBestPriceForCruiseIds($upComingCruiseIds);
  71. return $this->render('pages/_includes/details/_other-departures-pricing-fragment.html.twig', [
  72. 'view' => $view,
  73. 'currentCruise' => $cruise,
  74. 'bestUpComingPrice' => $bestUpComingPrice,
  75. ]);
  76. }
  77. /**
  78. * Charge la croisière courante depuis la querystring `?croisiere=<id>`
  79. * (cohérent avec le lien généré par `_snippets/components/cards/list_card.html.twig`).
  80. *
  81. * Retourne `null` si :
  82. * - la requête n'est pas disponible (rendu hors HTTP, preview, etc.),
  83. * - aucun id n'est fourni,
  84. * - aucune croisière ne correspond à l'id.
  85. *
  86. * Le template doit donc se montrer défensif sur `cruise` (ce que fait `list_card`).
  87. */
  88. private function resolveCruise(): ?Cruise
  89. {
  90. $request = $this->container->get('request_stack')->getCurrentRequest();
  91. if ($request === null) {
  92. return null;
  93. }
  94. $id = $request->query->getInt('croisiere', 0);
  95. if ($id <= 0) {
  96. return null;
  97. }
  98. return $this->cruiseRepository->find($id);
  99. }
  100. }