Files
Atomcms-edit/app/Console/Commands/AutoDjPlayback.php
T

71 lines
2.1 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Models\Miscellaneous\WebsiteSetting;
use App\Models\RadioAutoDjTrack;
use App\Services\Community\RadioScheduleService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Symfony\Component\Console\Attribute\AsCommand;
#[AsCommand(name: 'radio:auto-dj')]
final class AutoDjPlayback extends Command
{
#[\Override]
protected $signature = 'radio:auto-dj {--force : Update now-playing even if track has not changed}';
#[\Override]
protected $description = 'Auto DJ fallback: updates now-playing from playlist when no DJ is live';
public function handle(RadioScheduleService $scheduleService): int
{
$enabled = $this->getSetting('radio_auto_dj_detection', '0');
if ($enabled !== '1' && ! $this->option('force')) {
$this->info('Auto DJ is uitgeschakeld. Gebruik --force om toch te draaien.');
return Command::SUCCESS;
}
$currentDjId = $this->getSetting('radio_current_dj_id', '');
$currentDj = $scheduleService->getCurrentDJ($currentDjId ?: null);
$track = RadioAutoDjTrack::getNextTrack();
if (! $track) {
$this->info('Geen Auto DJ tracks in de playlist.');
return Command::SUCCESS;
}
if ($currentDj !== null) {
Cache::forget('radio_auto_dj_active');
$this->info('DJ is live. Auto DJ niet actief.');
return Command::SUCCESS;
}
Cache::forever('radio_auto_dj_active', [
'title' => $track->title,
'artist' => $track->artist,
'is_auto_dj' => true,
]);
$track->markPlayed();
$this->info("Auto DJ speelt: {$track->title}" . ($track->artist ? " by {$track->artist}" : ''));
return Command::SUCCESS;
}
private function getSetting(string $key, string $default = ''): string
{
$setting = WebsiteSetting::where('key', $key)->first();
return $setting !== null && isset($setting->value) ? (string) $setting->value : $default;
}
}