1
+ <?php
2
+ namespace FireGento \FastSimpleImportDemo \Console \Command \Product ;
3
+
4
+ use Magento \Framework \Component \ComponentRegistrar ;
5
+ use Magento \Framework \App \Filesystem \DirectoryList ;
6
+ use FireGento \FastSimpleImportDemo \Console \Command \AbstractImportCommand ;
7
+ use Magento \Framework \App \ObjectManagerFactory ;
8
+ use Magento \ImportExport \Model \Import ;
9
+ use League \Csv \Reader ;
10
+
11
+ class ImportCsv extends AbstractImportCommand
12
+ {
13
+ const IMPORT_FILE = "importfile.csv " ;
14
+
15
+ /**
16
+ * @var \Magento\Framework\Filesystem\Directory\ReadFactory
17
+ */
18
+ private $ readFactory ;
19
+ /**
20
+ * @var DirectoryList
21
+ */
22
+ private $ directory_list ;
23
+
24
+
25
+ /**
26
+ * Constructor
27
+ *
28
+ * @param ObjectManagerFactory $objectManagerFactory
29
+ */
30
+ public function __construct (
31
+ ObjectManagerFactory $ objectManagerFactory ,
32
+ \Magento \Framework \Filesystem \Directory \ReadFactory $ readFactory ,
33
+ \Magento \Framework \App \Filesystem \DirectoryList $ directory_list
34
+ )
35
+ {
36
+
37
+ parent ::__construct ($ objectManagerFactory );
38
+
39
+
40
+ $ this ->readFactory = $ readFactory ;
41
+
42
+ $ this ->directory_list = $ directory_list ;
43
+ }
44
+
45
+ protected function configure ()
46
+ {
47
+
48
+ $ this ->setName ('fastsimpleimportdemo:products:importcsv ' )
49
+ ->setDescription ('Import Products from CSV ' );
50
+ $ this ->setBehavior (Import::BEHAVIOR_ADD_UPDATE );
51
+ $ this ->setEntityCode ('catalog_product ' );
52
+
53
+ parent ::configure ();
54
+ }
55
+
56
+ /**
57
+ * @return array
58
+ */
59
+ protected function getEntities ()
60
+ {
61
+ $ csvIterationObject = $ this ->readCSV ();
62
+ $ data = array ();
63
+ // Do mapping here:
64
+ foreach ($ csvIterationObject as $ row ){
65
+ $ data [] = $ row ;
66
+ }
67
+ // Mapping end
68
+ //var_dump($data);
69
+ //die();
70
+ return $ data ;
71
+ }
72
+
73
+ protected function readCSV ()
74
+ {
75
+ $ csvObj = Reader::createFromString ($ this ->readFile (self ::IMPORT_FILE ));
76
+ $ csvObj ->setDelimiter (', ' );
77
+ $ results = $ csvObj ->fetchAssoc ();
78
+ return $ results ;
79
+
80
+ }
81
+
82
+ protected function readFile ($ fileName )
83
+ {
84
+ $ path = $ this ->directory_list ->getRoot ();
85
+ $ directoryRead = $ this ->readFactory ->create ($ path );
86
+ return $ directoryRead ->readFile ($ fileName );
87
+ }
88
+ }
0 commit comments