src/Service/SeasonService.php line 28

  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Season;
  4. use App\Repository\SeasonRepository;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class SeasonService
  7. {
  8.     private const SESSION_KEY 'selected_season_id';
  9.     public function __construct(
  10.         private SeasonRepository $seasonRepository,
  11.         private RequestStack $requestStack
  12.     ) {
  13.     }
  14.     /**
  15.      * Vrátí aktuálně vybranou sezónu ze session, nebo aktivní sezónu (isCurrent = true)
  16.      */
  17.     public function getSelectedSeason(): ?Season
  18.     {
  19.         // V CLI kontextu (např. watchdog command) není request ani session dostupná
  20.         $request $this->requestStack->getMainRequest();
  21.         if ($request && $request->hasSession()) {
  22.             $session $request->getSession();
  23.             $selectedSeasonId $session->get(self::SESSION_KEY);
  24.             if ($selectedSeasonId) {
  25.                 $season $this->seasonRepository->find($selectedSeasonId);
  26.                 if ($season instanceof Season) {
  27.                     return $season;
  28.                 }
  29.             }
  30.         }
  31.         // Pokud není v session nebo session není dostupná, vrátíme aktivní sezónu
  32.         return $this->seasonRepository->findOneBy(['isCurrent' => true]);
  33.     }
  34.     /**
  35.      * Uloží vybranou sezónu do session
  36.      */
  37.     public function setSelectedSeason(int $seasonId): void
  38.     {
  39.         // V CLI kontextu (např. watchdog command) není request ani session dostupná
  40.         $request $this->requestStack->getMainRequest();
  41.         if ($request && $request->hasSession()) {
  42.             $session $request->getSession();
  43.             $session->set(self::SESSION_KEY$seasonId);
  44.         }
  45.     }
  46.     /**
  47.      * Vrátí všechny sezóny seřazené podle roku (nejnovější první)
  48.      */
  49.     public function getAllSeasons(): array
  50.     {
  51.         return $this->seasonRepository->findBy([], ['year' => 'DESC']);
  52.     }
  53. }