Skip to content

Commit f247670

Browse files
committed
Merge branch 'release/0.9.9'
2 parents 641e148 + e27d86b commit f247670

30 files changed

Lines changed: 726 additions & 541 deletions

app/Console/AutoImports.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

app/Console/Commands/AutoImport.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class AutoImport extends Command
4242
*
4343
* @var string
4444
*/
45-
protected $description = 'Will automatically import from the given directory and use the JSON and CSV files found.';
45+
protected $description = 'Will automatically import from the given directory and use the JSON and importable files found.';
4646
/**
4747
* The name and signature of the console command.
4848
*
@@ -80,7 +80,7 @@ public function handle(): int
8080

8181
return 1;
8282
}
83-
$this->line(sprintf('Found %d (CSV +) JSON file sets in %s', count($files), $directory));
83+
$this->line(sprintf('Found %d (importable +) JSON file sets in %s', count($files), $directory));
8484
try {
8585
$this->importFiles($directory, $files);
8686
} catch (ImporterErrorException $e) {

app/Console/Commands/Import.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Import extends Command
5252
*/
5353
protected $signature = 'importer:import
5454
{config : The configuration file. }
55-
{file? : Optionally, the CSV file you want to import}
55+
{file? : Optionally, the importable file you want to import}
5656
';
5757

5858
/**
@@ -108,8 +108,8 @@ public function handle(): int
108108
return 1;
109109
}
110110
$configuration = Configuration::fromArray(json_decode(file_get_contents($config), true));
111-
if ('csv' === $configuration->getFlow() && (!file_exists($file) || (file_exists($file) && !is_file($file)))) {
112-
$message = sprintf('The importer can\'t import: CSV file "%s" does not exist or could not be read.', $file);
111+
if ('file' === $configuration->getFlow() && (!file_exists($file) || (file_exists($file) && !is_file($file)))) {
112+
$message = sprintf('The importer can\'t import: importable file "%s" does not exist or could not be read.', $file);
113113
$this->error($message);
114114
app('log')->error($message);
115115

app/Http/Controllers/AutoImportController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function index(Request $request): Response
9292
if (0 === count($files)) {
9393
return response('');
9494
}
95-
app('log')->info(sprintf('Found %d (CSV +) JSON file sets in %s', count($files), $directory));
95+
app('log')->info(sprintf('Found %d (importable +) JSON file sets in %s', count($files), $directory));
9696
try {
9797
$this->importFiles($directory, $files);
9898
} catch (ImporterErrorException $e) {

app/Http/Controllers/AutoUploadController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ public function index(AutoUploadRequest $request)
6868
}
6969

7070
$json = $request->file('json');
71-
$csv = $request->file('csv');
72-
$csvPath = $csv?->getPathname();
71+
// TODO update documentation to document rename of importable file variable.
72+
$importable = $request->file('importable');
73+
$importablePath = $importable?->getPathname();
7374

7475
try {
75-
$this->importUpload($json->getPathname(), $csvPath);
76+
$this->importUpload($json->getPathname(), $importablePath);
7677
} catch (ImporterErrorException $e) {
7778
app('log')->error($e->getMessage());
7879
$this->line(sprintf('Import exception (see the logs): %s', $e->getMessage()));

app/Http/Controllers/Import/CSV/RoleController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function index(Request $request)
7474
{
7575
app('log')->debug('Now in role controller');
7676
$flow = $request->cookie(Constants::FLOW_COOKIE);
77-
if ('csv' !== $flow) {
77+
if ('file' !== $flow) {
7878
die('redirect or something');
7979
}
8080
$mainTitle = 'Role definition';

app/Http/Controllers/Import/ConversionController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function index()
7474

7575
app('log')->debug('Will now verify configuration content.');
7676
$jobBackUrl = route('back.mapping');
77-
if (empty($configuration->getDoMapping()) && 'csv' === $configuration->getFlow()) {
77+
if (empty($configuration->getDoMapping()) && 'file' === $configuration->getFlow()) {
7878
// no mapping, back to roles
7979
app('log')->debug('Pressing "back" will send you to roles.');
8080
$jobBackUrl = route('back.roles');
@@ -100,7 +100,8 @@ public function index()
100100
throw new ImporterErrorException(sprintf('Not a supported flow: "%s"', $flow));
101101
}
102102
/** @var RoutineManagerInterface $routine */
103-
if ('csv' === $flow) {
103+
if ('file' === $flow) {
104+
// TODO needs a file check here
104105
app('log')->debug('Create CSV routine manager.');
105106
$routine = new CSVRoutineManager($identifier);
106107
}
@@ -147,7 +148,7 @@ public function start(Request $request): JsonResponse
147148
throw new ImporterErrorException(sprintf('Not a supported flow: "%s"', $flow));
148149
}
149150
/** @var RoutineManagerInterface $routine */
150-
if ('csv' === $flow) {
151+
if ('file' === $flow) {
151152
$routine = new CSVRoutineManager($identifier);
152153
}
153154
if ('nordigen' === $flow) {

app/Http/Controllers/Import/MapController.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ public function index()
7878
$data = [];
7979
$roles = [];
8080

81-
if ('csv' === $configuration->getFlow()) {
82-
app('log')->debug('Get mapping data for CSV');
81+
if ('file' === $configuration->getFlow()) {
82+
app('log')->debug('Get mapping data for importable file');
8383
$roles = $configuration->getRoles();
8484
$data = $this->getCSVMapInformation();
8585
}
8686

8787
// nordigen, spectre and others:
88-
if ('csv' !== $configuration->getFlow()) {
88+
if ('file' !== $configuration->getFlow()) {
8989
app('log')->debug('Get mapping data for nordigen and spectre');
9090
$roles = [];
9191
$data = $this->getImporterMapInformation();
@@ -96,9 +96,9 @@ public function index()
9696
// set map config as complete.
9797
session()->put(Constants::MAPPING_COMPLETE_INDICATOR, true);
9898

99-
// if CSV, now ready for conversion
100-
if ('csv' === $configuration->getFlow()) {
101-
app('log')->debug('Its CSV, also set ready for conversion.');
99+
// if file, now ready for conversion
100+
if ('file' === $configuration->getFlow()) {
101+
app('log')->debug('Its a file, also set ready for conversion.');
102102
session()->put(Constants::READY_FOR_CONVERSION, true);
103103
}
104104
return redirect()->route('007-convert.index');
@@ -109,8 +109,9 @@ public function index()
109109
}
110110

111111
/**
112-
* Return the map data necessary for the CSV mapping based on some weird helpers.
112+
* Return the map data necessary for the importable file mapping based on some weird helpers.
113113
* TODO needs refactoring and proper splitting into helpers.
114+
* TODO needs renaming or specific CAMT counterpart.
114115
*
115116
* @return array
116117
* @throws ContainerExceptionInterface

app/Http/Controllers/Import/UploadController.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,13 @@ public function index(Request $request)
100100
public function upload(Request $request)
101101
{
102102
app('log')->debug(sprintf('Now at %s', __METHOD__));
103-
$csvFile = $request->file('csv_file');
103+
$csvFile = $request->file('importable_file');
104104
$configFile = $request->file('config_file');
105105
$flow = $request->cookie(Constants::FLOW_COOKIE);
106106
$errors = new MessageBag;
107107

108-
// process CSV file (if present)
108+
// process uploaded file (if present)
109+
// TODO needs to be file agnostic.
109110
$errors = $this->processCsvFile($flow, $errors, $csvFile);
110111

111112
// process config file (if present)
@@ -134,18 +135,19 @@ public function upload(Request $request)
134135
}
135136

136137
/**
138+
* TODO method needs to be file agnostic.
137139
* @return MessageBag
138140
*/
139141
private function processCsvFile(string $flow, MessageBag $errors, UploadedFile|null $file): MessageBag
140142
{
141-
if (null === $file && 'csv' === $flow) {
142-
$errors->add('csv_file', 'No file was uploaded.');
143+
if (null === $file && 'file' === $flow) {
144+
$errors->add('importable_file', 'No file was uploaded.');
143145
return $errors;
144146
}
145-
if ('csv' === $flow) {
147+
if ('file' === $flow) {
146148
$errorNumber = $file->getError();
147149
if (0 !== $errorNumber) {
148-
$errors->add('csv_file', $this->getError($errorNumber));
150+
$errors->add('importable_file', $this->getError($errorNumber));
149151
}
150152

151153

@@ -162,8 +164,8 @@ private function processCsvFile(string $flow, MessageBag $errors, UploadedFile|n
162164
$content = str_replace("\r", "\n", $content);
163165
}
164166

165-
$csvFileName = StorageService::storeContent($content);
166-
session()->put(Constants::UPLOAD_CSV_FILE, $csvFileName);
167+
$fileName = StorageService::storeContent($content);
168+
session()->put(Constants::UPLOAD_CSV_FILE, $fileName);
167169
session()->put(Constants::HAS_UPLOAD, true);
168170
}
169171
}
@@ -254,6 +256,7 @@ private function processConfigFile(MessageBag $errors, UploadedFile|null $file):
254256
}
255257
// if conversion of the config file was a success, store the new version again:
256258
if (true === $success) {
259+
$configuration->updateDateRange();
257260
$configFileName = StorageService::storeContent(json_encode($configuration->toArray(), JSON_PRETTY_PRINT));
258261
session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName);
259262
}

app/Http/Controllers/IndexController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct()
5050
public function flush(): mixed
5151
{
5252
app('log')->debug(sprintf('Now at %s', __METHOD__));
53-
session()->forget(['csv_file_path', 'config_file_path', 'import_job_id']);
53+
session()->forget([Constants::UPLOAD_CSV_FILE, Constants::UPLOAD_CONFIG_FILE, Constants::IMPORT_JOB_IDENTIFIER]);
5454
session()->flush();
5555
$cookies = [
5656
cookie(Constants::FLOW_COOKIE, ''),
@@ -128,7 +128,7 @@ public function postIndex(Request $request): mixed
128128
public function reset(): mixed
129129
{
130130
app('log')->debug(sprintf('Now at %s', __METHOD__));
131-
session()->forget(['csv_file_path', 'config_file_path', 'import_job_id']);
131+
session()->forget([Constants::UPLOAD_CSV_FILE, Constants::UPLOAD_CONFIG_FILE, Constants::IMPORT_JOB_IDENTIFIER]);
132132
session()->flush();
133133
Artisan::call('cache:clear');
134134

0 commit comments

Comments
 (0)