forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsv.php
More file actions
152 lines (131 loc) · 4.73 KB
/
Csv.php
File metadata and controls
152 lines (131 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\ImportExport\Model\Report;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\ImportExport\Model\Import;
/**
* Class Csv create new CSV file and add Error data in additional column
*/
class Csv implements ReportProcessorInterface
{
const ERROR_REPORT_FILE_SUFFIX = '_error_report';
const ERROR_REPORT_FILE_EXTENSION = '.csv';
const REPORT_ERROR_COLUMN_NAME = 'errors';
/**
* @var \Magento\ImportExport\Helper\Report
*/
protected $reportHelper;
/**
* @var \Magento\ImportExport\Model\Import\Source\CsvFactory
*/
protected $sourceCsvFactory;
/**
* @var \Magento\ImportExport\Model\Export\Adapter\CsvFactory
*/
protected $outputCsvFactory;
/**
* @var \Magento\Framework\Filesystem
*/
protected $filesystem;
/**
* @param \Magento\ImportExport\Helper\Report $reportHelper
* @param Import\Source\CsvFactory $sourceCsvFactory
* @param \Magento\ImportExport\Model\Export\Adapter\CsvFactory $outputCsvFactory
* @param \Magento\Framework\Filesystem $filesystem
*/
public function __construct(
\Magento\ImportExport\Helper\Report $reportHelper,
\Magento\ImportExport\Model\Import\Source\CsvFactory $sourceCsvFactory,
\Magento\ImportExport\Model\Export\Adapter\CsvFactory $outputCsvFactory,
\Magento\Framework\Filesystem $filesystem
) {
$this->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,
]
);
}
}