get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, 5)', $name ) ); try { $fn(); } finally { if ( $locked ) { $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $name ) ); } } } public function __construct( private readonly RuleStore $rules, private readonly EventStore $events, private readonly StateRepository $state, ?callable $clock = null, ?callable $onBonus = null ) { $this->clock = $clock ?? static fn(): int => time(); $this->onBonus = $onBonus; } public function applyDonation( Donation $d ): void { foreach ( $this->rules->enabledForGoal( $d->goalId ) as $def ) { try { $this->applyRule( $def, $d ); } catch ( \InvalidArgumentException ) { continue; // Misconfigured rule must not block the others. } } } public function reverseDonation( Donation $d ): void { foreach ( $this->rules->enabledForGoal( $d->goalId ) as $def ) { if ( ! $def->reverseOnRefund() ) { continue; } try { $this->reverseRule( $def, $d ); } catch ( \InvalidArgumentException ) { continue; } } } private function applyRule( RuleDef $def, Donation $d ): void { $g = $d->goalId; switch ( $def->type ) { case 'add_time': $rule = AddTimeRule::fromConfig( $def->config ); $k = "timer_added:{$def->id}"; $r = $rule->apply( $d, $this->state->getInt( $g, 'timer_ends_at' ), $this->state->getInt( $g, $k ), ( $this->clock )() ); if ( ! $this->events->record( $def->id, $d->id, $r['delta'] ) ) { return; } if ( $r['delta'] > 0 ) { $this->state->setInt( $g, 'timer_ends_at', $r['ends_at'] ); $this->state->setInt( $g, $k, $r['added_total'] ); } return; case 'counter': $rule = CounterRule::fromConfig( $def->config ); $r = $rule->apply( $d, $this->state->getInt( $g, "counter:{$def->id}" ), $this->state->getInt( $g, "carry:{$def->id}" ) ); if ( ! $this->events->record( $def->id, $d->id, $r['delta'] ) ) { return; } $this->state->setInt( $g, "counter:{$def->id}", $r['counter'] ); $this->state->setInt( $g, "carry:{$def->id}", $r['carry'] ); return; case 'bonus': $rule = BonusRule::fromConfig( $def->config ); $fired = $rule->apply( $d ); if ( ! $this->events->record( $def->id, $d->id, $fired ) ) { return; } if ( $fired && $this->onBonus ) { ( $this->onBonus )( $rule->event, $def, $d, false ); } return; } } private function reverseRule( RuleDef $def, Donation $d ): void { $g = $d->goalId; $delta = $this->events->markReversed( $def->id, $d->id ); if ( null === $delta ) { return; // Never applied, or already reversed. } switch ( $def->type ) { case 'add_time': $k = "timer_added:{$def->id}"; $r = AddTimeRule::fromConfig( $def->config )->reverse( $delta, $this->state->getInt( $g, 'timer_ends_at' ), $this->state->getInt( $g, $k ), ( $this->clock )() ); if ( $delta > 0 ) { $this->state->setInt( $g, 'timer_ends_at', $r['ends_at'] ); $this->state->setInt( $g, $k, $r['added_total'] ); } return; case 'counter': $r = CounterRule::fromConfig( $def->config )->reverse( $d, $delta, $this->state->getInt( $g, "counter:{$def->id}" ), $this->state->getInt( $g, "carry:{$def->id}" ) ); $this->state->setInt( $g, "counter:{$def->id}", $r['counter'] ); $this->state->setInt( $g, "carry:{$def->id}", $r['carry'] ); return; case 'bonus': if ( $delta > 0 && $this->onBonus ) { ( $this->onBonus )( BonusRule::fromConfig( $def->config )->event, $def, $d, true ); } return; } } }