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.

123 lines
3.2 KiB

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Console\Commands;
  4. use App\Services\Shared\Configuration\Configuration;
  5. use Illuminate\Console\Command;
  6. use RecursiveDirectoryIterator;
  7. use RecursiveIteratorIterator;
  8. use SplFileInfo;
  9. class UpgradeImportConfigurations extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'importer:upgrade-import-configurations {directory}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Pointed to a directory, will parse and OVERWRITE all JSON files found there according to the latest JSON configuration file standards.';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle(): int
  38. {
  39. $directory = (string) $this->argument('directory');
  40. if (!file_exists($directory)) {
  41. $this->error(sprintf('"%s" does not exist.', $directory));
  42. return 1;
  43. }
  44. if (!is_dir($directory)) {
  45. $this->error(sprintf('"%s" is not a directory.', $directory));
  46. return 1;
  47. }
  48. $this->processRoot($directory);
  49. return 0;
  50. }
  51. /**
  52. * @param string $directory
  53. */
  54. private function processRoot(string $directory): void
  55. {
  56. $dir = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS);
  57. $files = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);
  58. /**
  59. * @var string $name
  60. * @var SplFileInfo $object
  61. */
  62. foreach ($files as $name => $object) {
  63. $this->processFile($name);
  64. }
  65. }
  66. /**
  67. * @param string $name
  68. */
  69. private function processFile(string $name): void
  70. {
  71. if ('json' !== $this->getExtension($name) || is_dir($name)) {
  72. return;
  73. }
  74. $this->line(sprintf('Now processing "%s" ...', $name));
  75. $content = (string) file_get_contents($name);
  76. if (!$this->isValidJson($content)) {
  77. $this->error('File does not contain valid JSON. Skipped.');
  78. return;
  79. }
  80. $configuration = Configuration::fromFile(json_decode($content, true));
  81. $newJson = $configuration->toArray();
  82. $newJson['mapping'] = [];
  83. $newJson['default_account'] = 0;
  84. file_put_contents($name, json_encode($newJson, JSON_PRETTY_PRINT));
  85. }
  86. /**
  87. * @param string $name
  88. * @return string
  89. */
  90. private function getExtension(string $name): string
  91. {
  92. $parts = explode('.', $name);
  93. return $parts[count($parts) - 1];
  94. }
  95. /**
  96. * @param string $content
  97. * @return bool
  98. */
  99. private function isValidJson(string $content): bool
  100. {
  101. if ('' === $content) {
  102. return false;
  103. }
  104. $json = json_decode($content, true);
  105. if (false === $json) {
  106. return false;
  107. }
  108. return true;
  109. }
  110. }