<?php
namespace App\Service;
use App\Entity\Sound;
use App\Repository\SoundRepository;
use Symfony\Component\HttpFoundation\RequestStack;
class CueList
{
public function __construct(
private readonly RequestStack $requestStack,
private readonly SoundRepository $soundRepository
) {
}
public function getCurrentSound(): ?Sound
{
return count($this->getCueList()) ?
$this->soundRepository->find($this->getCueList()[$this->getCurrentIndex()]) :
null;
}
public function getCueList(): array
{
return $this->requestStack->getSession()->get('cueList') ?? [];
}
public function setCueList(array $cueList): void
{
$this->requestStack->getSession()->set('cueList', $cueList);
}
public function getCurrentIndex(): int
{
return $this->requestStack->getSession()->get('currentIndex') ?? 0;
}
public function setCurrentIndex(int $currentIndex): void
{
$this->requestStack->getSession()->set('currentIndex', $currentIndex);
}
public function addToCueList(int $soundId): void
{
$cueList = $this->getCueList();
$cueList[] = $soundId;
$this->setCueList($cueList);
}
public function addNextToCueList(int $soundId): void
{
$cueList = $this->getCueList();
$currentIndex = $this->getCurrentIndex();
array_splice($cueList, $currentIndex + 1, count($cueList), $soundId);
$this->setCueList($cueList);
}
public function seekToNextSound(): ?Sound
{
$currentIndex = $this->getCurrentIndex();
$currentIndex += $currentIndex + 1 < count($this->getCueList()) ? 1 : 0;
$this->setCurrentIndex($currentIndex);
return $this->getCurrentSound();
}
public function seekToPrevSound(): ?Sound
{
$currentIndex = $this->getCurrentIndex();
$currentIndex -= $currentIndex - 1 >= 0 ? 1 : 0;
$this->setCurrentIndex($currentIndex);
return $this->getCurrentSound();
}
}