queue = $queue; $this->consumer = $consumer; $this->ateApi = $ateApi; } /** * @param int $quantity * * @return Result */ public function run( $quantity = 5 ) { $result = new Result(); $job = $processedJob = null; do { try { $job = $this->queue->getFirst(); if ( $job ) { $this->queue->remove( $job ); $processedJob = $this->consumer->process( $job ); if ( ! $processedJob ) { throw new Exception( 'The translation job could not be applied.' ); } $result->processedJobs->push( $processedJob ); } } catch ( Exception $e ) { $this->logException( $e, $processedJob ?: $job ); } $processedJob = null; $quantity--; } while ( $quantity && $job ); $this->acknowledgeAte( $result->processedJobs ); $result->downloadQueueSize = $this->queue->count(); return $result; } private function acknowledgeAte( Collection $processedJobs ) { if ( $processedJobs->count() ) { $this->ateApi->confirm_received_job( $processedJobs->pluck( 'ateJobId' )->toArray() ); } } /** * @param Exception $e * @param Job|null $job */ private function logException( Exception $e, Job $job = null ) { $entry = new Entry(); $entry->description = $e->getMessage(); if ( $job ) { $entry->ateJobId = $job->ateJobId; $entry->wpmlJobId = $job->wpmlJobId; $entry->extraData = [ 'downloadUrl' => $job->url ]; } if ( $e instanceof \Requests_Exception ) { $entry->event = ErrorEvents::SERVER_XLIFF; } else { $entry->event = ErrorEvents::JOB_DOWNLOAD; } wpml_tm_ate_ams_log( $entry ); } }