vendor/croisiland/common-bundle/src/Twig/CruiseExtension.php line 122

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Croisiland\CommonBundle\Twig;
  4. use Crea\MediaBundle\Entity\MediaTagOwnerInterface;
  5. use Croisiland\CommonBundle\Constants\Cruise\CruiseConstants;
  6. use Croisiland\CommonBundle\Entity\Cabin\CabinCategory;
  7. use Croisiland\CommonBundle\Entity\Company\Atmosphere;
  8. use Croisiland\CommonBundle\Entity\Cruise\Cruise;
  9. use Croisiland\CommonBundle\Entity\Cruise\CruiseInterface;
  10. use Croisiland\CommonBundle\Entity\Itinerary\ItineraryActivity;
  11. use Croisiland\CommonBundle\Entity\Itinerary\ItineraryPartInterface;
  12. use Croisiland\CommonBundle\Entity\Itinerary\ItinerarySegment;
  13. use Croisiland\CommonBundle\Entity\Offer\Offer;
  14. use Croisiland\CommonBundle\Helper\Cruise\CruiseHelper;
  15. use Croisiland\CommonBundle\Helper\Itinerary\ItinerarySummaryHelper;
  16. use Croisiland\CommonBundle\Helper\Price\PriceHelper;
  17. use Croisiland\CommonBundle\Repository\Cabin\CabinCategoryRepository;
  18. use Croisiland\CommonBundle\Repository\Cruise\CruiseRepository;
  19. use Croisiland\CommonBundle\Repository\Itinerary\StopoverRepository;
  20. use Croisiland\CommonBundle\Repository\Offer\OfferRepository;
  21. use DateMalformedStringException;
  22. use DateTime;
  23. use Doctrine\ORM\NonUniqueResultException;
  24. use Doctrine\ORM\NoResultException;
  25. use Twig\Extension\AbstractExtension;
  26. use Twig\TwigFunction;
  27. class CruiseExtension extends AbstractExtension
  28. {
  29. public function __construct(
  30. public readonly PriceHelper $priceHelper,
  31. public readonly StopoverRepository $stopoverRepository,
  32. private readonly OfferRepository $offerRepository,
  33. private readonly CabinCategoryRepository $cabinCategoryRepository,
  34. private readonly ItinerarySummaryHelper $itinerarySummaryHelper,
  35. private readonly CruiseHelper $cruiseHelper,
  36. ) {
  37. }
  38. public function getFunctions(): array
  39. {
  40. return [
  41. new TwigFunction('getCruiseBestPrice', $this->getCruiseBestPrice(...)),
  42. new TwigFunction('getCruiseOffers', $this->getCruiseOffers(...)),
  43. new TwigFunction('getItineraryPartDateForCruise', $this->getItineraryPartDateForCruise(...)),
  44. new TwigFunction('getCabinCategoriesForCruise', $this->getCabinCategoriesForCruise(...)),
  45. new TwigFunction('getSegmentActivities', $this->getSegmentActivities(...)),
  46. new TwigFunction('getCruiseAvatarOwner', $this->getCruiseAvatarOwner(...)),
  47. new TwigFunction('getCruiseName', $this->getCruiseName(...)),
  48. new TwigFunction('getCruiseIncludeForAtmosphereArray', $this->getCruiseIncludeForAtmosphereArray(...)),
  49. new TwigFunction('getCruiseExcludeForAtmosphereArray', $this->getCruiseExcludeForAtmosphereArray(...)),
  50. ];
  51. }
  52. /**
  53. * @throws NonUniqueResultException
  54. * @throws NoResultException
  55. */
  56. public function getCruiseBestPrice(Cruise $cruise): ?int
  57. {
  58. return $this->priceHelper->getBestPriceForCruise($cruise);
  59. }
  60. /**
  61. * @return Offer[]
  62. * @throws DateMalformedStringException
  63. */
  64. public function getCruiseOffers(Cruise $cruise): array
  65. {
  66. return $this->offerRepository->findValidOffersForCruise($cruise, CruiseConstants::DATETIME_INTERVAL_NEXT_DEPARTURES);
  67. }
  68. public function getItineraryPartDateForCruise(ItineraryPartInterface $itineraryPart, Cruise $cruise): ?\DateTimeInterface
  69. {
  70. $dateDelta = $itineraryPart->getDateDelta();
  71. if ($dateDelta < 1) {
  72. return $cruise->getDepartureDate();
  73. }
  74. $interval = new \DateInterval("P{$dateDelta}D");
  75. $date = clone $cruise->getDepartureDate();
  76. /** @var Datetime $date */
  77. $date->add($interval);
  78. return $date;
  79. }
  80. /**
  81. * @return CabinCategory[]
  82. */
  83. public function getCabinCategoriesForCruise(Cruise $cruise): array
  84. {
  85. return $this->cabinCategoryRepository->getCabinCategoriesForCruise($cruise);
  86. }
  87. /**
  88. * @return ItineraryActivity[]
  89. */
  90. public function getSegmentActivities(ItinerarySegment $segment): array
  91. {
  92. return $this->itinerarySummaryHelper->getSegmentActivities($segment);
  93. }
  94. public function getCruiseAvatarOwner(CruiseInterface $cruise): ?MediaTagOwnerInterface
  95. {
  96. return $this->cruiseHelper->getCruiseAvatarOwner($cruise);
  97. }
  98. public function getCruiseName(Cruise $cruise): ?string
  99. {
  100. return $this->cruiseHelper->getCruiseDisplayName($cruise);
  101. }
  102. /**
  103. * @return array<string>>
  104. */
  105. public function getCruiseIncludeForAtmosphereArray(Cruise $cruise, ?Atmosphere $atmosphere=null): array
  106. {
  107. return $this->cruiseHelper->getCruiseIncludeForAtmosphereArray($cruise, $atmosphere);
  108. }
  109. /**
  110. * @return array<string>>
  111. */
  112. public function getCruiseExcludeForAtmosphereArray(Cruise $cruise, ?Atmosphere $atmosphere=null): array
  113. {
  114. return $this->cruiseHelper->getCruiseExcludeForAtmosphereArray($cruise, $atmosphere);
  115. }
  116. }