Skip to content

Commit 0c0c86d

Browse files
committed
ACPT-493: Upload csv with request parameter
1 parent dd77f7e commit 0c0c86d

File tree

7 files changed

+28
-15
lines changed

7 files changed

+28
-15
lines changed

app/code/Magento/ImportExport/Model/Import.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class Import extends AbstractModel
220220
* @param DateTime $localeDate
221221
* @param array $data
222222
* @param ManagerInterface|null $messageManager
223+
* @param SourceFactory|null $sourceFactory
223224
* @param Upload|null $upload
224225
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
225226
*/
@@ -240,6 +241,7 @@ public function __construct(
240241
DateTime $localeDate,
241242
array $data = [],
242243
ManagerInterface $messageManager = null,
244+
SourceFactory $sourceFactory = null,
243245
Upload $upload = null
244246
) {
245247
$this->_importExportData = $importExportData;
@@ -257,6 +259,8 @@ public function __construct(
257259
$this->localeDate = $localeDate;
258260
$this->messageManager = $messageManager ?: ObjectManager::getInstance()
259261
->get(ManagerInterface::class);
262+
$this->sourceFactory = $sourceFactory?? ObjectManager::getInstance()
263+
->get(SourceFactory::class);
260264
$this->upload = $upload ?: ObjectManager::getInstance()
261265
->get(Upload::class);
262266
parent::__construct($logger, $filesystem, $data);
@@ -309,7 +313,8 @@ protected function _getEntityAdapter()
309313

310314
/**
311315
* Returns source adapter object.
312-
*
316+
* @Deprecated
317+
* @see \Magento\ImportExport\Model\Import\Source\Factory::create()
313318
* @param string $sourceFile Full path to source file
314319
* @return AbstractSource
315320
* @throws FileSystemException
@@ -579,7 +584,11 @@ public function uploadFileAndGetSource()
579584
{
580585
$sourceFile = $this->uploadSource();
581586
try {
582-
$source = $this->_getSourceAdapter($sourceFile);
587+
$source = $this->sourceFactory->create(
588+
$sourceFile,
589+
$this->_filesystem->getDirectoryWrite(DirectoryList::ROOT),
590+
$this->getData(self::FIELD_FIELD_SEPARATOR)
591+
);
583592
} catch (\Exception $e) {
584593
$this->_varDirectory->delete($this->_varDirectory->getRelativePath($sourceFile));
585594
throw new LocalizedException(__($e->getMessage()));

app/code/Magento/ImportExport/Model/Import/Source/Base64EncodedCsvData.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class Base64EncodedCsvData extends AbstractSource
3131
/**
3232
* Read Data and detect column names
3333
*
34-
* @param string $source
34+
* @param string $file
3535
*/
36-
public function __construct(string $source)
36+
public function __construct(string $file)
3737
{
3838
// phpcs:ignore Magento2.Functions.DiscouragedFunction
39-
$source = trim(base64_decode($source));
39+
$source = trim(base64_decode($file));
4040
$rowsData = preg_split("/\r\n|\n|\r/", $source);
4141
$colNames = explode(',', $rowsData[0]);
4242
$this->rows = array_splice($rowsData, 1);

app/code/Magento/ImportExport/Model/Import/Source/Csv.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,27 @@ class Csv extends \Magento\ImportExport\Model\Import\AbstractSource
4242
*
4343
* There must be column names in the first line
4444
*
45-
* @param string $source
45+
* @param string $file
4646
* @param Read $directory
4747
* @param string $delimiter
4848
* @param string $enclosure
4949
* @throws \LogicException
5050
*/
5151
public function __construct(
52-
$source,
52+
$file,
5353
Read $directory,
5454
$delimiter = ',',
5555
$enclosure = '"'
5656
) {
5757
// phpcs:ignore Magento2.Functions.DiscouragedFunction
5858
register_shutdown_function([$this, 'destruct']);
5959
try {
60-
$this->filePath = $directory->getRelativePath($source);
60+
$this->filePath = $directory->getRelativePath($file);
6161
$this->_file = $directory->openFile($this->filePath, 'r');
6262
$this->_file->seek(0);
6363
self::$openFiles[$this->filePath] = true;
6464
} catch (\Magento\Framework\Exception\FileSystemException $e) {
65-
throw new \LogicException("Unable to open file: '{$source}'");
65+
throw new \LogicException("Unable to open file: '{$file}'");
6666
}
6767
if ($delimiter) {
6868
$this->_delimiter = $delimiter;

app/code/Magento/ImportExport/Model/Import/Source/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function create($source, $directory = null, $options = null): AbstractSou
5555
return $this->objectManager->create(
5656
$adapterClass,
5757
[
58-
'source' => $source,
58+
'file' => $source,
5959
'directory' => $directory,
6060
'options' => $options
6161
]

app/code/Magento/ImportExport/Model/Import/Source/Zip.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,28 @@
1414
class Zip extends Csv
1515
{
1616
/**
17-
* @param string $source
17+
* @param string $file
1818
* @param \Magento\Framework\Filesystem\Directory\Write $directory
1919
* @param string $options
2020
* @param \Magento\Framework\Archive\Zip|null $zipArchive
2121
* @throws \Magento\Framework\Exception\LocalizedException
2222
* @throws \Magento\Framework\Exception\ValidatorException
2323
*/
2424
public function __construct(
25-
$source,
25+
$file,
2626
\Magento\Framework\Filesystem\Directory\Write $directory,
2727
$options,
2828
\Magento\Framework\Archive\Zip $zipArchive = null
2929
) {
3030
$zip = $zipArchive ?? ObjectManager::getInstance()->get(\Magento\Framework\Archive\Zip::class);
3131
$csvFile = $zip->unpack(
32-
$source,
33-
preg_replace('/\.zip$/i', '.csv', $source)
32+
$file,
33+
preg_replace('/\.zip$/i', '.csv', $file)
3434
);
3535
if (!$csvFile) {
3636
throw new ValidatorException(__('Sorry, but the data is invalid or the file is not uploaded.'));
3737
}
38-
$directory->delete($directory->getRelativePath($source));
38+
$directory->delete($directory->getRelativePath($file));
3939

4040
try {
4141
parent::__construct($csvFile, $directory, $options);

app/code/Magento/ImportExport/Test/Unit/Helper/ReportTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ public function testGetSummaryStats()
204204
$importHistoryModel = $this->createMock(History::class);
205205
$localeDate = $this->createMock(\Magento\Framework\Stdlib\DateTime\DateTime::class);
206206
$upload = $this->createMock(Upload::class);
207+
$sourceFactoryMock = $this->createMock(\Magento\ImportExport\Model\Import\Source\Factory::class);
207208
$import = new Import(
208209
$logger,
209210
$filesystem,
@@ -221,6 +222,7 @@ public function testGetSummaryStats()
221222
$localeDate,
222223
[],
223224
null,
225+
$sourceFactoryMock,
224226
$upload
225227
);
226228
$import->setData('entity', 'catalog_product');

app/code/Magento/ImportExport/Test/Unit/Model/ImportTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ protected function setUp(): void
231231
->expects($this->any())
232232
->method('getDriver')
233233
->willReturn($this->_driver);
234+
$sourceFactoryMock = $this->createMock(\Magento\ImportExport\Model\Import\Source\Factory::class);
234235
$this->upload = $this->createMock(Upload::class);
235236
$this->import = $this->getMockBuilder(Import::class)
236237
->setConstructorArgs(
@@ -251,6 +252,7 @@ protected function setUp(): void
251252
$this->dateTime,
252253
[],
253254
null,
255+
$sourceFactoryMock,
254256
$this->upload
255257
]
256258
)

0 commit comments

Comments
 (0)