Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Csv.php
Go to the documentation of this file.
1 <?php
8 
12 
16 class Csv implements ReportProcessorInterface
17 {
18  const ERROR_REPORT_FILE_SUFFIX = '_error_report';
19 
21 
22  const REPORT_ERROR_COLUMN_NAME = 'errors';
23 
27  protected $reportHelper;
28 
32  protected $sourceCsvFactory;
33 
37  protected $outputCsvFactory;
38 
42  protected $filesystem;
43 
50  public function __construct(
51  \Magento\ImportExport\Helper\Report $reportHelper,
52  \Magento\ImportExport\Model\Import\Source\CsvFactory $sourceCsvFactory,
54  \Magento\Framework\Filesystem $filesystem
55  ) {
56  $this->reportHelper = $reportHelper;
57  $this->sourceCsvFactory = $sourceCsvFactory;
58  $this->outputCsvFactory = $outputCsvFactory;
59  $this->filesystem = $filesystem;
60  }
61 
69  public function createReport(
70  $originalFileName,
71  ProcessingErrorAggregatorInterface $errorAggregator,
72  $writeOnlyErrorItems = false
73  ) {
74  $sourceCsv = $this->createSourceCsvModel($originalFileName);
75 
76  $outputFileName = $this->generateOutputFileName($originalFileName);
77  $outputCsv = $this->createOutputCsvModel($outputFileName);
78 
79  $columnsName = $sourceCsv->getColNames();
80  $columnsName[] = self::REPORT_ERROR_COLUMN_NAME;
81  $outputCsv->setHeaderCols($columnsName);
82 
83  foreach ($sourceCsv as $rowNum => $rowData) {
84  $errorMessages = $this->retrieveErrorMessagesByRowNumber($rowNum, $errorAggregator);
85  if (!$writeOnlyErrorItems || ($writeOnlyErrorItems && $errorMessages)) {
86  $rowData[self::REPORT_ERROR_COLUMN_NAME] = $errorMessages;
87  $outputCsv->writeRow($rowData);
88  }
89  }
90 
91  return $outputFileName;
92  }
93 
99  public function retrieveErrorMessagesByRowNumber($rowNumber, ProcessingErrorAggregatorInterface $errorAggregator)
100  {
101  $messages = '';
102  foreach ($errorAggregator->getErrorByRowNumber((int)$rowNumber) as $error) {
103  $messages .= $error->getErrorMessage() . ',';
104  }
105  $messages = rtrim($messages, ',');
106 
107  if ($messages) {
108  $messages = str_pad($messages, 1, '"', STR_PAD_BOTH);
109  }
110 
111  return $messages;
112  }
113 
118  protected function generateOutputFileName($sourceFile)
119  {
120  $fileName = basename($sourceFile, self::ERROR_REPORT_FILE_EXTENSION);
121  return $fileName . self::ERROR_REPORT_FILE_SUFFIX . self::ERROR_REPORT_FILE_EXTENSION;
122  }
123 
128  protected function createSourceCsvModel($sourceFile)
129  {
130  return $this->sourceCsvFactory->create(
131  [
132  'file' => $sourceFile,
133  'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR),
134  'delimiter' => $this->reportHelper->getDelimiter(),
135  ]
136  );
137  }
138 
143  protected function createOutputCsvModel($outputFileName)
144  {
145  return $this->outputCsvFactory->create(
146  [
147  'destination' => Import::IMPORT_HISTORY_DIR . $outputFileName,
148  'destinationDirectoryCode' => DirectoryList::VAR_DIR,
149  ]
150  );
151  }
152 }
createReport( $originalFileName, ProcessingErrorAggregatorInterface $errorAggregator, $writeOnlyErrorItems=false)
Definition: Csv.php:69
createOutputCsvModel($outputFileName)
Definition: Csv.php:143
$fileName
Definition: translate.phtml:15
__construct(\Magento\ImportExport\Helper\Report $reportHelper, \Magento\ImportExport\Model\Import\Source\CsvFactory $sourceCsvFactory, \Magento\ImportExport\Model\Export\Adapter\CsvFactory $outputCsvFactory, \Magento\Framework\Filesystem $filesystem)
Definition: Csv.php:50
retrieveErrorMessagesByRowNumber($rowNumber, ProcessingErrorAggregatorInterface $errorAggregator)
Definition: Csv.php:99