10
10
11
11
use Magento \Framework \App \Filesystem \DirectoryList ;
12
12
use Magento \Framework \HTTP \Adapter \FileTransferFactory ;
13
+ use Magento \Framework \Stdlib \DateTime \DateTime ;
13
14
use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingError ;
14
15
use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
15
16
@@ -78,6 +79,11 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
78
79
*/
79
80
const FIELD_FIELD_MULTIPLE_VALUE_SEPARATOR = '_import_multiple_value_separator ' ;
80
81
82
+ /**
83
+ * Import empty attribute value constant.
84
+ */
85
+ const FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '_import_empty_attribute_value_constant ' ;
86
+
81
87
/**
82
88
* Allow multiple values wrapping in double quotes for additional attributes.
83
89
*/
@@ -90,6 +96,11 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
90
96
*/
91
97
const DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR = ', ' ;
92
98
99
+ /**
100
+ * default empty attribute value constant
101
+ */
102
+ const DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT = '__EMPTY__VALUE__ ' ;
103
+
93
104
/**#@+
94
105
* Import constants
95
106
*/
@@ -162,6 +173,16 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
162
173
*/
163
174
protected $ _filesystem ;
164
175
176
+ /**
177
+ * @var History
178
+ */
179
+ private $ importHistoryModel ;
180
+
181
+ /**
182
+ * @var DateTime
183
+ */
184
+ private $ localeDate ;
185
+
165
186
/**
166
187
* @param \Psr\Log\LoggerInterface $logger
167
188
* @param \Magento\Framework\Filesystem $filesystem
@@ -176,7 +197,7 @@ class Import extends \Magento\ImportExport\Model\AbstractModel
176
197
* @param Source\Import\Behavior\Factory $behaviorFactory
177
198
* @param \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry
178
199
* @param History $importHistoryModel
179
- * @param \Magento\Framework\Stdlib\ DateTime\DateTime
200
+ * @param DateTime $localeDate
180
201
* @param array $data
181
202
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
182
203
*/
@@ -194,7 +215,7 @@ public function __construct(
194
215
\Magento \ImportExport \Model \Source \Import \Behavior \Factory $ behaviorFactory ,
195
216
\Magento \Framework \Indexer \IndexerRegistry $ indexerRegistry ,
196
217
\Magento \ImportExport \Model \History $ importHistoryModel ,
197
- \ Magento \ Framework \ Stdlib \ DateTime \ DateTime $ localeDate ,
218
+ DateTime $ localeDate ,
198
219
array $ data = []
199
220
) {
200
221
$ this ->_importExportData = $ importExportData ;
@@ -221,7 +242,6 @@ public function __construct(
221
242
*/
222
243
protected function _getEntityAdapter ()
223
244
{
224
-
225
245
if (!$ this ->_entityAdapter ) {
226
246
$ entities = $ this ->_importConfig ->getEntities ();
227
247
if (isset ($ entities [$ this ->getEntity ()])) {
@@ -239,8 +259,8 @@ protected function _getEntityAdapter()
239
259
throw new \Magento \Framework \Exception \LocalizedException (
240
260
__ (
241
261
'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
244
264
)
245
265
);
246
266
}
@@ -264,6 +284,7 @@ protected function _getEntityAdapter()
264
284
*
265
285
* @param string $sourceFile Full path to source file
266
286
* @return \Magento\ImportExport\Model\Import\AbstractSource
287
+ * @throws \Magento\Framework\Exception\FileSystemException
267
288
*/
268
289
protected function _getSourceAdapter ($ sourceFile )
269
290
{
@@ -279,6 +300,7 @@ protected function _getSourceAdapter($sourceFile)
279
300
*
280
301
* @param ProcessingErrorAggregatorInterface $validationResult
281
302
* @return string[]
303
+ * @throws \Magento\Framework\Exception\LocalizedException
282
304
*/
283
305
public function getOperationResultMessages (ProcessingErrorAggregatorInterface $ validationResult )
284
306
{
@@ -375,6 +397,7 @@ public function getEntity()
375
397
* Returns number of checked entities.
376
398
*
377
399
* @return int
400
+ * @throws \Magento\Framework\Exception\LocalizedException
378
401
*/
379
402
public function getProcessedEntitiesCount ()
380
403
{
@@ -385,6 +408,7 @@ public function getProcessedEntitiesCount()
385
408
* Returns number of checked rows.
386
409
*
387
410
* @return int
411
+ * @throws \Magento\Framework\Exception\LocalizedException
388
412
*/
389
413
public function getProcessedRowsCount ()
390
414
{
@@ -435,12 +459,12 @@ public function importSource()
435
459
$ this ->importHistoryModel ->invalidateReport ($ this );
436
460
}
437
461
438
-
439
462
return $ result ;
440
463
}
441
464
442
465
/**
443
466
* @return bool
467
+ * @throws \Magento\Framework\Exception\LocalizedException
444
468
*/
445
469
protected function processImport ()
446
470
{
@@ -469,6 +493,7 @@ protected function processImport()
469
493
* Import possibility getter.
470
494
*
471
495
* @return bool
496
+ * @throws \Magento\Framework\Exception\LocalizedException
472
497
*/
473
498
public function isImportAllowed ()
474
499
{
@@ -537,21 +562,36 @@ public function uploadSource()
537
562
}
538
563
$ this ->_removeBom ($ sourceFile );
539
564
$ 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 ();
541
579
try {
542
- $ this ->_getSourceAdapter ($ sourceFile );
580
+ $ source = $ this ->_getSourceAdapter ($ sourceFile );
543
581
} catch (\Exception $ e ) {
544
- $ this ->_varDirectory ->delete ($ sourceFileRelative );
582
+ $ this ->_varDirectory ->delete ($ this -> _varDirectory -> getRelativePath ( $ sourceFile ) );
545
583
throw new \Magento \Framework \Exception \LocalizedException (__ ($ e ->getMessage ()));
546
584
}
547
- return $ sourceFile ;
585
+
586
+ return $ source ;
548
587
}
549
588
550
589
/**
551
590
* Remove BOM from a file
552
591
*
553
592
* @param string $sourceFile
554
593
* @return $this
594
+ * @throws \Magento\Framework\Exception\FileSystemException
555
595
*/
556
596
protected function _removeBom ($ sourceFile )
557
597
{
@@ -571,6 +611,7 @@ protected function _removeBom($sourceFile)
571
611
*
572
612
* @param \Magento\ImportExport\Model\Import\AbstractSource $source
573
613
* @return bool
614
+ * @throws \Magento\Framework\Exception\LocalizedException
574
615
*/
575
616
public function validateSource (\Magento \ImportExport \Model \Import \AbstractSource $ source )
576
617
{
@@ -610,6 +651,7 @@ public function validateSource(\Magento\ImportExport\Model\Import\AbstractSource
610
651
* Invalidate indexes by process codes.
611
652
*
612
653
* @return $this
654
+ * @throws \Magento\Framework\Exception\LocalizedException
613
655
*/
614
656
public function invalidateIndex ()
615
657
{
@@ -675,6 +717,7 @@ public function getEntityBehaviors()
675
717
* )
676
718
*
677
719
* @return array
720
+ * @throws \Magento\Framework\Exception\LocalizedException
678
721
*/
679
722
public function getUniqueEntityBehaviors ()
680
723
{
@@ -708,7 +751,9 @@ public function isReportEntityType($entity = null)
708
751
try {
709
752
$ result = $ this ->_getEntityAdapter ()->isNeedToLogInHistory ();
710
753
} 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
+ );
712
757
}
713
758
} else {
714
759
throw new \Magento \Framework \Exception \LocalizedException (__ ('Please enter a correct entity model ' ));
@@ -720,11 +765,11 @@ public function isReportEntityType($entity = null)
720
765
}
721
766
722
767
/**
723
- * Create history report
768
+ * Create history report.
724
769
*
770
+ * @param string $sourceFileRelative
725
771
* @param string $entity
726
772
* @param string $extension
727
- * @param string $sourceFileRelative
728
773
* @param array $result
729
774
* @return $this
730
775
* @throws \Magento\Framework\Exception\LocalizedException
@@ -737,7 +782,7 @@ protected function createHistoryReport($sourceFileRelative, $entity, $extension
737
782
$ sourceFileRelative = $ this ->_varDirectory ->getRelativePath (self ::IMPORT_DIR . $ fileName );
738
783
} elseif (isset ($ result ['name ' ])) {
739
784
$ fileName = $ result ['name ' ];
740
- } elseif (! is_null ( $ extension) ) {
785
+ } elseif ($ extension !== null ) {
741
786
$ fileName = $ entity . $ extension ;
742
787
} else {
743
788
$ fileName = basename ($ sourceFileRelative );
@@ -759,11 +804,11 @@ protected function createHistoryReport($sourceFileRelative, $entity, $extension
759
804
return $ this ;
760
805
}
761
806
762
-
763
807
/**
764
808
* Get count of created items
765
809
*
766
810
* @return int
811
+ * @throws \Magento\Framework\Exception\LocalizedException
767
812
*/
768
813
public function getCreatedItemsCount ()
769
814
{
@@ -774,6 +819,7 @@ public function getCreatedItemsCount()
774
819
* Get count of updated items
775
820
*
776
821
* @return int
822
+ * @throws \Magento\Framework\Exception\LocalizedException
777
823
*/
778
824
public function getUpdatedItemsCount ()
779
825
{
@@ -784,6 +830,7 @@ public function getUpdatedItemsCount()
784
830
* Get count of deleted items
785
831
*
786
832
* @return int
833
+ * @throws \Magento\Framework\Exception\LocalizedException
787
834
*/
788
835
public function getDeletedItemsCount ()
789
836
{
0 commit comments