reportHelper = $reportHelper; $this->sourceCsvFactory = $sourceCsvFactory; $this->outputCsvFactory = $outputCsvFactory; $this->filesystem = $filesystem; } /** * @param string $originalFileName * @param ProcessingErrorAggregatorInterface $errorAggregator * @param bool $writeOnlyErrorItems * @return string * @throws \Magento\Framework\Exception\LocalizedException */ public function createReport( $originalFileName, ProcessingErrorAggregatorInterface $errorAggregator, $writeOnlyErrorItems = false ) { $sourceCsv = $this->createSourceCsvModel($originalFileName); $outputFileName = $this->generateOutputFileName($originalFileName); $outputCsv = $this->createOutputCsvModel($outputFileName); $columnsName = $sourceCsv->getColNames(); $columnsName[] = self::REPORT_ERROR_COLUMN_NAME; $outputCsv->setHeaderCols($columnsName); foreach ($sourceCsv as $rowNum => $rowData) { $errorMessages = $this->retrieveErrorMessagesByRowNumber($rowNum, $errorAggregator); if (!$writeOnlyErrorItems || ($writeOnlyErrorItems && $errorMessages)) { $rowData[self::REPORT_ERROR_COLUMN_NAME] = $errorMessages; $outputCsv->writeRow($rowData); } } return $outputFileName; } /** * @param int $rowNumber * @param ProcessingErrorAggregatorInterface $errorAggregator * @return string */ public function retrieveErrorMessagesByRowNumber($rowNumber, ProcessingErrorAggregatorInterface $errorAggregator) { $messages = ''; foreach ($errorAggregator->getErrorByRowNumber((int)$rowNumber) as $error) { $messages .= $error->getErrorMessage() . ','; } $messages = rtrim($messages, ','); if ($messages) { $messages = str_pad($messages, 1, '"', STR_PAD_BOTH); } return $messages; } /** * @param string $sourceFile * @return string */ protected function generateOutputFileName($sourceFile) { $fileName = basename($sourceFile, self::ERROR_REPORT_FILE_EXTENSION); return $fileName . self::ERROR_REPORT_FILE_SUFFIX . self::ERROR_REPORT_FILE_EXTENSION; } /** * @param string $sourceFile * @return \Magento\ImportExport\Model\Import\Source\Csv */ protected function createSourceCsvModel($sourceFile) { return $this->sourceCsvFactory->create( [ 'file' => $sourceFile, 'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR), 'delimiter' => $this->reportHelper->getDelimiter(), ] ); } /** * @param string $outputFileName * @return \Magento\ImportExport\Model\Export\Adapter\Csv */ protected function createOutputCsvModel($outputFileName) { return $this->outputCsvFactory->create( [ 'destination' => Import::IMPORT_HISTORY_DIR . $outputFileName, 'destinationDirectoryCode' => DirectoryList::VAR_DIR, ] ); } }