From 7d348f25ac00d3be3c924d337a76bd99f5073667 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 16 Dec 2017 20:47:08 +0100 Subject: [PATCH] Expand import routine to work over command line. --- app/Console/Commands/CreateImport.php | 28 +++++++++++++------- app/Console/Commands/VerifiesAccessToken.php | 1 + app/Import/Routine/FileRoutine.php | 26 +++++++++++++++++- app/Import/Routine/RoutineInterface.php | 24 ++++++++++++----- resources/lang/en_US/import.php | 14 ---------- 5 files changed, 61 insertions(+), 32 deletions(-) diff --git a/app/Console/Commands/CreateImport.php b/app/Console/Commands/CreateImport.php index 69b034ac7d..8fcb6c16b2 100644 --- a/app/Console/Commands/CreateImport.php +++ b/app/Console/Commands/CreateImport.php @@ -23,8 +23,10 @@ declare(strict_types=1); namespace FireflyIII\Console\Commands; use Artisan; +use FireflyIII\Exceptions\FireflyException; use FireflyIII\Import\Logging\CommandHandler; use FireflyIII\Import\Routine\ImportRoutine; +use FireflyIII\Import\Routine\RoutineInterface; use FireflyIII\Repositories\ImportJob\ImportJobRepositoryInterface; use FireflyIII\Repositories\User\UserRepositoryInterface; use Illuminate\Console\Command; @@ -54,7 +56,7 @@ class CreateImport extends Command protected $signature = 'firefly:create-import {file : The file to import.} - {configuration : The configuration file to use for the import/} + {configuration : The configuration file to use for the import.} {--type=csv : The file type of the import.} {--user= : The user ID that the import should import for.} {--token= : The user\'s access token.} @@ -73,6 +75,7 @@ class CreateImport extends Command * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) // cannot be helped * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's five exactly. + * @throws FireflyException */ public function handle() { @@ -93,7 +96,7 @@ class CreateImport extends Command return; } - $configurationData = json_decode(file_get_contents($configuration)); + $configurationData = json_decode(file_get_contents($configuration), true); if (null === $configurationData) { $this->error(sprintf('Firefly III cannot read the contents of configuration file "%s" (working directory: "%s").', $configuration, $cwd)); @@ -114,9 +117,8 @@ class CreateImport extends Command Artisan::call('firefly:encrypt-file', ['file' => $file, 'key' => $job->key]); $this->line('Stored import data...'); - $job->configuration = $configurationData; - $job->status = 'configured'; - $job->save(); + $jobRepository->setConfiguration($job, $configurationData); + $jobRepository->updateStatus($job, 'configured'); $this->line('Stored configuration...'); if (true === $this->option('start')) { @@ -131,18 +133,24 @@ class CreateImport extends Command $monolog->pushHandler($handler); // start the actual routine: - /** @var ImportRoutine $routine */ - $routine = app(ImportRoutine::class); + $type = $job->file_type === 'csv' ? 'file' : $job->file_type; + $key = sprintf('import.routine.%s', $type); + $className = config($key); + if (null === $className || !class_exists($className)) { + throw new FireflyException(sprintf('Cannot find import routine class for job of type "%s".', $type)); // @codeCoverageIgnore + } + /** @var RoutineInterface $routine */ + $routine = app($className); $routine->setJob($job); $routine->run(); // give feedback. /** @var MessageBag $error */ - foreach ($routine->errors as $index => $error) { + foreach ($routine->getErrors() as $index => $error) { $this->error(sprintf('Error importing line #%d: %s', $index, $error)); } $this->line( - sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->journals->count(), $routine->lines) + sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->getJournals()->count(), $routine->lines) ); } @@ -166,7 +174,7 @@ class CreateImport extends Command $configuration = $this->argument('configuration'); $user = $userRepository->find(intval($this->option('user'))); $cwd = getcwd(); - $validTypes = array_keys(config('firefly.import_formats')); + $validTypes = config('import.options.file.import_formats'); $type = strtolower($this->option('type')); if (null === $user->id) { $this->error(sprintf('There is no user with ID %d.', $this->option('user'))); diff --git a/app/Console/Commands/VerifiesAccessToken.php b/app/Console/Commands/VerifiesAccessToken.php index 15b2ae5658..97f24d2a8e 100644 --- a/app/Console/Commands/VerifiesAccessToken.php +++ b/app/Console/Commands/VerifiesAccessToken.php @@ -68,6 +68,7 @@ trait VerifiesAccessToken } if (!($accessToken->data === $token)) { Log::error(sprintf('Invalid access token for user #%d.', $userId)); + Log::error(sprintf('Token given is "%s", expected "%s".', $token, $accessToken->data)); return false; } diff --git a/app/Import/Routine/FileRoutine.php b/app/Import/Routine/FileRoutine.php index 4d0ce8006c..2c5c12fd64 100644 --- a/app/Import/Routine/FileRoutine.php +++ b/app/Import/Routine/FileRoutine.php @@ -55,6 +55,30 @@ class FileRoutine implements RoutineInterface $this->errors = new Collection; } + /** + * @return Collection + */ + public function getErrors(): Collection + { + return $this->errors; + } + + /** + * @return Collection + */ + public function getJournals(): Collection + { + return $this->journals; + } + + /** + * @return int + */ + public function getLines(): int + { + return $this->lines; + } + /** * */ @@ -111,7 +135,7 @@ class FileRoutine implements RoutineInterface { $objects = new Collection; $config = $this->job->configuration; - $fileType = $config['file-type']; + $fileType = $config['file-type'] ?? 'csv'; // will only respond to "file" $class = config(sprintf('import.options.file.processors.%s', $fileType)); /** @var FileProcessorInterface $processor */ diff --git a/app/Import/Routine/RoutineInterface.php b/app/Import/Routine/RoutineInterface.php index bd1f44493d..241a8e01da 100644 --- a/app/Import/Routine/RoutineInterface.php +++ b/app/Import/Routine/RoutineInterface.php @@ -22,18 +22,26 @@ declare(strict_types=1); namespace FireflyIII\Import\Routine; -use Carbon\Carbon; -use DB; -use FireflyIII\Import\FileProcessor\FileProcessorInterface; -use FireflyIII\Import\Storage\ImportStorage; use FireflyIII\Models\ImportJob; -use FireflyIII\Models\Tag; -use FireflyIII\Repositories\Tag\TagRepositoryInterface; use Illuminate\Support\Collection; -use Log; interface RoutineInterface { + /** + * @return Collection + */ + public function getErrors(): Collection; + + /** + * @return Collection + */ + public function getJournals(): Collection; + + /** + * @return int + */ + public function getLines(): int; + /** * @return bool */ @@ -45,4 +53,6 @@ interface RoutineInterface * @return mixed */ public function setJob(ImportJob $job); + + } diff --git a/resources/lang/en_US/import.php b/resources/lang/en_US/import.php index 858daff129..33db4983f0 100644 --- a/resources/lang/en_US/import.php +++ b/resources/lang/en_US/import.php @@ -3,20 +3,6 @@ declare(strict_types=1); return [ - - // general strings for file upload - // 'import_index_intro' => 'This routine will help you import files from your bank into Firefly III. Please check out the help pages in the top right corner.', - // 'import_index_file' => 'Select your file', - // 'import_index_config' => 'If you have previously imported data into Firefly III, you may have a configuration file, which will pre-set configuration values for you. For some banks, other users have kindly provided their configuration file.', - // - // 'import_index_start' => 'Start importing', - // 'import_file' => 'Import a file', - // - // // supported file types: - // - // - // // import configuration routine: - // status of import: 'status_wait_title' => 'Please hold...', 'status_wait_text' => 'This box will disappear in a moment.',