self::NEW, 'inprogress' => self::INPROGRESS, 'complete' => self::COMPLETE, 'error' => self::ERROR, 'cancelled' => self::CANCELLED, 'completed' => self::COMPLETED, 'ready' => self::READY, 'ready_for_review' => self::READY_FOR_REVIEW, 'orphaned' => self::ORPHANED, default => null, }; } /** * Map API numeric status codes to enum cases (used in API v1). * * @param int $api_code The numeric status code from the API. * @return BatchRequestStatus|null The corresponding enum case, or null if unrecognized. */ public static function from_api_code( int $api_code ): ?self { return match ( $api_code ) { 0 => self::NEW, 1 => self::INPROGRESS, 2 => self::COMPLETE, 3 => self::ERROR, 4 => self::CANCELLED, default => null, }; } /** * Convert the enum case to a human-readable label. * * @param self $enum_case The enum case to convert. * @return string The human-readable label for the given enum case. */ public static function to_human_readable( self $enum_case ): string { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.SelfOutsideClassScopeFound -- enum scope is class-like; plugin requires PHP 8.1+ return match ( $enum_case ) { self::NEW => Messages::get( 'enums.status_new' ), self::INPROGRESS => Messages::get( 'enums.status_in_progress' ), self::COMPLETE => Messages::get( 'enums.status_complete' ), self::ERROR => Messages::get( 'enums.status_error' ), self::CANCELLED => Messages::get( 'enums.status_cancelled' ), self::PENDING => Messages::get( 'enums.status_pending' ), self::COMPLETED => Messages::get( 'enums.status_completed' ), self::READY => Messages::get( 'enums.status_ready' ), self::READY_FOR_REVIEW => Messages::get( 'enums.status_ready_for_review' ), self::ORPHANED => Messages::get( 'enums.status_orphaned' ), }; } /** * Get an array of all enum case values. * * @return array An array of all enum case values. */ public static function options_array(): array { return array_map( fn( $enum_case ) => $enum_case->value, self::cases() ); } /** * Statuses that indicate a batch request is finished and should not be polled again. * * Covers both v1 spellings ('complete', 'error') and v2 ('completed', 'failed') * plus 'cancelled', so records stored under either API version are excluded. * * @return string[] */ public static function terminal_statuses(): array { return array( self::COMPLETE->value, self::COMPLETED->value, self::ERROR->value, self::CANCELLED->value, // 'failed' is a legacy DB value from v1 that predates the enum. 'failed', // 'orphaned' means the backend has lost this batch; stop polling. self::ORPHANED->value, ); } /** * Statuses for which optimized data may be available from the API * and row hydration should be attempted (step 4 in detail tables). * * Includes active processing states because partial results may be * available while the batch is still running. * * @return string[] */ public static function api_fetchable(): array { return array( self::INPROGRESS->value, self::COMPLETE->value, self::COMPLETED->value, self::READY->value, self::READY_FOR_REVIEW->value, // Legacy v1 value that may still exist in DB rows. 'processing', ); } /** * Statuses for which rows should be marked 'ready' for import * even if no optimized data was found (step 5 in detail tables). * * @return string[] */ public static function hydration_eligible(): array { return array( self::COMPLETED->value, self::COMPLETE->value, self::READY->value, self::READY_FOR_REVIEW->value ); } }