Csv.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Model\Report;
  7. use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\ImportExport\Model\Import;
  10. /**
  11. * Class Csv create new CSV file and add Error data in additional column
  12. */
  13. class Csv implements ReportProcessorInterface
  14. {
  15. const ERROR_REPORT_FILE_SUFFIX = '_error_report';
  16. const ERROR_REPORT_FILE_EXTENSION = '.csv';
  17. const REPORT_ERROR_COLUMN_NAME = 'errors';
  18. /**
  19. * @var \Magento\ImportExport\Helper\Report
  20. */
  21. protected $reportHelper;
  22. /**
  23. * @var \Magento\ImportExport\Model\Import\Source\CsvFactory
  24. */
  25. protected $sourceCsvFactory;
  26. /**
  27. * @var \Magento\ImportExport\Model\Export\Adapter\CsvFactory
  28. */
  29. protected $outputCsvFactory;
  30. /**
  31. * @var \Magento\Framework\Filesystem
  32. */
  33. protected $filesystem;
  34. /**
  35. * @param \Magento\ImportExport\Helper\Report $reportHelper
  36. * @param Import\Source\CsvFactory $sourceCsvFactory
  37. * @param \Magento\ImportExport\Model\Export\Adapter\CsvFactory $outputCsvFactory
  38. * @param \Magento\Framework\Filesystem $filesystem
  39. */
  40. public function __construct(
  41. \Magento\ImportExport\Helper\Report $reportHelper,
  42. \Magento\ImportExport\Model\Import\Source\CsvFactory $sourceCsvFactory,
  43. \Magento\ImportExport\Model\Export\Adapter\CsvFactory $outputCsvFactory,
  44. \Magento\Framework\Filesystem $filesystem
  45. ) {
  46. $this->reportHelper = $reportHelper;
  47. $this->sourceCsvFactory = $sourceCsvFactory;
  48. $this->outputCsvFactory = $outputCsvFactory;
  49. $this->filesystem = $filesystem;
  50. }
  51. /**
  52. * @param string $originalFileName
  53. * @param ProcessingErrorAggregatorInterface $errorAggregator
  54. * @param bool $writeOnlyErrorItems
  55. * @return string
  56. * @throws \Magento\Framework\Exception\LocalizedException
  57. */
  58. public function createReport(
  59. $originalFileName,
  60. ProcessingErrorAggregatorInterface $errorAggregator,
  61. $writeOnlyErrorItems = false
  62. ) {
  63. $sourceCsv = $this->createSourceCsvModel($originalFileName);
  64. $outputFileName = $this->generateOutputFileName($originalFileName);
  65. $outputCsv = $this->createOutputCsvModel($outputFileName);
  66. $columnsName = $sourceCsv->getColNames();
  67. $columnsName[] = self::REPORT_ERROR_COLUMN_NAME;
  68. $outputCsv->setHeaderCols($columnsName);
  69. foreach ($sourceCsv as $rowNum => $rowData) {
  70. $errorMessages = $this->retrieveErrorMessagesByRowNumber($rowNum, $errorAggregator);
  71. if (!$writeOnlyErrorItems || ($writeOnlyErrorItems && $errorMessages)) {
  72. $rowData[self::REPORT_ERROR_COLUMN_NAME] = $errorMessages;
  73. $outputCsv->writeRow($rowData);
  74. }
  75. }
  76. return $outputFileName;
  77. }
  78. /**
  79. * @param int $rowNumber
  80. * @param ProcessingErrorAggregatorInterface $errorAggregator
  81. * @return string
  82. */
  83. public function retrieveErrorMessagesByRowNumber($rowNumber, ProcessingErrorAggregatorInterface $errorAggregator)
  84. {
  85. $messages = '';
  86. foreach ($errorAggregator->getErrorByRowNumber((int)$rowNumber) as $error) {
  87. $messages .= $error->getErrorMessage() . ',';
  88. }
  89. $messages = rtrim($messages, ',');
  90. if ($messages) {
  91. $messages = str_pad($messages, 1, '"', STR_PAD_BOTH);
  92. }
  93. return $messages;
  94. }
  95. /**
  96. * @param string $sourceFile
  97. * @return string
  98. */
  99. protected function generateOutputFileName($sourceFile)
  100. {
  101. $fileName = basename($sourceFile, self::ERROR_REPORT_FILE_EXTENSION);
  102. return $fileName . self::ERROR_REPORT_FILE_SUFFIX . self::ERROR_REPORT_FILE_EXTENSION;
  103. }
  104. /**
  105. * @param string $sourceFile
  106. * @return \Magento\ImportExport\Model\Import\Source\Csv
  107. */
  108. protected function createSourceCsvModel($sourceFile)
  109. {
  110. return $this->sourceCsvFactory->create(
  111. [
  112. 'file' => $sourceFile,
  113. 'directory' => $this->filesystem->getDirectoryWrite(DirectoryList::VAR_DIR),
  114. 'delimiter' => $this->reportHelper->getDelimiter(),
  115. ]
  116. );
  117. }
  118. /**
  119. * @param string $outputFileName
  120. * @return \Magento\ImportExport\Model\Export\Adapter\Csv
  121. */
  122. protected function createOutputCsvModel($outputFileName)
  123. {
  124. return $this->outputCsvFactory->create(
  125. [
  126. 'destination' => Import::IMPORT_HISTORY_DIR . $outputFileName,
  127. 'destinationDirectoryCode' => DirectoryList::VAR_DIR,
  128. ]
  129. );
  130. }
  131. }