Skip to content

Commit 601ccea

Browse files
committed
ACPT-493: Upload csv with request parameter
1 parent a5e2370 commit 601ccea

File tree

9 files changed

+112
-57
lines changed

9 files changed

+112
-57
lines changed

app/code/Magento/ImportCsv/Model/StartImport.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\ImportCsvApi\Api\Data\SourceDataInterface;
1111
use Magento\ImportCsvApi\Api\StartImportInterface;
1212
use Magento\ImportExport\Model\Import;
13+
use Magento\ImportExport\Model\Import\Source\Factory as SourceFactory;
1314

1415
/**
1516
* @inheritdoc
@@ -22,13 +23,21 @@ class StartImport implements StartImportInterface
2223
*/
2324
private $import;
2425

26+
/**
27+
* @var SourceFactory
28+
*/
29+
private $sourceFactory;
30+
2531
/**
2632
* @param Import $import
33+
* @param SourceFactory $sourceFactory
2734
*/
2835
public function __construct(
29-
Import $import
36+
Import $import,
37+
SourceFactory $sourceFactory
3038
) {
3139
$this->import = $import;
40+
$this->sourceFactory = $sourceFactory;
3241
}
3342

3443
/**
@@ -41,7 +50,7 @@ public function execute(
4150
$import = $this->import->setData($source);
4251
$errors = [];
4352
try {
44-
$source = $import->getSourceForApiData();
53+
$source = $this->sourceFactory->create($import->getData('csvData'));
4554
$this->processValidationResult($import->validateSource($source), $errors);
4655
} catch (\Magento\Framework\Exception\LocalizedException $e) {
4756
$errors[] = $e->getMessage();

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Magento\Framework\Filesystem;
1818
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
1919
use Magento\Framework\Indexer\IndexerRegistry;
20-
use Magento\Framework\Math\Random;
2120
use Magento\Framework\Message\ManagerInterface;
2221
use Magento\Framework\Stdlib\DateTime\DateTime;
2322
use Magento\ImportExport\Helper\Data as DataHelper;
@@ -30,6 +29,7 @@
3029
use Magento\ImportExport\Model\Import\Entity\Factory;
3130
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
3231
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
32+
use Magento\ImportExport\Model\Import\Source\Factory as SourceFactory;
3333
use Magento\ImportExport\Model\ResourceModel\Import\Data;
3434
use Magento\ImportExport\Model\Source\Import\AbstractBehavior;
3535
use Magento\ImportExport\Model\Source\Import\Behavior\Factory as BehaviorFactory;
@@ -193,6 +193,11 @@ class Import extends AbstractModel
193193
*/
194194
private $messageManager;
195195

196+
/**
197+
* @var SourceFactory
198+
*/
199+
private $sourceFactory;
200+
196201
/**
197202
* @var Upload
198203
*/
@@ -215,6 +220,7 @@ class Import extends AbstractModel
215220
* @param DateTime $localeDate
216221
* @param array $data
217222
* @param ManagerInterface|null $messageManager
223+
* @param SourceFactory $sourceFactory
218224
* @param Upload|null $upload
219225
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
220226
*/
@@ -235,6 +241,7 @@ public function __construct(
235241
DateTime $localeDate,
236242
array $data = [],
237243
ManagerInterface $messageManager = null,
244+
SourceFactory $sourceFactory = null,
238245
Upload $upload = null
239246
) {
240247
$this->_importExportData = $importExportData;
@@ -252,6 +259,8 @@ public function __construct(
252259
$this->localeDate = $localeDate;
253260
$this->messageManager = $messageManager ?: ObjectManager::getInstance()
254261
->get(ManagerInterface::class);
262+
$this->sourceFactory = $sourceFactory?? ObjectManager::getInstance()
263+
->get(SourceFactory::class);
255264
$this->upload = $upload ?: ObjectManager::getInstance()
256265
->get(Upload::class);
257266
parent::__construct($logger, $filesystem, $data);
@@ -304,7 +313,8 @@ protected function _getEntityAdapter()
304313

305314
/**
306315
* Returns source adapter object.
307-
*
316+
* @Deprecated
317+
* @see \Magento\ImportExport\Model\Import\Source\Factory::create()
308318
* @param string $sourceFile Full path to source file
309319
* @return AbstractSource
310320
* @throws FileSystemException
@@ -318,20 +328,6 @@ protected function _getSourceAdapter($sourceFile)
318328
);
319329
}
320330

321-
/**
322-
* Returns source adapter object for Api Data.
323-
*
324-
* @return AbstractSource
325-
*/
326-
protected function _getSourceAdapterForApi()
327-
{
328-
return Adapter::findAdapterForData(
329-
// phpcs:ignore Magento2.Functions.DiscouragedFunction
330-
trim(base64_decode($this->getData('csvData'))),
331-
$this->getData(self::FIELD_FIELD_SEPARATOR)
332-
);
333-
}
334-
335331
/**
336332
* Return operation result messages
337333
*
@@ -588,7 +584,11 @@ public function uploadFileAndGetSource()
588584
{
589585
$sourceFile = $this->uploadSource();
590586
try {
591-
$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+
);
592592
} catch (\Exception $e) {
593593
$this->_varDirectory->delete($this->_varDirectory->getRelativePath($sourceFile));
594594
throw new LocalizedException(__($e->getMessage()));
@@ -597,16 +597,6 @@ public function uploadFileAndGetSource()
597597
return $source;
598598
}
599599

600-
/**
601-
* Get Source adapter object
602-
*
603-
* @return AbstractSource
604-
*/
605-
public function getSourceForApiData()
606-
{
607-
return $this->_getSourceAdapterForApi();
608-
}
609-
610600
/**
611601
* Get Var Directory instance
612602
*

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
namespace Magento\ImportExport\Model\Import;
77

88
use Magento\Framework\Filesystem\Directory\Write;
9+
use Magento\ImportExport\Model\Import\Source\Factory;
910

1011
/**
1112
* Import adapter model
12-
*
13+
* @Deprecated
14+
* @see \Magento\ImportExport\Model\Import\Source\Factory
1315
* @author Magento Core Team <[email protected]>
1416
*/
1517
class Adapter
@@ -63,17 +65,4 @@ public static function findAdapterFor($source, $directory, $options = null)
6365
{
6466
return self::factory(pathinfo($source, PATHINFO_EXTENSION), $directory, $source, $options); // phpcs:ignore
6567
}
66-
67-
/**
68-
* Create adapter instance for specified source.
69-
*
70-
* @param string $source Source
71-
* @param mixed $options OPTIONAL Adapter constructor options
72-
* phpcs:disable Magento2.Functions.StaticFunction
73-
* @return AbstractSource
74-
*/
75-
public static function findAdapterForData($source, $options = null)
76-
{
77-
return self::factory('Base64EncodedCsvData', null, $source, $options);
78-
}
7968
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ class Base64EncodedCsvData extends AbstractSource
3131
/**
3232
* Read Data and detect column names
3333
*
34-
* @param string $data
34+
* @param string $source
3535
*/
36-
public function __construct(string $data)
36+
public function __construct(string $source)
3737
{
38-
$rowsData = preg_split("/\r\n|\n|\r/", $data);
38+
$source = trim(base64_decode($source));
39+
$rowsData = preg_split("/\r\n|\n|\r/", $source);
3940
$colNames = explode(',', $rowsData[0]);
4041
$this->rows = array_splice($rowsData, 1);
4142
parent::__construct($colNames);

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 $file
45+
* @param string $source
4646
* @param Read $directory
4747
* @param string $delimiter
4848
* @param string $enclosure
4949
* @throws \LogicException
5050
*/
5151
public function __construct(
52-
$file,
52+
$source,
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($file);
60+
$this->filePath = $directory->getRelativePath($source);
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: '{$file}'");
65+
throw new \LogicException("Unable to open file: '{$source}'");
6666
}
6767
if ($delimiter) {
6868
$this->_delimiter = $delimiter;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\ImportExport\Model\Import\Source;
10+
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\ImportExport\Model\Import\AbstractSource;
13+
14+
class Factory
15+
{
16+
/**
17+
* Object Manager
18+
*
19+
* @var \Magento\Framework\ObjectManagerInterface
20+
*/
21+
private $objectManager;
22+
23+
public function __construct(
24+
ObjectManagerInterface $objectManager
25+
) {
26+
$this->objectManager = $objectManager;
27+
}
28+
29+
/**
30+
* @param $source
31+
* @param $directory
32+
* @param $options
33+
* @return AbstractSource
34+
* @throws \Magento\Framework\Exception\LocalizedException
35+
*/
36+
public function create($source, $directory = null, $options = null): AbstractSource
37+
{
38+
$adapterClass = 'Magento\ImportExport\Model\Import\Source\\';
39+
if (file_exists($source)) {
40+
$type = ucfirst(strtolower(pathinfo($source, PATHINFO_EXTENSION)));
41+
} else {
42+
$type = 'Base64EncodedCsvData';
43+
}
44+
if (!is_string($source) || !$source) {
45+
throw new \Magento\Framework\Exception\LocalizedException(
46+
__('The source type must be a non-empty string.')
47+
);
48+
}
49+
$adapterClass.= $type;
50+
if (!class_exists($adapterClass)) {
51+
throw new \Magento\Framework\Exception\LocalizedException(
52+
__('\'%1\' file extension is not supported', $type)
53+
);
54+
}
55+
return $this->objectManager->create(
56+
$adapterClass,
57+
[
58+
'source' => $source,
59+
'directory' => $directory,
60+
'options' => $options
61+
]
62+
);
63+
}
64+
}

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 $file
17+
* @param string $source
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-
$file,
25+
$source,
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-
$file,
33-
preg_replace('/\.zip$/i', '.csv', $file)
32+
$source,
33+
preg_replace('/\.zip$/i', '.csv', $source)
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($file));
38+
$directory->delete($directory->getRelativePath($source));
3939

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

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

Lines changed: 2 additions & 1 deletion
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,7 +222,7 @@ public function testGetSummaryStats()
221222
$localeDate,
222223
[],
223224
null,
224-
null,
225+
$sourceFactoryMock,
225226
$upload
226227
);
227228
$import->setData('entity', 'catalog_product');

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

Lines changed: 2 additions & 1 deletion
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,7 +252,7 @@ protected function setUp(): void
251252
$this->dateTime,
252253
[],
253254
null,
254-
null,
255+
$sourceFactoryMock,
255256
$this->upload
256257
]
257258
)

0 commit comments

Comments
 (0)