argument('key'); $job = ImportJob::where('key', $jobKey)->first(); if (is_null($job)) { $this->error(sprintf('No job found with key "%s"', $jobKey)); return; } if (!$this->isValid($job)) { Log::error('Job is not valid for some reason. Exit.'); return; } $this->line(sprintf('Going to import job with key "%s" of type "%s"', $job->key, $job->file_type)); $monolog = Log::getMonolog(); $handler = new CommandHandler($this); $monolog->pushHandler($handler); $routine = new ImportRoutine($job); $routine->run(); /** @var MessageBag $error */ foreach($routine->errors as $index => $error) { if($error->count() > 0) { $message = join(', ',$error->all()); $this->error(sprintf('Error importing line #%d: %s', $index, $message)); } } // display result to user: //$this->presentResults($result); $this->line(sprintf('The import has finished. %d transactions have been imported out of %d records.', $routine->journals->count(), $routine->lines)); // get any errors from the importer: //$this->presentErrors($job); return; } /** * @param ImportJob $job * * @return bool */ private function isValid(ImportJob $job): bool { if (is_null($job)) { Log::error('This job does not seem to exist.'); $this->error('This job does not seem to exist.'); return false; } if ($job->status !== 'configured') { Log::error(sprintf('This job is not ready to be imported (status is %s).', $job->status)); $this->error('This job is not ready to be imported.'); return false; } return true; } /** * @param ImportJob $job */ private function presentErrors(ImportJob $job) { $extendedStatus = $job->extended_status; if (isset($extendedStatus['errors']) && count($extendedStatus['errors']) > 0) { $this->line(sprintf('The following %d error(s) occured during the import:', count($extendedStatus['errors']))); foreach ($extendedStatus['errors'] as $error) { $this->error($error); } } } /** * @param Collection $result */ private function presentResults(Collection $result) { /** * @var int $index * @var TransactionJournal $journal */ foreach ($result as $index => $journal) { if (!is_null($journal->id)) { $this->line(sprintf('Line #%d has been imported as transaction #%d.', $index, $journal->id)); continue; } $this->error(sprintf('Could not store line #%d', $index)); } } }