Exemplo n.º 1
0
 /**
  * @param array $processedItems
  *
  * @return null
  */
 protected function write($processedItems)
 {
     try {
         $this->writer->write($processedItems);
     } catch (InvalidItemException $e) {
         $this->handleStepExecutionWarning($this->stepExecution, $this->writer, $e);
     }
 }
 /**
  * {@inheritdoc}
  *
  * Re-parse the imported file and write into a new one the invalid lines.
  */
 public function archive(JobExecution $jobExecution)
 {
     if (empty($this->collector->getInvalidItems())) {
         return;
     }
     $invalidLineNumbers = new ArrayCollection();
     foreach ($this->collector->getInvalidItems() as $invalidItem) {
         if ($invalidItem instanceof InvalidItemInterface) {
             $invalidLineNumbers->add($invalidItem->getLineNumber());
         }
     }
     $readJobParameters = $jobExecution->getJobParameters();
     $currentLineNumber = 0;
     $itemsToWrite = [];
     $fileIterator = $this->getInputFileIterator($readJobParameters);
     $headers = $fileIterator->getHeaders();
     $this->setupWriter($jobExecution);
     foreach ($fileIterator as $readItem) {
         $currentLineNumber++;
         if ($invalidLineNumbers->contains($currentLineNumber)) {
             $itemsToWrite[] = array_combine($headers, $readItem);
             $invalidLineNumbers->removeElement($currentLineNumber);
         }
         if (count($itemsToWrite) > 0 && 0 === count($itemsToWrite) % $this->batchSize) {
             $this->writer->write($itemsToWrite);
             $itemsToWrite = [];
         }
         if ($invalidLineNumbers->isEmpty()) {
             break;
         }
     }
     if (count($itemsToWrite) > 0) {
         $this->writer->write($itemsToWrite);
     }
     $this->writer->flush();
 }