You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.4 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Import.php
  5. * Copyright (c) 2020 james@firefly-iii.org
  6. *
  7. * This file is part of the Firefly III CSV importer
  8. * (https://github.com/firefly-iii/csv-importer).
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. namespace App\Console\Commands;
  24. use App\Console\HaveAccess;
  25. use App\Console\StartImport;
  26. use App\Console\VerifyJSON;
  27. use App\Mail\ImportFinished;
  28. use Illuminate\Console\Command;
  29. use Illuminate\Support\Facades\Mail;
  30. use JsonException;
  31. use Log;
  32. /**
  33. * Class Import
  34. */
  35. class Import extends Command
  36. {
  37. use HaveAccess, VerifyJSON, StartImport;
  38. /**
  39. * The console command description.
  40. *
  41. * @var string
  42. */
  43. protected $description = 'Import a CSV file. Requires the CSV file and the associated configuration file.';
  44. /**
  45. * The name and signature of the console command.
  46. *
  47. * @var string
  48. */
  49. protected $signature = 'importer:import
  50. {file : The CSV file you want to import}
  51. {config : The associated configuration file}';
  52. /**
  53. * Execute the console command.
  54. *
  55. * @return int
  56. */
  57. public function handle(): int
  58. {
  59. $access = $this->haveAccess();
  60. if (false === $access) {
  61. $this->error('Could not connect to your local Firefly III instance.');
  62. return 1;
  63. }
  64. $this->info(sprintf('Welcome to the Firefly III data importer, v%s', config('importer.version')));
  65. Log::debug(sprintf('Now in %s', __METHOD__));
  66. $file = (string) $this->argument('file');
  67. $config = (string) $this->argument('config');
  68. if (!file_exists($file) || (file_exists($file) && !is_file($file))) {
  69. $message = sprintf('The importer can\'t import: CSV file "%s" does not exist or could not be read.', $file);
  70. $this->error($message);
  71. Log::error($message);
  72. return 1;
  73. }
  74. if (!file_exists($config) || (file_exists($config) && !is_file($config))) {
  75. $message = sprintf('The importer can\'t import: configuration file "%s" does not exist or could not be read.', $config);
  76. $this->error($message);
  77. Log::error($message);
  78. return 1;
  79. }
  80. $jsonResult = $this->verifyJSON($config);
  81. if (false === $jsonResult) {
  82. $message = 'The importer can\'t import: could not decode the JSON in the config file.';
  83. $this->error($message);
  84. return 1;
  85. }
  86. try {
  87. $configuration = json_decode(file_get_contents($config), true, 512, JSON_THROW_ON_ERROR);
  88. } catch (JsonException $e) {
  89. Log::error($e->getMessage());
  90. $this->error(sprintf('Invalid JSON in configuration file: %s', $e->getMessage()));
  91. return 1;
  92. }
  93. $this->line('The import routine is about to start.');
  94. $this->line('This is invisible and may take quite some time.');
  95. $this->line('Once finished, you will see a list of errors, warnings and messages (if applicable).');
  96. $this->line('--------');
  97. $this->line('Running...');
  98. $csv = file_get_contents($file);
  99. $result = $this->startImport($csv, $configuration);
  100. if (0 === $result) {
  101. $this->line('Import complete.');
  102. }
  103. if (0 !== $result) {
  104. $this->warn('The import finished with errors.');
  105. }
  106. // send mail:
  107. $log
  108. = [
  109. 'messages' => $this->messages,
  110. 'warnings' => $this->warnings,
  111. 'errors' => $this->errors,
  112. ];
  113. $send = config('mail.enable_mail_report');
  114. Log::debug('Log log', $log);
  115. if (true === $send) {
  116. Log::debug('SEND MAIL');
  117. Mail::to(config('mail.destination'))->send(new ImportFinished($log));
  118. }
  119. return $result;
  120. }
  121. }