2026-11-15 08:45:17 -08:00
|
|
|
<?php
|
|
|
|
|
declare( strict_types=1 );
|
|
|
|
|
|
|
|
|
|
namespace StreamGoals\Overlay;
|
|
|
|
|
|
|
|
|
|
/** Maps a widget name to its payload using a data source. */
|
|
|
|
|
final class OverlayService {
|
|
|
|
|
|
2026-11-15 12:36:28 -08:00
|
|
|
public const WIDGETS = array( 'goal', 'timer', 'counter', 'alerts', 'actions' );
|
2026-11-15 08:45:17 -08:00
|
|
|
|
|
|
|
|
public function __construct( private OverlayDataSource $source ) {}
|
|
|
|
|
|
2026-11-15 12:26:03 -08:00
|
|
|
public function payload( string $widget, int $now, ?int $since = null, bool $recent = false ): ?array {
|
2026-11-15 08:45:17 -08:00
|
|
|
$goal = $this->source->activeGoal();
|
|
|
|
|
$gid = $goal ? (int) $goal['id'] : 0;
|
|
|
|
|
switch ( $widget ) {
|
|
|
|
|
case 'goal':
|
|
|
|
|
return Payloads::goal( $goal, $goal ? $this->source->raisedMinor( $goal ) : 0 );
|
|
|
|
|
case 'timer':
|
|
|
|
|
return Payloads::timer(
|
|
|
|
|
$gid ? $this->source->stateValue( $gid, 'timer_ends_at' ) : null,
|
|
|
|
|
$gid ? $this->source->stateValue( $gid, 'paused_remaining' ) : null,
|
|
|
|
|
$now
|
|
|
|
|
);
|
|
|
|
|
case 'counter':
|
|
|
|
|
$c = $gid ? $this->source->counter( $gid ) : array( 'label' => '', 'value' => 0 );
|
|
|
|
|
return Payloads::counter( $c['label'], $c['value'] );
|
|
|
|
|
case 'alerts':
|
2026-11-15 12:26:03 -08:00
|
|
|
if ( $recent ) {
|
|
|
|
|
// Newest 20 approved rows regardless of cursor. Sources that predate RecentDonationsSource
|
|
|
|
|
// fall back to the oldest 20 (still correct for small installs, just not "newest").
|
|
|
|
|
$rows = $this->source instanceof RecentDonationsSource
|
|
|
|
|
? $this->source->recentDonations( 20 )
|
|
|
|
|
: $this->source->donations( 0, 20 );
|
|
|
|
|
$since = null;
|
|
|
|
|
} else {
|
|
|
|
|
$rows = $this->source->donations( $since, 20 );
|
|
|
|
|
}
|
2026-11-15 12:07:06 -08:00
|
|
|
$ids = array_values( array_filter( array_map( static fn( $r ) => (int) ( $r['id'] ?? 0 ), $rows ) ) );
|
2026-11-15 12:26:03 -08:00
|
|
|
$bonuses = ( ( $recent || null !== $since ) && $ids ) ? $this->source->bonusesFor( $ids ) : array();
|
2026-11-15 12:36:28 -08:00
|
|
|
$actions = ( ( $recent || null !== $since ) && $ids && $this->source instanceof ActionsSource ) ? $this->source->actionsFor( $ids ) : array();
|
2026-11-15 12:07:06 -08:00
|
|
|
foreach ( $rows as $i => $r ) {
|
|
|
|
|
$rows[ $i ]['bonuses'] = $bonuses[ (int) ( $r['id'] ?? 0 ) ] ?? array();
|
2026-11-15 12:36:28 -08:00
|
|
|
$rows[ $i ]['actions'] = $actions[ (int) ( $r['id'] ?? 0 ) ] ?? array();
|
2026-11-15 12:07:06 -08:00
|
|
|
}
|
2026-11-15 12:26:03 -08:00
|
|
|
return Payloads::alerts( $rows, $since, $recent ? 20 : 10, $recent );
|
2026-11-15 12:36:28 -08:00
|
|
|
case 'actions':
|
|
|
|
|
$rows = ( $gid && $this->source instanceof ActionsSource ) ? $this->source->menuActions( $gid ) : array();
|
|
|
|
|
return Payloads::actionsMenu( $goal, $rows );
|
2026-11-15 08:45:17 -08:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|