Skip to content

Commit 2d33ca7

Browse files
committed
MAGETWO-95643: Import error
1 parent 2d9c1e6 commit 2d33ca7

File tree

6 files changed

+134
-48
lines changed

6 files changed

+134
-48
lines changed

app/code/Magento/ImportExport/Controller/Adminhtml/Import/Validate.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
use Magento\ImportExport\Model\Import;
1010
use Magento\ImportExport\Block\Adminhtml\Import\Frame\Result;
1111
use Magento\Framework\Controller\ResultFactory;
12-
use Magento\Framework\App\Filesystem\DirectoryList;
13-
use Magento\ImportExport\Model\Import\Adapter as ImportAdapter;
14-
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1512

1613
class Validate extends ImportResultController
1714
{
@@ -42,12 +39,7 @@ public function execute()
4239
/** @var $import \Magento\ImportExport\Model\Import */
4340
$import = $this->getImport()->setData($data);
4441
try {
45-
$source = ImportAdapter::findAdapterFor(
46-
$import->uploadSource(),
47-
$this->_objectManager->create('Magento\Framework\Filesystem')
48-
->getDirectoryWrite(DirectoryList::ROOT),
49-
$data[$import::FIELD_FIELD_SEPARATOR]
50-
);
42+
$source = $import->uploadFileAndGetSource();
5143
$this->processValidationResult($import->validateSource($source), $resultBlock);
5244
} catch (\Magento\Framework\Exception\LocalizedException $e) {
5345
$resultBlock->addError($e->getMessage());
@@ -72,6 +64,7 @@ public function execute()
7264
* @param bool $validationResult
7365
* @param Result $resultBlock
7466
* @return void
67+
* @throws \Magento\Framework\Exception\LocalizedException
7568
*/
7669
private function processValidationResult($validationResult, $resultBlock)
7770
{
@@ -128,6 +121,7 @@ private function getImport()
128121
*
129122
* @param Result $resultBlock
130123
* @return void
124+
* @throws \Magento\Framework\Exception\LocalizedException
131125
*/
132126
private function addMessageToSkipErrors(Result $resultBlock)
133127
{
@@ -148,6 +142,7 @@ private function addMessageToSkipErrors(Result $resultBlock)
148142
*
149143
* @param Result $resultBlock
150144
* @return void
145+
* @throws \Magento\Framework\Exception\LocalizedException
151146
*/
152147
private function addMessageForValidResult(Result $resultBlock)
153148
{
@@ -166,6 +161,7 @@ private function addMessageForValidResult(Result $resultBlock)
166161
*
167162
* @param Result $resultBlock
168163
* @return void
164+
* @throws \Magento\Framework\Exception\LocalizedException
169165
*/
170166
private function collectErrors(Result $resultBlock)
171167
{

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

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Framework\App\Filesystem\DirectoryList;
1212
use Magento\Framework\HTTP\Adapter\FileTransferFactory;
13+
use Magento\Framework\Stdlib\DateTime\DateTime;
1314
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
1415
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1516

@@ -78,6 +79,11 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
7879
*/
7980
const FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR = '_import_multiple_value_separator';
8081

82+
/**
83+
* Import empty attribute value constant.
84+
*/
85+
const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant';
86+
8187
/**
8288
* Allow multiple values wrapping in double quotes for additional attributes.
8389
*/
@@ -90,6 +96,11 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
9096
*/
9197
const DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR = ',';
9298

99+
/**
100+
* default empty attribute value constant
101+
*/
102+
const DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '__EMPTY__VALUE__';
103+
93104
/**#@+
94105
* Import constants
95106
*/
@@ -162,6 +173,16 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
162173
*/
163174
protected $_filesystem;
164175

176+
/**
177+
* @var History
178+
*/
179+
private $importHistoryModel;
180+
181+
/**
182+
* @var DateTime
183+
*/
184+
private $localeDate;
185+
165186
/**
166187
* @param \Psr\Log\LoggerInterface $logger
167188
* @param \Magento\Framework\Filesystem $filesystem
@@ -176,7 +197,7 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
176197
* @param Source\Import\Behavior\Factory $behaviorFactory
177198
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
178199
* @param History $importHistoryModel
179-
* @param \Magento\Framework\Stdlib\DateTime\DateTime
200+
* @param DateTime $localeDate
180201
* @param array $data
181202
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
182203
*/
@@ -194,7 +215,7 @@ public function __construct(
194215
\Magento\ImportExport\Model\Source\Import\Behavior\Factory $behaviorFactory,
195216
\Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
196217
\Magento\ImportExport\Model\History $importHistoryModel,
197-
\Magento\Framework\Stdlib\DateTime\DateTime $localeDate,
218+
DateTime $localeDate,
198219
array $data = []
199220
) {
200221
$this->_importExportData = $importExportData;
@@ -221,7 +242,6 @@ public function __construct(
221242
*/
222243
protected function _getEntityAdapter()
223244
{
224-
225245
if (!$this->_entityAdapter) {
226246
$entities = $this->_importConfig->getEntities();
227247
if (isset($entities[$this->getEntity()])) {
@@ -239,8 +259,8 @@ protected function _getEntityAdapter()
239259
throw new \Magento\Framework\Exception\LocalizedException(
240260
__(
241261
'The entity adapter object must be an instance of %1 or %2.',
242-
'Magento\ImportExport\Model\Import\Entity\AbstractEntity',
243-
'Magento\ImportExport\Model\Import\AbstractEntity'
262+
\Magento\ImportExport\Model\Import\Entity\AbstractEntity::class,
263+
\Magento\ImportExport\Model\Import\AbstractEntity::class
244264
)
245265
);
246266
}
@@ -264,6 +284,7 @@ protected function _getEntityAdapter()
264284
*
265285
* @param string $sourceFile Full path to source file
266286
* @return \Magento\ImportExport\Model\Import\AbstractSource
287+
* @throws \Magento\Framework\Exception\FileSystemException
267288
*/
268289
protected function _getSourceAdapter($sourceFile)
269290
{
@@ -279,6 +300,7 @@ protected function _getSourceAdapter($sourceFile)
279300
*
280301
* @param ProcessingErrorAggregatorInterface $validationResult
281302
* @return string[]
303+
* @throws \Magento\Framework\Exception\LocalizedException
282304
*/
283305
public function getOperationResultMessages(ProcessingErrorAggregatorInterface $validationResult)
284306
{
@@ -375,6 +397,7 @@ public function getEntity()
375397
* Returns number of checked entities.
376398
*
377399
* @return int
400+
* @throws \Magento\Framework\Exception\LocalizedException
378401
*/
379402
public function getProcessedEntitiesCount()
380403
{
@@ -385,6 +408,7 @@ public function getProcessedEntitiesCount()
385408
* Returns number of checked rows.
386409
*
387410
* @return int
411+
* @throws \Magento\Framework\Exception\LocalizedException
388412
*/
389413
public function getProcessedRowsCount()
390414
{
@@ -435,12 +459,12 @@ public function importSource()
435459
$this->importHistoryModel->invalidateReport($this);
436460
}
437461

438-
439462
return $result;
440463
}
441464

442465
/**
443466
* @return bool
467+
* @throws \Magento\Framework\Exception\LocalizedException
444468
*/
445469
protected function processImport()
446470
{
@@ -469,6 +493,7 @@ protected function processImport()
469493
* Import possibility getter.
470494
*
471495
* @return bool
496+
* @throws \Magento\Framework\Exception\LocalizedException
472497
*/
473498
public function isImportAllowed()
474499
{
@@ -537,21 +562,36 @@ public function uploadSource()
537562
}
538563
$this->_removeBom($sourceFile);
539564
$this->createHistoryReport($sourceFileRelative, $entity, $extension, $result);
540-
// trying to create source adapter for file and catch possible exception to be convinced in its adequacy
565+
566+
return $sourceFile;
567+
}
568+
569+
/**
570+
* Move uploaded file and provide source instance.
571+
*
572+
* @return Import\AbstractSource
573+
* @throws \Magento\Framework\Exception\FileSystemException
574+
* @throws \Magento\Framework\Exception\LocalizedException
575+
*/
576+
public function uploadFileAndGetSource()
577+
{
578+
$sourceFile = $this->uploadSource();
541579
try {
542-
$this->_getSourceAdapter($sourceFile);
580+
$source = $this->_getSourceAdapter($sourceFile);
543581
} catch (\Exception $e) {
544-
$this->_varDirectory->delete($sourceFileRelative);
582+
$this->_varDirectory->delete($this->_varDirectory->getRelativePath($sourceFile));
545583
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
546584
}
547-
return $sourceFile;
585+
586+
return $source;
548587
}
549588

550589
/**
551590
* Remove BOM from a file
552591
*
553592
* @param string $sourceFile
554593
* @return $this
594+
* @throws \Magento\Framework\Exception\FileSystemException
555595
*/
556596
protected function _removeBom($sourceFile)
557597
{
@@ -571,6 +611,7 @@ protected function _removeBom($sourceFile)
571611
*
572612
* @param \Magento\ImportExport\Model\Import\AbstractSource $source
573613
* @return bool
614+
* @throws \Magento\Framework\Exception\LocalizedException
574615
*/
575616
public function validateSource(\Magento\ImportExport\Model\Import\AbstractSource $source)
576617
{
@@ -610,6 +651,7 @@ public function validateSource(\Magento\ImportExport\Model\Import\AbstractSource
610651
* Invalidate indexes by process codes.
611652
*
612653
* @return $this
654+
* @throws \Magento\Framework\Exception\LocalizedException
613655
*/
614656
public function invalidateIndex()
615657
{
@@ -675,6 +717,7 @@ public function getEntityBehaviors()
675717
* )
676718
*
677719
* @return array
720+
* @throws \Magento\Framework\Exception\LocalizedException
678721
*/
679722
public function getUniqueEntityBehaviors()
680723
{
@@ -708,7 +751,9 @@ public function isReportEntityType($entity = null)
708751
try {
709752
$result = $this->_getEntityAdapter()->isNeedToLogInHistory();
710753
} catch (\Exception $e) {
711-
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity model'));
754+
throw new \Magento\Framework\Exception\LocalizedException(
755+
__('Please enter a correct entity model')
756+
);
712757
}
713758
} else {
714759
throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a correct entity model'));
@@ -720,11 +765,11 @@ public function isReportEntityType($entity = null)
720765
}
721766

722767
/**
723-
* Create history report
768+
* Create history report.
724769
*
770+
* @param string $sourceFileRelative
725771
* @param string $entity
726772
* @param string $extension
727-
* @param string $sourceFileRelative
728773
* @param array $result
729774
* @return $this
730775
* @throws \Magento\Framework\Exception\LocalizedException
@@ -737,7 +782,7 @@ protected function createHistoryReport($sourceFileRelative, $entity, $extension
737782
$sourceFileRelative = $this->_varDirectory->getRelativePath(self::IMPORT_DIR . $fileName);
738783
} elseif (isset($result['name'])) {
739784
$fileName = $result['name'];
740-
} elseif (!is_null($extension)) {
785+
} elseif ($extension !== null) {
741786
$fileName = $entity . $extension;
742787
} else {
743788
$fileName = basename($sourceFileRelative);
@@ -759,11 +804,11 @@ protected function createHistoryReport($sourceFileRelative, $entity, $extension
759804
return $this;
760805
}
761806

762-
763807
/**
764808
* Get count of created items
765809
*
766810
* @return int
811+
* @throws \Magento\Framework\Exception\LocalizedException
767812
*/
768813
public function getCreatedItemsCount()
769814
{
@@ -774,6 +819,7 @@ public function getCreatedItemsCount()
774819
* Get count of updated items
775820
*
776821
* @return int
822+
* @throws \Magento\Framework\Exception\LocalizedException
777823
*/
778824
public function getUpdatedItemsCount()
779825
{
@@ -784,6 +830,7 @@ public function getUpdatedItemsCount()
784830
* Get count of deleted items
785831
*
786832
* @return int
833+
* @throws \Magento\Framework\Exception\LocalizedException
787834
*/
788835
public function getDeletedItemsCount()
789836
{

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\ImportExport\Model\Import\Source;
77

8+
use Magento\Framework\Exception\ValidatorException;
9+
810
/**
911
* Zip import adapter.
1012
*/
@@ -14,17 +16,23 @@ class Zip extends Csv
1416
* @param string $file
1517
* @param \Magento\Framework\Filesystem\Directory\Write $directory
1618
* @param string $options
19+
* @throws \Magento\Framework\Exception\LocalizedException
20+
* @throws \Magento\Framework\Exception\ValidatorException
1721
*/
1822
public function __construct(
1923
$file,
2024
\Magento\Framework\Filesystem\Directory\Write $directory,
2125
$options
2226
) {
2327
$zip = new \Magento\Framework\Archive\Zip();
24-
$file = $zip->unpack(
25-
$directory->getRelativePath($file),
26-
$directory->getRelativePath(preg_replace('/\.zip$/i', '.csv', $file))
28+
$csvFile = $zip->unpack(
29+
$file,
30+
preg_replace('/\.zip$/i', '.csv', $file)
2731
);
28-
parent::__construct($file, $directory, $options);
32+
if (!$csvFile) {
33+
throw new ValidatorException(__('Sorry, but the data is invalid or the file is not uploaded.'));
34+
}
35+
$directory->delete($directory->getRelativePath($file));
36+
parent::__construct($csvFile, $directory, $options);
2937
}
3038
}

0 commit comments

Comments
 (0)