settings = $settings; $this->http = $http; $this->progress = $progress; } public function check(): void { $progress = $this->progress->current(); if ( $progress['phase'] !== SyncProgress::PHASE_INDEXING ) { return; } $sessionId = (string) ( $progress['session_id'] ?? '' ); $processed = (int) $progress['processed']; if ( $sessionId === '' ) { $this->progress->fail( \__( 'Synchronization state was lost. Please start the synchronization again.', 'agent001-support-assistant-for-woocommerce' ) ); return; } if ( time() - (int) ( $progress['checked_at'] ?? 0 ) < self::CHECK_INTERVAL_SECONDS ) { return; } $result = $this->http->getJson( $this->settings->syncSessionUrl( $sessionId ), [ 'X-API-Key' => $this->settings->apiKey(), 'Accept' => 'application/json', 'User-Agent' => 'Agent001/' . ( \defined( 'AGENT001_VERSION' ) ? \AGENT001_VERSION : 'dev' ), ] ); if ( ! $result->isSuccess() ) { $this->handleFailedCheck( $result->status(), $result->error(), (int) ( $progress['indexing_since'] ?? 0 ), $progress ); return; } $data = json_decode( $result->body(), true ); $status = is_array( $data ) ? (string) ( $data['status'] ?? '' ) : ''; if ( $status === 'succeeded' ) { $this->progress->finish( true, sprintf( /* translators: %d: number of products synchronized */ \_n( 'Synced %d product.', 'Synced %d products.', $processed, 'agent001-support-assistant-for-woocommerce' ), $processed ), $processed ); return; } if ( $status === 'failed' ) { $error = is_string( $data['error'] ?? null ) ? \sanitize_text_field( $data['error'] ) : ''; $this->progress->finish( false, $error !== '' ? \__( 'Indexing failed:', 'agent001-support-assistant-for-woocommerce' ) . ' ' . $error : \__( 'Indexing failed. Please try again.', 'agent001-support-assistant-for-woocommerce' ), $processed ); return; } if ( $this->timedOut( (int) ( $progress['indexing_since'] ?? 0 ) ) ) { $this->failTimedOut(); return; } $this->progress->indexingChecked( (int) ( $data['processed_batches'] ?? 0 ), isset( $data['expected_batches'] ) ? (int) $data['expected_batches'] : null ); } /** * @param array $progress */ private function handleFailedCheck( int $status, string $error, int $indexingSince, array $progress ): void { if ( $status === 404 ) { $this->progress->fail( \__( 'This synchronization was replaced by a newer one. Please check the latest synchronization.', 'agent001-support-assistant-for-woocommerce' ) ); return; } if ( $status >= 400 && $status < 500 && $status !== 408 && $status !== 429 ) { $this->progress->fail( $error ); return; } if ( $this->timedOut( $indexingSince ) ) { $this->failTimedOut(); return; } $this->progress->indexingChecked( (int) ( $progress['processed_batches'] ?? 0 ), isset( $progress['expected_batches'] ) ? (int) $progress['expected_batches'] : null ); } private function timedOut( int $indexingSince ): bool { return $indexingSince > 0 && time() - $indexingSince > self::TIMEOUT_SECONDS; } private function failTimedOut(): void { $this->progress->fail( \__( 'Indexing is taking longer than expected. Check the synchronization status in Agent001.', 'agent001-support-assistant-for-woocommerce' ) ); } }