src/Service/CueList.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Entity\Sound;
  4. use App\Repository\SoundRepository;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. class CueList
  7. {
  8.     public function __construct(
  9.         private readonly RequestStack $requestStack,
  10.         private readonly SoundRepository $soundRepository
  11.     ) {
  12.     }
  13.     public function getCurrentSound(): ?Sound
  14.     {
  15.         return count($this->getCueList()) ?
  16.             $this->soundRepository->find($this->getCueList()[$this->getCurrentIndex()]) :
  17.             null;
  18.     }
  19.     public function getCueList(): array
  20.     {
  21.         return $this->requestStack->getSession()->get('cueList') ?? [];
  22.     }
  23.     public function setCueList(array $cueList): void
  24.     {
  25.         $this->requestStack->getSession()->set('cueList'$cueList);
  26.     }
  27.     public function getCurrentIndex(): int
  28.     {
  29.          return $this->requestStack->getSession()->get('currentIndex') ?? 0;
  30.     }
  31.     public function setCurrentIndex(int $currentIndex): void
  32.     {
  33.         $this->requestStack->getSession()->set('currentIndex'$currentIndex);
  34.     }
  35.     public function addToCueList(int $soundId): void
  36.     {
  37.         $cueList $this->getCueList();
  38.         $cueList[] = $soundId;
  39.         $this->setCueList($cueList);
  40.     }
  41.     public function addNextToCueList(int $soundId): void
  42.     {
  43.         $cueList $this->getCueList();
  44.         $currentIndex $this->getCurrentIndex();
  45.         array_splice($cueList$currentIndex 1count($cueList), $soundId);
  46.         $this->setCueList($cueList);
  47.     }
  48.     public function seekToNextSound(): ?Sound
  49.     {
  50.         $currentIndex $this->getCurrentIndex();
  51.         $currentIndex += $currentIndex count($this->getCueList()) ? 0;
  52.         $this->setCurrentIndex($currentIndex);
  53.         return $this->getCurrentSound();
  54.     }
  55.     public function seekToPrevSound(): ?Sound
  56.     {
  57.         $currentIndex $this->getCurrentIndex();
  58.         $currentIndex -= $currentIndex >= 0;
  59.         $this->setCurrentIndex($currentIndex);
  60.         return $this->getCurrentSound();
  61.     }
  62. }