<?php
declare(strict_types=1);
namespace App\Twig\Cruise;
use App\Helper\Cruise\CruiseHelper;
use App\Repository\Cruise\CruiseRepository;
use Crea\MediaBundle\Entity\MediaFile;
use Croisiland\CommonBundle\Constants\Cruise\CruiseConstants;
use Croisiland\CommonBundle\Entity\Cruise\Cruise;
use Croisiland\CommonBundle\Entity\Itinerary\Itinerary;
use DateMalformedStringException;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Random\RandomException;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class CruiseExtension extends AbstractExtension
{
/** @var array<int, Cruise|null> */
private array $cacheById = [];
public function __construct(
private readonly CruiseRepository $cruiseRepository,
private readonly CruiseHelper $cruiseHelper,
) {
}
/**
* @return TwigFunction[]
*/
public function getFunctions(): array
{
return [
new TwigFunction('getCruiseById', $this->getCruiseById(...)),
new TwigFunction('getUpcomingCruises', $this->getUpcomingCruises(...)),
new TwigFunction('searchCruises', $this->searchCruises(...)),
new TwigFunction('getCruiseSearchUrl', $this->getCruiseSearchUrl(...)),
];
}
public function getCruiseById(int $id): ?Cruise
{
if (!array_key_exists($id, $this->cacheById)) {
$this->cacheById[$id] = $this->cruiseRepository->findForDdd($id);
}
return $this->cacheById[$id];
}
/**
* @throws DateMalformedStringException
*/
public function getUpcomingCruises(Itinerary $itinerary): Collection
{
return $this->cruiseRepository->getUpcomingCruises($itinerary, CruiseConstants::DATETIME_INTERVAL_NEXT_DEPARTURES);
}
/**
* @param array<object> $searchElements available objects : Companies, Ship, Tags, Partner,
* @param int $limit
* @param bool $random
* @return Cruise[]
*
* @throws NoResultException
* @throws NonUniqueResultException
*/
private function searchCruises(array $searchElements, int $limit = 5, bool $random = false): array
{
return $this->cruiseHelper->searchCruises($searchElements, $limit, $random);
}
/**
* @param array<object> $searchElements available objects : Companies, Ship, Tags, Partner,
* @param string|null $locale Locale. Si null, utilise la locale actuelle.
* @param string|null $webspaceKey Clé du webspace. Si null, utilise le webspace actuel.
* @param string|null $domain Domaine personnalisé. Si null, utilise le domaine actuel.
* @param string|null $scheme Schéma (ex: 'https'). Si null, utilise le schéma actuel.
* @param bool $withoutDomain Si true, retourne une URL relative (sans domaine).
*
* @return string URL générée avec paramètres GET.
*/
private function getCruiseSearchUrl(array $searchElements,
?string $locale = null,
?string $webspaceKey = null,
?string $domain = null,
?string $scheme = null,
bool $withoutDomain = true): string
{
return $this->cruiseHelper->getCruiseSearchUrl($searchElements, $locale, $webspaceKey, $domain, $scheme, $withoutDomain);
}
}