*/ private array $check_ids; /** @param list $check_ids Exact implemented Rendered plan. */ public function __construct( RunRepository $runs, TaskRepository $tasks, FindingRepository $findings, RenderedObservationContract $contract, RenderedFindingAnalyzer $analyzer, RunCoordinator $coordinator, TransactionManager $transaction, array $check_ids ) { if ( array() === $check_ids || count( $check_ids ) !== count( array_unique( $check_ids ) ) ) { throw new InvalidArgumentException( 'rendered_plan_invalid' ); } $this->runs = $runs; $this->tasks = $tasks; $this->findings = $findings; $this->contract = $contract; $this->analyzer = $analyzer; $this->coordinator = $coordinator; $this->transaction = $transaction; $this->check_ids = $check_ids; } /** * @param array $input Untrusted REST payload. * @return array{state:string,run_status:string,check_id:string|null} */ public function submit( string $run_uuid, array $input ): array { $run = $this->runs->get_by_uuid( $run_uuid ); if ( null === $run ) { throw new StorageException( 'run_not_found' ); } if ( 'rendered' !== $run->profile() || RunStatus::RUNNING !== $run->status() ) { throw new StorageException( 'rendered_run_not_accepting_observation' ); } $counts = $this->tasks->counts( $run->id() ); if ( count( $this->check_ids ) !== $counts['total'] || $counts['queued'] !== $counts['total'] || 0 !== $counts['completed'] + $counts['running'] + $counts['failed'] + $counts['cancelled'] ) { throw new StorageException( 'rendered_observation_already_submitted' ); } $observation = $this->contract->sanitize( $input ); $timestamp = mysql_to_rfc3339( current_time( 'mysql', true ) ); $started = microtime( true ); $findings = array(); foreach ( $this->check_ids as $check_id ) { $findings[ $check_id ] = $this->analyzer->analyze( $check_id, $observation, $timestamp ); } $encoded = wp_json_encode( $findings, JSON_UNESCAPED_SLASHES ); if ( false === $encoded || strlen( $encoded ) > 2 * 1024 * 1024 ) { throw new StorageException( 'rendered_findings_too_large' ); } $resources = is_array( $observation['page']['resources'] ?? null ) ? $observation['page']['resources'] : array(); $viewports = is_array( $observation['page']['viewports'] ?? null ) ? $observation['page']['viewports'] : array(); $requests = isset( $resources['request_count'] ) ? min( 16, (int) $resources['request_count'] ) + 1 : 0; $usage = array( 'milliseconds' => min( 30000, (int) $observation['duration_ms'] + (int) ceil( ( microtime( true ) - $started ) * 1000 ) ), 'items' => count( $findings ) + count( $viewports ) + min( 16, (int) ( $resources['request_count'] ?? 0 ) ), 'requests' => $requests, 'bytes' => strlen( $encoded ), ); $this->transaction->begin(); try { $this->runs->consume_budget( $run->id(), $usage ); foreach ( $this->check_ids as $check_id ) { $lease = $this->tasks->acquire_next( $run->id(), 60 ); if ( null === $lease || $check_id !== $lease->check_id() ) { throw new StorageException( 'rendered_task_order_invalid' ); } $this->findings->save( $run->id(), $findings[ $check_id ] ); if ( ! $this->tasks->complete( $lease, array( 'finding_count' => 1 ) ) ) { throw new StorageException( 'lease_expired_before_completion' ); } } $this->transaction->commit(); } catch ( Throwable $exception ) { $this->transaction->rollback(); throw $exception; } $settled = $this->coordinator->settle( $run_uuid ); return array( 'state' => 'completed', 'run_status' => $settled->status(), 'check_id' => null, ); } }