<?php

declare(strict_types=1);

/**
 * CompletionGatePhaseService - Cloud result polling and scan completion gate.
 *
 * Wraps the ScanOrchestrator's poll phase and completion check. Participates
 * in the round-robin loop like any other phase; its tick() polls cloud jobs
 * for results, and is_complete() signals when the scan is fully done.
 *
 */

namespace SiteFort\Modules\Scanner\Queue\Phase;

if ( ! defined( 'ABSPATH' ) ) { exit; }

class CompletionGatePhaseService implements PhaseServiceInterface {

    /** @var callable(array): bool */
    private $tick_fn;

    /** @var callable(): bool */
    private $is_complete_fn;

    /**
     * @param callable(array &$stats): bool $tick_fn       Polls cloud results; returns true if work done.
     * @param callable(): bool              $is_complete_fn Returns true when scan is fully complete.
     */
    public function __construct( callable $tick_fn, callable $is_complete_fn ) {
        $this->tick_fn        = $tick_fn;
        $this->is_complete_fn = $is_complete_fn;
    }

    public function tick( array &$stats ): bool {
        return ( $this->tick_fn )( $stats );
    }

    public function is_complete(): bool {
        return ( $this->is_complete_fn )();
    }

    public function get_phase_name(): string {
        return 'poll';
    }
}
