From 1017fe91de654e32de2245365b04929708d35180 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 Apr 2022 17:41:37 +0200 Subject: [PATCH] Rename other stuff from file to CSV. --- app/Console/AutoImports.php | 18 ++++++++++-------- app/Console/Commands/Import.php | 4 ++-- app/Http/Controllers/AutoImportController.php | 2 +- .../Import/ConversionController.php | 1 + app/Http/Controllers/Import/MapController.php | 5 +++-- .../Controllers/Import/UploadController.php | 8 +++++--- app/Http/Controllers/IndexController.php | 4 ++-- app/Services/Session/Constants.php | 2 +- 8 files changed, 25 insertions(+), 19 deletions(-) diff --git a/app/Console/AutoImports.php b/app/Console/AutoImports.php index 84b813b5..43ae7662 100644 --- a/app/Console/AutoImports.php +++ b/app/Console/AutoImports.php @@ -125,6 +125,8 @@ trait AutoImports } /** + * TODO this function must be more universal. + * * @param string $directory * @param string $file * @@ -165,7 +167,7 @@ trait AutoImports { app('log')->debug(sprintf('ImportFile: directory "%s"', $directory)); app('log')->debug(sprintf('ImportFile: file "%s"', $file)); - $csvFile = sprintf('%s/%s', $directory, $file); + $importableFile = sprintf('%s/%s', $directory, $file); $jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -5)); // TODO not yet sure why the distinction is necessary. @@ -174,33 +176,33 @@ trait AutoImports $jsonFile = sprintf('%s/%s.json', $directory, substr($file, 0, -4)); } - app('log')->debug(sprintf('ImportFile: importable "%s"', $csvFile)); + app('log')->debug(sprintf('ImportFile: importable "%s"', $importableFile)); app('log')->debug(sprintf('ImportFile: JSON "%s"', $jsonFile)); // do JSON check $jsonResult = $this->verifyJSON($jsonFile); if (false === $jsonResult) { - $message = sprintf('The importer can\'t import %s: could not decode the JSON in config file %s.', $csvFile, $jsonFile); + $message = sprintf('The importer can\'t import %s: could not decode the JSON in config file %s.', $importableFile, $jsonFile); $this->error($message); return; } $configuration = Configuration::fromArray(json_decode(file_get_contents($jsonFile), true)); - // sanity check. If the csvFile is a .json file and it parses as valid json, don't import it: - if ('file' === $configuration->getFlow() && str_ends_with(strtolower($csvFile), '.json') && $this->verifyJSON($csvFile)) { + // sanity check. If the importableFile is a .json file and it parses as valid json, don't import it: + if ('file' === $configuration->getFlow() && str_ends_with(strtolower($importableFile), '.json') && $this->verifyJSON($importableFile)) { app('log')->warning('Almost tried to import a JSON file as a file lol. Skip it.'); return; } $configuration->updateDateRange(); - $this->line(sprintf('Going to convert from file %s using configuration %s and flow "%s".', $csvFile, $jsonFile, $configuration->getFlow())); + $this->line(sprintf('Going to convert from file %s using configuration %s and flow "%s".', $importableFile, $jsonFile, $configuration->getFlow())); // this is it! - $this->startConversion($configuration, $csvFile); + $this->startConversion($configuration, $importableFile); $this->reportConversion(); - $this->line(sprintf('Done converting from file %s using configuration %s.', $csvFile, $jsonFile)); + $this->line(sprintf('Done converting from file %s using configuration %s.', $importableFile, $jsonFile)); $this->startImport($configuration); $this->reportImport(); diff --git a/app/Console/Commands/Import.php b/app/Console/Commands/Import.php index b9acd46f..e6f530c3 100644 --- a/app/Console/Commands/Import.php +++ b/app/Console/Commands/Import.php @@ -52,7 +52,7 @@ class Import extends Command */ protected $signature = 'importer:import {config : The configuration file. } - {file? : Optionally, the CSV file you want to import} + {file? : Optionally, the importable file you want to import} '; /** @@ -109,7 +109,7 @@ class Import extends Command } $configuration = Configuration::fromArray(json_decode(file_get_contents($config), true)); if ('file' === $configuration->getFlow() && (!file_exists($file) || (file_exists($file) && !is_file($file)))) { - $message = sprintf('The importer can\'t import: CSV file "%s" does not exist or could not be read.', $file); + $message = sprintf('The importer can\'t import: importable file "%s" does not exist or could not be read.', $file); $this->error($message); app('log')->error($message); diff --git a/app/Http/Controllers/AutoImportController.php b/app/Http/Controllers/AutoImportController.php index ea562234..0ee2b98e 100644 --- a/app/Http/Controllers/AutoImportController.php +++ b/app/Http/Controllers/AutoImportController.php @@ -92,7 +92,7 @@ class AutoImportController extends Controller if (0 === count($files)) { return response(''); } - app('log')->info(sprintf('Found %d (CSV +) JSON file sets in %s', count($files), $directory)); + app('log')->info(sprintf('Found %d (importable +) JSON file sets in %s', count($files), $directory)); try { $this->importFiles($directory, $files); } catch (ImporterErrorException $e) { diff --git a/app/Http/Controllers/Import/ConversionController.php b/app/Http/Controllers/Import/ConversionController.php index 551b1b7b..a684a6b3 100644 --- a/app/Http/Controllers/Import/ConversionController.php +++ b/app/Http/Controllers/Import/ConversionController.php @@ -101,6 +101,7 @@ class ConversionController extends Controller } /** @var RoutineManagerInterface $routine */ if ('file' === $flow) { + // TODO needs a file check here app('log')->debug('Create CSV routine manager.'); $routine = new CSVRoutineManager($identifier); } diff --git a/app/Http/Controllers/Import/MapController.php b/app/Http/Controllers/Import/MapController.php index cf973b5c..f9e16082 100644 --- a/app/Http/Controllers/Import/MapController.php +++ b/app/Http/Controllers/Import/MapController.php @@ -79,7 +79,7 @@ class MapController extends Controller $roles = []; if ('file' === $configuration->getFlow()) { - app('log')->debug('Get mapping data for CSV'); + app('log')->debug('Get mapping data for importable file'); $roles = $configuration->getRoles(); $data = $this->getCSVMapInformation(); } @@ -109,8 +109,9 @@ class MapController extends Controller } /** - * Return the map data necessary for the CSV mapping based on some weird helpers. + * Return the map data necessary for the importable file mapping based on some weird helpers. * TODO needs refactoring and proper splitting into helpers. + * TODO needs renaming or specific CAMT counterpart. * * @return array * @throws ContainerExceptionInterface diff --git a/app/Http/Controllers/Import/UploadController.php b/app/Http/Controllers/Import/UploadController.php index fe5a749a..8f9f2a51 100644 --- a/app/Http/Controllers/Import/UploadController.php +++ b/app/Http/Controllers/Import/UploadController.php @@ -105,7 +105,8 @@ class UploadController extends Controller $flow = $request->cookie(Constants::FLOW_COOKIE); $errors = new MessageBag; - // process CSV file (if present) + // process uploaded file (if present) + // TODO needs to be file agnostic. $errors = $this->processCsvFile($flow, $errors, $csvFile); // process config file (if present) @@ -134,6 +135,7 @@ class UploadController extends Controller } /** + * TODO method needs to be file agnostic. * @return MessageBag */ private function processCsvFile(string $flow, MessageBag $errors, UploadedFile|null $file): MessageBag @@ -162,8 +164,8 @@ class UploadController extends Controller $content = str_replace("\r", "\n", $content); } - $csvFileName = StorageService::storeContent($content); - session()->put(Constants::UPLOAD_CSV_FILE, $csvFileName); + $fileName = StorageService::storeContent($content); + session()->put(Constants::UPLOAD_CSV_FILE, $fileName); session()->put(Constants::HAS_UPLOAD, true); } } diff --git a/app/Http/Controllers/IndexController.php b/app/Http/Controllers/IndexController.php index 1562a1a5..16e3fd46 100644 --- a/app/Http/Controllers/IndexController.php +++ b/app/Http/Controllers/IndexController.php @@ -50,7 +50,7 @@ class IndexController extends Controller public function flush(): mixed { app('log')->debug(sprintf('Now at %s', __METHOD__)); - session()->forget(['csv_file_path', 'config_file_path', 'import_job_id']); + session()->forget([Constants::UPLOAD_CSV_FILE, Constants::UPLOAD_CONFIG_FILE, Constants::IMPORT_JOB_IDENTIFIER]); session()->flush(); $cookies = [ cookie(Constants::FLOW_COOKIE, ''), @@ -128,7 +128,7 @@ class IndexController extends Controller public function reset(): mixed { app('log')->debug(sprintf('Now at %s', __METHOD__)); - session()->forget(['csv_file_path', 'config_file_path', 'import_job_id']); + session()->forget([Constants::UPLOAD_CSV_FILE, Constants::UPLOAD_CONFIG_FILE, Constants::IMPORT_JOB_IDENTIFIER]); session()->flush(); Artisan::call('cache:clear'); diff --git a/app/Services/Session/Constants.php b/app/Services/Session/Constants.php index c642313d..4fda5a9c 100644 --- a/app/Services/Session/Constants.php +++ b/app/Services/Session/Constants.php @@ -73,7 +73,7 @@ class Constants // constants for data conversion job: public const UPLOAD_CONFIG_FILE = 'config_file_path'; - public const UPLOAD_CSV_FILE = 'csv_file_path'; + public const UPLOAD_CSV_FILE = 'csv_file_path'; // TODO this variable must be renamed. // /** @var string */