Files

81 lines
3.8 KiB
PHP
Raw Permalink Normal View History

<?php
declare( strict_types=1 );
namespace StreamGoals\Overlay;
/** Maps a widget name to its payload using a data source. */
final class OverlayService {
public const WIDGETS = array( 'goal', 'timer', 'counter', 'alerts', 'actions' );
/** @var array<int,array<string,mixed>>|null */
private ?array $defs = null;
public function __construct( private OverlayDataSource $source ) {}
/** Enabled alert definitions, loaded at most once per service instance (= per request). */
private function defs(): array {
if ( null === $this->defs ) {
$this->defs = $this->source instanceof AlertMediaSource ? array_values( $this->source->alertDefs() ) : array();
}
return $this->defs;
}
/** Extra ETag input so editing media definitions invalidates cached alert payloads even when no alert is pending. */
public function etagSalt( string $widget ): string {
return 'alerts' === $widget && $this->defs() ? md5( (string) json_encode( $this->defs() ) ) : '';
}
public function payload( string $widget, int $now, ?int $since = null, bool $recent = false ): ?array {
$goal = $this->source->activeGoal();
$gid = $goal ? (int) $goal['id'] : 0;
switch ( $widget ) {
case 'goal':
$tiers = ( $gid && $this->source instanceof TiersSource ) ? $this->source->tiersFor( $gid ) : array();
return Payloads::goal( $goal, $goal ? $this->source->raisedMinor( $goal ) : 0, $tiers ?: null );
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':
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 );
}
$ids = array_values( array_filter( array_map( static fn( $r ) => (int) ( $r['id'] ?? 0 ), $rows ) ) );
$bonuses = ( ( $recent || null !== $since ) && $ids ) ? $this->source->bonusesFor( $ids ) : array();
$actions = ( ( $recent || null !== $since ) && $ids && $this->source instanceof ActionsSource ) ? $this->source->actionsFor( $ids ) : array();
$tiers = ( ( $recent || null !== $since ) && $ids && $this->source instanceof TiersSource ) ? $this->source->tiersReachedFor( $ids ) : array();
foreach ( $rows as $i => $r ) {
$rows[ $i ]['bonuses'] = $bonuses[ (int) ( $r['id'] ?? 0 ) ] ?? array();
$rows[ $i ]['actions'] = $actions[ (int) ( $r['id'] ?? 0 ) ] ?? array();
$rows[ $i ]['tiers'] = $tiers[ (int) ( $r['id'] ?? 0 ) ] ?? array();
}
$mediaFor = null;
$defs = ( $recent || null !== $since ) ? $this->defs() : array();
if ( $defs && $ids && $this->source instanceof AlertMediaSource ) {
$src = $this->source;
$triggered = $src->triggeredActionsFor( $ids );
$mediaFor = static fn( array $r, string $name, string $amount ): ?array => \StreamGoals\Alerts\AlertMedia::build(
$r, $name, $amount, $defs, $triggered[ (int) ( $r['id'] ?? 0 ) ] ?? array(), static fn( int $id ): ?array => $src->attachment( $id )
);
}
return Payloads::alerts( $rows, $since, $recent ? 20 : 10, $recent, $mediaFor );
case 'actions':
$rows = ( $gid && $this->source instanceof ActionsSource ) ? $this->source->menuActions( $gid ) : array();
return Payloads::actionsMenu( $goal, $rows );
}
return null;
}
}