<?php

namespace Formatrica\Submission\Pipeline;

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

/**
 * Runs an ordered list of stages against a context, stopping early if a stage
 * finishes the pipeline. Stages throw to abort (surfaced to the caller).
 */
final class Submission_Pipeline
{
    /**
     * @param list<Submission_Stage> $stages
     */
    public function __construct(private array $stages)
    {
    }

    public function process(Submission_Context $ctx): Submission_Context
    {
        foreach ($this->stages as $stage) {
            if ($ctx->is_finished()) {
                break;
            }
            $stage->handle($ctx);
        }

        return $ctx;
    }
}
