Files
wp-donations/includes/Overlay/OverlayService.php
T

39 lines
1.4 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' );
public function __construct( private OverlayDataSource $source ) {}
public function payload( string $widget, int $now, ?int $since = null ): ?array {
$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':
$rows = $this->source->donations( $since, 20 );
$ids = array_values( array_filter( array_map( static fn( $r ) => (int) ( $r['id'] ?? 0 ), $rows ) ) );
$bonuses = ( null !== $since && $ids ) ? $this->source->bonusesFor( $ids ) : array();
foreach ( $rows as $i => $r ) {
$rows[ $i ]['bonuses'] = $bonuses[ (int) ( $r['id'] ?? 0 ) ] ?? array();
}
return Payloads::alerts( $rows, $since );
}
return null;
}
}