7
7
8
8
namespace Magento \ImportService \Model \Import \Processor ;
9
9
10
+ use Magento \Framework \Exception \CouldNotSaveException ;
10
11
use Magento \Framework \Filesystem \Io \File ;
12
+ use Magento \ImportService \Api \Data \SourceInterface ;
13
+ use Magento \ImportService \Api \Data \SourceUploadResponseInterface ;
14
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
15
+ use Magento \Framework \App \Filesystem \DirectoryList ;
16
+ use Magento \Framework \Exception \FileSystemException ;
17
+ use Magento \Framework \Filesystem ;
18
+ use Magento \ImportService \Api \SourceRepositoryInterface ;
19
+ use Magento \ImportService \ImportServiceException ;
20
+ use Magento \ImportService \Model \Import \SourceTypesValidatorInterface ;
11
21
12
22
/**
13
23
* CSV files processor for asynchronous import
@@ -23,27 +33,174 @@ class LocalPathFileProcessor implements SourceProcessorInterface
23
33
* CSV Source Type
24
34
*/
25
35
const SOURCE_TYPE_CSV = 'csv ' ;
36
+
37
+ /**
38
+ * @var SourceTypesValidatorInterface
39
+ */
40
+ private $ sourceTypesValidator ;
26
41
27
42
/**
28
- * @var \Magento\Framework\Filesystem\Io\ File
43
+ * @var File
29
44
*/
30
45
private $ fileSystemIo ;
31
46
32
47
/**
33
- * LocalPathFileProcessor constructor.
48
+ * @var Filesystem
49
+ */
50
+ private $ fileSystem ;
51
+
52
+ /**
53
+ * @var WriteInterface
54
+ */
55
+ private $ directoryWrite ;
56
+
57
+ /**
58
+ * @var SourceRepositoryInterface
59
+ */
60
+ private $ sourceRepository ;
61
+
62
+ /**
63
+ * @var string
64
+ */
65
+ private $ newFileName ;
66
+
67
+ /**
68
+ * @var SourceInterface
69
+ */
70
+ private $ source ;
71
+
72
+ /**
73
+ * LocalPathFileProcessor constructor
74
+ *
34
75
* @param File $fileSystemIo
76
+ * @param Filesystem $fileSystem
77
+ * @param SourceTypesValidatorInterface $sourceTypesValidator
78
+ * @param SourceRepositoryInterface $sourceRepository
35
79
*/
36
80
public function __construct (
37
- File $ fileSystemIo
81
+ File $ fileSystemIo ,
82
+ Filesystem $ fileSystem ,
83
+ SourceTypesValidatorInterface $ sourceTypesValidator ,
84
+ SourceRepositoryInterface $ sourceRepository
38
85
) {
39
86
$ this ->fileSystemIo = $ fileSystemIo ;
87
+ $ this ->sourceTypesValidator = $ sourceTypesValidator ;
88
+ $ this ->fileSystem = $ fileSystem ;
89
+ $ this ->sourceRepository = $ sourceRepository ;
40
90
}
41
91
42
92
/**
43
- * {@inheritdoc}
93
+ * Uploads process
94
+ *
95
+ * @inheritdoc
96
+ * @throws FileSystemException
97
+ * @throws ImportServiceException
44
98
*/
45
- public function processUpload (\ Magento \ ImportService \ Api \ Data \ SourceInterface $ source , \ Magento \ ImportService \ Api \ Data \ SourceUploadResponseInterface $ response )
99
+ public function processUpload (SourceInterface $ source , SourceUploadResponseInterface $ response )
46
100
{
101
+ $ this ->source = $ source ;
102
+ try {
103
+ $ this ->validateSource ();
104
+ $ this ->saveFile ();
105
+ $ source = $ this ->saveSource ();
106
+ $ response ->setStatus ($ source ->getStatus ());
107
+ $ response ->setSourceId ($ source ->getSourceId ());
108
+ } catch (CouldNotSaveException $ e ) {
109
+ $ this ->removeFile ($ source ->getImportData ());
110
+ throw new ImportServiceException (__ ($ e ->getMessage ()));
111
+ }
112
+
47
113
return $ response ;
48
114
}
115
+
116
+ /**
117
+ * Saves source in DB
118
+ *
119
+ * @return SourceInterface
120
+ */
121
+ private function saveSource ()
122
+ {
123
+ $ this ->source ->setImportData ($ this ->getNewFileName ());
124
+ $ this ->source ->setStatus (SourceInterface::STATUS_UPLOADED );
125
+
126
+ return $ this ->sourceRepository ->save ($ this ->source );
127
+ }
128
+
129
+ /**
130
+ * Saves file at the storage
131
+ *
132
+ * @return string
133
+ * @throws FileSystemException
134
+ */
135
+ private function saveFile ()
136
+ {
137
+ $ this ->directoryWrite ->copyFile (
138
+ $ this ->source ->getImportData (),
139
+ $ this ->getNewFileName ()
140
+ );
141
+
142
+ return $ this ->getNewFileName ();
143
+ }
144
+
145
+ /**
146
+ * Generates new file name
147
+ *
148
+ * @return string
149
+ */
150
+ private function getNewFileName ()
151
+ {
152
+ if (!$ this ->newFileName ) {
153
+ $ this ->newFileName = self ::IMPORT_SOURCE_FILE_PATH . '/ '
154
+ . uniqid ()
155
+ . '. ' . $ this ->source ->getSourceType ();
156
+ }
157
+
158
+ return $ this ->newFileName ;
159
+ }
160
+
161
+ /**
162
+ * Provides configured directoryWrite
163
+ *
164
+ * @return WriteInterface
165
+ * @throws FileSystemException
166
+ */
167
+ private function getDirectoryWrite ()
168
+ {
169
+ if (!$ this ->directoryWrite ) {
170
+ $ this ->directoryWrite = $ this ->fileSystem
171
+ ->getDirectoryWrite (DirectoryList::ROOT );
172
+ }
173
+
174
+ return $ this ->directoryWrite ;
175
+ }
176
+
177
+ /**
178
+ * Validates source
179
+ *
180
+ * @throws FileSystemException
181
+ * @throws ImportServiceException
182
+ */
183
+ private function validateSource ()
184
+ {
185
+ $ absoluteSourcePath = $ this ->getDirectoryWrite ()
186
+ ->getAbsolutePath ($ this ->source ->getImportData ());
187
+ if (!$ this ->fileSystemIo ->read ($ absoluteSourcePath )) {
188
+ throw new ImportServiceException (
189
+ __ ("Cannot read from file system. File not existed or cannot be read " )
190
+ );
191
+ }
192
+ $ this ->sourceTypesValidator ->execute ($ this ->source );
193
+ }
194
+
195
+ /**
196
+ * Removes source
197
+ *
198
+ * @param string $filename
199
+ * @return bool
200
+ * @throws FileSystemException
201
+ */
202
+ private function removeFile ($ filename )
203
+ {
204
+ return $ this ->getDirectoryWrite ()->delete ($ filename );
205
+ }
49
206
}
0 commit comments