26 lines
1012 B
PHP
26 lines
1012 B
PHP
<?php
|
|||
|
|
declare( strict_types=1 );
|
||
|
|
|
||
|
|
namespace StreamGoals\Actions;
|
||
|
|
|
||
|
|
/** Queues the on-stream actions a completed donation triggers. */
|
||
|
|
final class Recorder {
|
||
|
|
|
||
|
|
/** Never throws. Idempotent: replays are ignored by the UNIQUE key on sg_action_events. */
|
||
|
|
public static function forDonation( int $donationId ): void {
|
||
|
|
try {
|
||
|
|
global $wpdb;
|
||
|
|
$row = $wpdb->get_row( $wpdb->prepare( 'SELECT id, goal_id, base_minor, status FROM ' . $wpdb->prefix . 'sg_donations WHERE id = %d', $donationId ), ARRAY_A );
|
||
|
|
if ( ! is_array( $row ) || 'completed' !== ( $row['status'] ?? '' ) ) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
$repo = new ActionRepository( $wpdb );
|
||
|
|
$matches = Matcher::match( (int) $row['base_minor'], $repo->enabledForGoal( (int) $row['goal_id'] ) );
|
||
|
|
if ( $matches ) {
|
||
|
|
$repo->recordEvents( $donationId, $matches );
|
||
|
|
}
|
||
|
|
} catch ( \Throwable $e ) {
|
||
|
|
error_log( 'StreamGoals: Recorder failed for donation ' . $donationId . ': ' . $e->getMessage() ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|