@@ -74,11 +74,13 @@ protected function getFiles(string $directory): array
7474 $ files = array_diff ($ array , $ ignore );
7575 $ return = [];
7676 foreach ($ files as $ file ) {
77- // import CSV file with JSON companion
77+ // import importable file with JSON companion
78+ // TODO may also need to be able to detect other file types. Or: detect any file with accompanying json file
7879 if ('csv ' === $ this ->getExtension ($ file ) && $ this ->hasJsonConfiguration ($ directory , $ file )) {
7980 $ return [] = $ file ;
8081 }
81- // import JSON with no CSV
82+ // import JSON with no importable file.
83+ // TODO must detect json files without accompanying camt/csv/whatever file.
8284 if ('json ' === $ this ->getExtension ($ file ) && !$ this ->hasCsvFile ($ directory , $ file )) {
8385 $ return [] = $ file ;
8486 }
@@ -123,6 +125,8 @@ private function hasJsonConfiguration(string $directory, string $file): bool
123125 }
124126
125127 /**
128+ * TODO this function must be more universal.
129+ *
126130 * @param string $directory
127131 * @param string $file
128132 *
@@ -163,41 +167,42 @@ private function importFile(string $directory, string $file): void
163167 {
164168 app ('log ' )->debug (sprintf ('ImportFile: directory "%s" ' , $ directory ));
165169 app ('log ' )->debug (sprintf ('ImportFile: file "%s" ' , $ file ));
166- $ csvFile = sprintf ('%s/%s ' , $ directory , $ file );
170+ $ importableFile = sprintf ('%s/%s ' , $ directory , $ file );
167171 $ jsonFile = sprintf ('%s/%s.json ' , $ directory , substr ($ file , 0 , -5 ));
168172
169173 // TODO not yet sure why the distinction is necessary.
174+ // TODO this may also be necessary for camt files.
170175 if ('csv ' === $ this ->getExtension ($ file )) {
171176 $ jsonFile = sprintf ('%s/%s.json ' , $ directory , substr ($ file , 0 , -4 ));
172177 }
173178
174- app ('log ' )->debug (sprintf ('ImportFile: CSV "%s" ' , $ csvFile ));
175- app ('log ' )->debug (sprintf ('ImportFile: JSON "%s" ' , $ jsonFile ));
179+ app ('log ' )->debug (sprintf ('ImportFile: importable "%s" ' , $ importableFile ));
180+ app ('log ' )->debug (sprintf ('ImportFile: JSON "%s" ' , $ jsonFile ));
176181
177182 // do JSON check
178183 $ jsonResult = $ this ->verifyJSON ($ jsonFile );
179184 if (false === $ jsonResult ) {
180- $ message = sprintf ('The importer can \'t import %s: could not decode the JSON in config file %s. ' , $ csvFile , $ jsonFile );
185+ $ message = sprintf ('The importer can \'t import %s: could not decode the JSON in config file %s. ' , $ importableFile , $ jsonFile );
181186 $ this ->error ($ message );
182187
183188 return ;
184189 }
185190 $ configuration = Configuration::fromArray (json_decode (file_get_contents ($ jsonFile ), true ));
186191
187- // sanity check. If the csvFile is a .json file and it parses as valid json, don't import it:
188- if ('csv ' === $ configuration ->getFlow () && str_ends_with (strtolower ($ csvFile ), '.json ' ) && $ this ->verifyJSON ($ csvFile )) {
189- app ('log ' )->warning ('Almost tried to import a JSON file as CSV lol. Skip it. ' );
192+ // sanity check. If the importableFile is a .json file and it parses as valid json, don't import it:
193+ if ('file ' === $ configuration ->getFlow () && str_ends_with (strtolower ($ importableFile ), '.json ' ) && $ this ->verifyJSON ($ importableFile )) {
194+ app ('log ' )->warning ('Almost tried to import a JSON file as a file lol. Skip it. ' );
190195 return ;
191196 }
192197
193198 $ configuration ->updateDateRange ();
194- $ this ->line (sprintf ('Going to convert from file %s using configuration %s and flow "%s". ' , $ csvFile , $ jsonFile , $ configuration ->getFlow ()));
199+ $ this ->line (sprintf ('Going to convert from file %s using configuration %s and flow "%s". ' , $ importableFile , $ jsonFile , $ configuration ->getFlow ()));
195200
196201 // this is it!
197- $ this ->startConversion ($ configuration , $ csvFile );
202+ $ this ->startConversion ($ configuration , $ importableFile );
198203 $ this ->reportConversion ();
199204
200- $ this ->line (sprintf ('Done converting from file %s using configuration %s. ' , $ csvFile , $ jsonFile ));
205+ $ this ->line (sprintf ('Done converting from file %s using configuration %s. ' , $ importableFile , $ jsonFile ));
201206 $ this ->startImport ($ configuration );
202207 $ this ->reportImport ();
203208
@@ -215,10 +220,10 @@ private function importFile(string $directory, string $file): void
215220 /**
216221 * @param Configuration $configuration
217222 *
218- * @param string|null $csvFile
223+ * @param string|null $importableFile
219224 * @throws ImporterErrorException
220225 */
221- private function startConversion (Configuration $ configuration , ?string $ csvFile ): void
226+ private function startConversion (Configuration $ configuration , ?string $ importableFile ): void
222227 {
223228 $ this ->conversionMessages = [];
224229 $ this ->conversionWarnings = [];
@@ -230,11 +235,13 @@ private function startConversion(Configuration $configuration, ?string $csvFile)
230235 default :
231236 $ this ->error (sprintf ('There is no support for flow "%s" ' , $ configuration ->getFlow ()));
232237 exit ();
233- case 'csv ' :
238+ case 'file ' :
234239 // create importer
240+ // TODO detect file type / content here.
241+ // TODO or perhaps create "FileRoutineManager"
235242 $ manager = new CSVRoutineManager (null );
236243 $ this ->identifier = $ manager ->getIdentifier ();
237- $ manager ->setContent (file_get_contents ($ csvFile ));
244+ $ manager ->setContent (file_get_contents ($ importableFile ));
238245 break ;
239246 case 'nordigen ' :
240247 $ manager = new NordigenRoutineManager (null );
@@ -417,29 +424,29 @@ private function reportImport(): void
417424
418425 /**
419426 * @param string $jsonFile
420- * @param null|string $csvFile
427+ * @param null|string $importableFile
421428 * @throws ImporterErrorException
422429 */
423- private function importUpload (string $ jsonFile , ?string $ csvFile ): void
430+ private function importUpload (string $ jsonFile , ?string $ importableFile ): void
424431 {
425432 // do JSON check
426433 $ jsonResult = $ this ->verifyJSON ($ jsonFile );
427434 if (false === $ jsonResult ) {
428- $ message = sprintf ('The importer can \'t import %s: could not decode the JSON in config file %s. ' , $ csvFile , $ jsonFile );
435+ $ message = sprintf ('The importer can \'t import %s: could not decode the JSON in config file %s. ' , $ importableFile , $ jsonFile );
429436 $ this ->error ($ message );
430437
431438 return ;
432439 }
433440 $ configuration = Configuration::fromArray (json_decode (file_get_contents ($ jsonFile ), true ));
434441 $ configuration ->updateDateRange ();
435442
436- $ this ->line (sprintf ('Going to convert from file "%s" using configuration "%s" and flow "%s". ' , $ csvFile , $ jsonFile , $ configuration ->getFlow ()));
443+ $ this ->line (sprintf ('Going to convert from file "%s" using configuration "%s" and flow "%s". ' , $ importableFile , $ jsonFile , $ configuration ->getFlow ()));
437444
438445 // this is it!
439- $ this ->startConversion ($ configuration , $ csvFile );
446+ $ this ->startConversion ($ configuration , $ importableFile );
440447 $ this ->reportConversion ();
441448
442- $ this ->line (sprintf ('Done converting from file %s using configuration %s. ' , $ csvFile , $ jsonFile ));
449+ $ this ->line (sprintf ('Done converting from file %s using configuration %s. ' , $ importableFile , $ jsonFile ));
443450 $ this ->startImport ($ configuration );
444451 $ this ->reportImport ();
445452
0 commit comments