2020-08-16 14:54:01 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-16 14:54:01 +00:00
|
|
|
namespace App\Entity\Repository;
|
|
|
|
|
|
|
|
use App\Entity;
|
2021-03-12 06:28:04 +00:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
use Carbon\CarbonInterface;
|
2021-02-09 19:57:25 +00:00
|
|
|
use Doctrine\ORM\Query;
|
2020-08-16 14:54:01 +00:00
|
|
|
use Doctrine\ORM\QueryBuilder;
|
|
|
|
|
2021-09-06 09:06:31 +00:00
|
|
|
/**
|
2022-05-30 06:25:35 +00:00
|
|
|
* @extends AbstractStationBasedRepository<Entity\StationQueue>
|
2021-09-06 09:06:31 +00:00
|
|
|
*/
|
2022-05-31 07:50:49 +00:00
|
|
|
final class StationQueueRepository extends AbstractStationBasedRepository
|
2020-08-16 14:54:01 +00:00
|
|
|
{
|
2020-12-01 05:27:23 +00:00
|
|
|
public function clearForMediaAndPlaylist(Entity\StationMedia $media, Entity\StationPlaylist $playlist): void
|
|
|
|
{
|
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
DELETE FROM App\Entity\StationQueue sq
|
2022-03-18 09:30:05 +00:00
|
|
|
WHERE sq.media = :media
|
|
|
|
AND sq.playlist = :playlist
|
|
|
|
AND sq.is_played = 0
|
2020-12-01 05:27:23 +00:00
|
|
|
DQL
|
2022-03-18 09:30:05 +00:00
|
|
|
)->setParameter('media', $media)
|
2020-12-01 05:27:23 +00:00
|
|
|
->setParameter('playlist', $playlist)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2020-10-19 10:41:15 +00:00
|
|
|
public function getNextVisible(Entity\Station $station): ?Entity\StationQueue
|
|
|
|
{
|
2021-12-30 19:56:35 +00:00
|
|
|
return $this->getUnplayedBaseQuery($station)
|
|
|
|
->andWhere('sq.is_visible = 1')
|
|
|
|
->getQuery()
|
|
|
|
->setMaxResults(1)
|
|
|
|
->getOneOrNullResult();
|
2020-08-16 14:54:01 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 08:47:18 +00:00
|
|
|
public function trackPlayed(
|
|
|
|
Entity\Station $station,
|
|
|
|
Entity\StationQueue $row
|
|
|
|
): void {
|
2022-02-03 20:44:25 +00:00
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
UPDATE App\Entity\StationQueue sq
|
|
|
|
SET sq.timestamp_played = :timestamp
|
|
|
|
WHERE sq.station = :station
|
|
|
|
AND sq.id = :id
|
|
|
|
DQL
|
|
|
|
)->setParameter('timestamp', time())
|
|
|
|
->setParameter('station', $station)
|
|
|
|
->setParameter('id', $row->getIdRequired())
|
|
|
|
->execute();
|
|
|
|
|
2021-11-14 08:47:18 +00:00
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
UPDATE App\Entity\StationQueue sq
|
|
|
|
SET sq.is_played=1, sq.sent_to_autodj=1
|
|
|
|
WHERE sq.station = :station
|
|
|
|
AND sq.is_played = 0
|
|
|
|
AND (sq.id = :id OR sq.timestamp_cued < :cued)
|
|
|
|
DQL
|
|
|
|
)->setParameter('station', $station)
|
|
|
|
->setParameter('id', $row->getIdRequired())
|
|
|
|
->setParameter('cued', $row->getTimestampCued())
|
|
|
|
->execute();
|
2020-08-16 14:54:01 +00:00
|
|
|
}
|
|
|
|
|
2023-01-19 04:05:51 +00:00
|
|
|
public function isPlaylistRecentlyPlayed(
|
|
|
|
Entity\StationPlaylist $playlist,
|
|
|
|
?int $playPerSongs = null
|
|
|
|
): bool {
|
|
|
|
$playPerSongs ??= $playlist->getPlayPerSongs();
|
2022-04-05 09:47:27 +00:00
|
|
|
|
2023-01-19 04:05:51 +00:00
|
|
|
$recentPlayedQuery = $this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
SELECT sq.playlist_id
|
|
|
|
FROM App\Entity\StationQueue sq
|
|
|
|
WHERE sq.station = :station
|
|
|
|
AND sq.playlist_id IS NOT NULL
|
|
|
|
AND (sq.playlist = :playlist OR sq.is_visible = 1)
|
|
|
|
ORDER BY sq.id DESC
|
|
|
|
DQL
|
|
|
|
)->setParameters([
|
|
|
|
'station' => $playlist->getStation(),
|
|
|
|
'playlist' => $playlist,
|
|
|
|
])->setMaxResults($playPerSongs);
|
2022-04-05 09:47:27 +00:00
|
|
|
|
2023-01-19 04:05:51 +00:00
|
|
|
$recentPlayedPlaylists = $recentPlayedQuery->getSingleColumnResult();
|
|
|
|
return in_array($playlist->getIdRequired(), (array)$recentPlayedPlaylists, true);
|
2021-03-12 06:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed[]
|
|
|
|
*/
|
|
|
|
public function getRecentlyPlayedByTimeRange(
|
|
|
|
Entity\Station $station,
|
|
|
|
CarbonInterface $now,
|
|
|
|
int $minutes
|
|
|
|
): array {
|
|
|
|
$threshold = $now->subMinutes($minutes)->getTimestamp();
|
|
|
|
|
2021-04-12 04:17:29 +00:00
|
|
|
return $this->em->createQuery(
|
|
|
|
<<<'DQL'
|
2022-01-06 02:29:40 +00:00
|
|
|
SELECT sq.song_id, sq.timestamp_played, sq.title, sq.artist
|
2021-04-12 04:17:29 +00:00
|
|
|
FROM App\Entity\StationQueue sq
|
|
|
|
WHERE sq.station = :station
|
2022-01-06 02:29:40 +00:00
|
|
|
AND (sq.is_played = 0 OR sq.timestamp_played >= :threshold)
|
2021-11-24 17:59:16 +00:00
|
|
|
ORDER BY sq.timestamp_played DESC
|
2021-04-12 04:17:29 +00:00
|
|
|
DQL
|
|
|
|
)->setParameter('station', $station)
|
2021-03-12 06:28:04 +00:00
|
|
|
->setParameter('threshold', $threshold)
|
|
|
|
->getArrayResult();
|
|
|
|
}
|
|
|
|
|
2020-08-16 14:54:01 +00:00
|
|
|
/**
|
|
|
|
* @param Entity\Station $station
|
|
|
|
* @return Entity\StationQueue[]
|
|
|
|
*/
|
2021-11-14 08:47:18 +00:00
|
|
|
public function getUnplayedQueue(Entity\Station $station): array
|
|
|
|
{
|
|
|
|
return $this->getUnplayedQuery($station)->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUnplayedQuery(Entity\Station $station): Query
|
2021-02-09 19:57:25 +00:00
|
|
|
{
|
2021-11-14 08:47:18 +00:00
|
|
|
return $this->getUnplayedBaseQuery($station)->getQuery();
|
2021-02-09 19:57:25 +00:00
|
|
|
}
|
|
|
|
|
2021-09-05 22:46:33 +00:00
|
|
|
public function clearUpcomingQueue(Entity\Station $station): void
|
|
|
|
{
|
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
DELETE FROM App\Entity\StationQueue sq
|
|
|
|
WHERE sq.station = :station
|
|
|
|
AND sq.sent_to_autodj = 0
|
|
|
|
DQL
|
|
|
|
)->setParameter('station', $station)
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
2021-11-14 08:47:18 +00:00
|
|
|
public function getNextToSendToAutoDj(Entity\Station $station): ?Entity\StationQueue
|
2021-05-07 15:24:34 +00:00
|
|
|
{
|
2021-11-14 08:47:18 +00:00
|
|
|
return $this->getBaseQuery($station)
|
|
|
|
->andWhere('sq.sent_to_autodj = 0')
|
|
|
|
->orderBy('sq.timestamp_cued', 'ASC')
|
2021-05-07 15:24:34 +00:00
|
|
|
->getQuery()
|
|
|
|
->setMaxResults(1)
|
|
|
|
->getOneOrNullResult();
|
|
|
|
}
|
|
|
|
|
2021-05-30 18:55:26 +00:00
|
|
|
public function findRecentlyCuedSong(
|
|
|
|
Entity\Station $station,
|
|
|
|
Entity\Interfaces\SongInterface $song
|
|
|
|
): ?Entity\StationQueue {
|
2021-12-09 00:38:00 +00:00
|
|
|
return $this->getUnplayedBaseQuery($station)
|
|
|
|
->andWhere('sq.sent_to_autodj = 1')
|
2020-10-04 22:35:41 +00:00
|
|
|
->andWhere('sq.song_id = :song_id')
|
|
|
|
->setParameter('song_id', $song->getSongId())
|
2020-08-16 14:54:01 +00:00
|
|
|
->getQuery()
|
|
|
|
->setMaxResults(1)
|
|
|
|
->getOneOrNullResult();
|
|
|
|
}
|
|
|
|
|
2021-07-11 05:51:00 +00:00
|
|
|
public function hasCuedPlaylistMedia(Entity\StationPlaylist $playlist): bool
|
|
|
|
{
|
|
|
|
$station = $playlist->getStation();
|
|
|
|
|
2021-11-14 08:47:18 +00:00
|
|
|
$cuedPlaylistContentCountQuery = $this->getUnplayedBaseQuery($station)
|
2021-07-11 05:51:00 +00:00
|
|
|
->select('count(sq.id)')
|
|
|
|
->andWhere('sq.playlist = :playlist')
|
|
|
|
->setParameter('playlist', $playlist)
|
|
|
|
->getQuery();
|
|
|
|
|
|
|
|
$cuedPlaylistContentCount = $cuedPlaylistContentCountQuery->getSingleScalarResult();
|
2021-11-14 08:47:18 +00:00
|
|
|
return $cuedPlaylistContentCount > 0;
|
2021-07-11 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 04:29:04 +00:00
|
|
|
private function getUnplayedBaseQuery(Entity\Station $station): QueryBuilder
|
2021-03-12 06:28:04 +00:00
|
|
|
{
|
|
|
|
return $this->getBaseQuery($station)
|
2021-11-14 08:47:18 +00:00
|
|
|
->andWhere('sq.is_played = 0')
|
|
|
|
->orderBy('sq.sent_to_autodj', 'DESC')
|
|
|
|
->addOrderBy('sq.timestamp_cued', 'ASC');
|
2021-03-12 06:28:04 +00:00
|
|
|
}
|
|
|
|
|
2022-08-23 04:29:04 +00:00
|
|
|
private function getBaseQuery(Entity\Station $station): QueryBuilder
|
2020-08-16 14:54:01 +00:00
|
|
|
{
|
|
|
|
return $this->em->createQueryBuilder()
|
2020-10-04 22:35:41 +00:00
|
|
|
->select('sq, sm, sp')
|
2020-08-16 14:54:01 +00:00
|
|
|
->from(Entity\StationQueue::class, 'sq')
|
|
|
|
->leftJoin('sq.media', 'sm')
|
|
|
|
->leftJoin('sq.playlist', 'sp')
|
|
|
|
->where('sq.station = :station')
|
2021-03-12 06:28:04 +00:00
|
|
|
->setParameter('station', $station);
|
|
|
|
}
|
|
|
|
|
2022-03-19 23:22:21 +00:00
|
|
|
public function clearUnplayed(?Entity\Station $station = null): void
|
2022-03-15 08:38:32 +00:00
|
|
|
{
|
2022-03-19 23:03:09 +00:00
|
|
|
$qb = $this->em->createQueryBuilder()
|
|
|
|
->delete(Entity\StationQueue::class, 'sq')
|
|
|
|
->where('sq.is_played = 0');
|
|
|
|
|
|
|
|
if (null !== $station) {
|
|
|
|
$qb->andWhere('sq.station = :station')
|
|
|
|
->setParameter('station', $station);
|
|
|
|
}
|
|
|
|
|
|
|
|
$qb->getQuery()->execute();
|
2022-03-15 08:38:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-12 06:28:04 +00:00
|
|
|
public function cleanup(int $daysToKeep): void
|
|
|
|
{
|
|
|
|
$threshold = CarbonImmutable::now()
|
|
|
|
->subDays($daysToKeep)
|
|
|
|
->getTimestamp();
|
|
|
|
|
|
|
|
$this->em->createQuery(
|
|
|
|
<<<'DQL'
|
|
|
|
DELETE FROM App\Entity\StationQueue sq
|
|
|
|
WHERE sq.timestamp_cued <= :threshold
|
|
|
|
DQL
|
|
|
|
)->setParameter('threshold', $threshold)
|
|
|
|
->execute();
|
2020-08-16 14:54:01 +00:00
|
|
|
}
|
2020-10-14 22:19:31 +00:00
|
|
|
}
|