. */ declare(strict_types=1); namespace App\Console\Commands; use App\Console\AutoImports; use App\Console\HaveAccess; use App\Console\VerifyJSON; use App\Enums\ExitCode; use Illuminate\Console\Command; /** * Class AutoImport */ final class AutoImport extends Command { use AutoImports; use HaveAccess; use VerifyJSON; /** * The console command description. * * @var string */ protected $description = 'Will automatically import from the given directory and use the JSON and importable files found.'; /** * The name and signature of the console command. * * @var string */ protected $signature = 'importer:auto-import {directory : The directory from which to import automatically.}'; /** * Execute the console command. */ public function handle(): int { $access = $this->haveAccess(); if (false === $access) { $this->error(sprintf('[a] No access, or no connection is possible to your local Firefly III instance at %s.', config('importer.url'))); app('log')->error(sprintf('Exit code is %s.', ExitCode::NO_CONNECTION->name)); return ExitCode::NO_CONNECTION->value; } $argument = (string) ($this->argument('directory') ?? './'); /** @phpstan-ignore-line */ $directory = realpath($argument); if (false === $directory) { $this->error(sprintf('Path "%s" is not a valid location.', $argument)); app('log')->error(sprintf('Exit code is %s.', ExitCode::INVALID_PATH->name)); return ExitCode::INVALID_PATH->value; } if (!$this->isAllowedPath($directory)) { $this->error(sprintf('Path "%s" is not in the list of allowed paths (IMPORT_DIR_ALLOWLIST).', $directory)); app('log')->error(sprintf('Exit code is %s.', ExitCode::NOT_ALLOWED_PATH->name)); return ExitCode::NOT_ALLOWED_PATH->value; } $this->line(sprintf('Going to automatically import everything found in %s (%s)', $directory, $argument)); $files = $this->getFiles($directory); if (0 === count($files)) { $this->info(sprintf('There are no files in directory %s', $directory)); $this->info('To learn more about this process, read the docs:'); $this->info('https://docs.firefly-iii.org/'); app('log')->error(sprintf('Exit code is %s.', ExitCode::NO_FILES_FOUND->name)); return ExitCode::NO_FILES_FOUND->value; } $this->line(sprintf('Found %d (importable +) JSON file sets in %s', count($files), $directory)); $result = $this->importFiles($directory, $files); $unique = array_unique($result); if (1 === count($unique)) { return (int) reset($result); } if (count($unique) > 0) { $this->warn('Multiple return codes found. Some imports may have failed'); foreach ($result as $file => $code) { $this->warn(sprintf('File %s returned code #%d', $file, $code)); } app('log')->error(sprintf('Exit code is %s.', ExitCode::GENERAL_ERROR->name)); return ExitCode::GENERAL_ERROR->value; } app('log')->error(sprintf('Exit code is %s.', ExitCode::SUCCESS->name)); return ExitCode::SUCCESS->value; } }