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.

67 lines
2.1 KiB

  1. <?php
  2. /*
  3. * ConfigFileProcessor.php
  4. * Copyright (c) 2021 james@firefly-iii.org
  5. *
  6. * This file is part of the Firefly III Data Importer
  7. * (https://github.com/firefly-iii/data-importer).
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. declare(strict_types=1);
  23. namespace App\Services\CSV\Configuration;
  24. use App\Exceptions\ImporterErrorException;
  25. use App\Services\Storage\StorageService;
  26. use Illuminate\Contracts\Filesystem\FileNotFoundException;
  27. use JsonException;
  28. use Log;
  29. /**
  30. * Class ConfigFileProcessor
  31. */
  32. class ConfigFileProcessor
  33. {
  34. /**
  35. * Input (the content of) a configuration file and this little script will convert it to a compatible array.
  36. *
  37. * @param string $fileName
  38. *
  39. * @return Configuration
  40. * @throws ImporterErrorException
  41. */
  42. public static function convertConfigFile(string $fileName): Configuration
  43. {
  44. Log::debug('Now in ConfigFileProcessor::convertConfigFile');
  45. try {
  46. $content = StorageService::getContent($fileName);
  47. } catch (FileNotFoundException $e) {
  48. Log::error($e->getMessage());
  49. throw new ImporterErrorException(sprintf('Cpuld not find config file: %s', $e->getMessage()));
  50. }
  51. try {
  52. $json = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
  53. } catch (JsonException $e) {
  54. Log::error($e->getMessage());
  55. throw new ImporterErrorException(sprintf('Invalid JSON configuration file: %s', $e->getMessage()));
  56. }
  57. return Configuration::fromFile($json);
  58. }
  59. }