Browse Source

Rename other stuff from file to CSV.

pull/92/head
James Cole 4 years ago
parent
commit
1017fe91de
No known key found for this signature in database GPG Key ID: B49A324B7EAD6D80
  1. 18
      app/Console/AutoImports.php
  2. 4
      app/Console/Commands/Import.php
  3. 2
      app/Http/Controllers/AutoImportController.php
  4. 1
      app/Http/Controllers/Import/ConversionController.php
  5. 5
      app/Http/Controllers/Import/MapController.php
  6. 8
      app/Http/Controllers/Import/UploadController.php
  7. 4
      app/Http/Controllers/IndexController.php
  8. 2
      app/Services/Session/Constants.php

18
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();

4
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);

2
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) {

1
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);
}

5
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

8
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);
}
}

4
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');

2
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 */

Loading…
Cancel
Save