SchemaValidateCommand.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /** @noinspection PhpUnused */
  3. namespace App\Commands\Doctrine;
  4. use App\Service\Doctrine\SchemaValidation\Difference;
  5. use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
  6. use App\Service\Doctrine\SchemaValidation\SchemaSnippetsMaker;
  7. use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
  8. use Symfony\Component\Console\Attribute\AsCommand;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. /**
  14. * Valide le schéma Doctrine en le comparant à la V1
  15. *
  16. * @see https://ressources-opentalent.atlassian.net/wiki/spaces/DEV/pages/240551231/V+rifier+le+sch+ma+Doctrine+de+la+V2+et+g+n+rer+les+entit+s+et+propri+t+s+manquantes
  17. */
  18. #[AsCommand(
  19. name: 'ot:schema:validate',
  20. description: 'Compare le schema doctrine de la V2 à celui de la V1'
  21. )]
  22. class SchemaValidateCommand extends Command
  23. {
  24. public function __construct(
  25. private readonly SchemaValidationService $schemaValidationService,
  26. private readonly SchemaSnippetsMaker $schemaSnippetsMaker
  27. ) {
  28. parent::__construct();
  29. }
  30. /**
  31. * Configures the command.
  32. */
  33. protected function configure(): void
  34. {
  35. $this->addOption(
  36. 'filter',
  37. null,
  38. InputOption::VALUE_OPTIONAL,
  39. "Filter the type of difference to display (ex: 'MISSING_PROPERTY')."
  40. );
  41. $this->addOption(
  42. 'csv',
  43. null,
  44. InputOption::VALUE_NONE,
  45. "Print the result in CSV format."
  46. );
  47. $this->addOption(
  48. 'snippets',
  49. null,
  50. InputOption::VALUE_NONE,
  51. "Make snippets of the missing classes and fields"
  52. );
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output): int
  55. {
  56. $filter = $input->getOption('filter') ? DiffTypeEnum::from($input->getOption('filter')) : null;
  57. $diff = $this->schemaValidationService->validateSchema($filter);
  58. foreach ($diff as $entity => $value) {
  59. if (empty($value)) {
  60. continue;
  61. }
  62. if ($input->getOption('csv')) {
  63. $this->printCsv($output, $entity, $value);
  64. } else {
  65. $this->printVerbose($output, $entity, $value);
  66. }
  67. }
  68. if ($diff) {
  69. $count = 0;
  70. foreach ($diff as $entity => $value) {
  71. $count += (is_array($value) ? count($value) : 1);
  72. }
  73. $output->writeln($count . " differences found");
  74. } else {
  75. $output->writeln("No difference found");
  76. }
  77. if ($input->getOption('snippets')) {
  78. $this->schemaSnippetsMaker->makeSnippets($diff);
  79. $output->writeln("Snippets generated");
  80. }
  81. return 0;
  82. }
  83. /**
  84. * @param OutputInterface $output
  85. * @param string $entity
  86. * @param Difference|array<Difference> $differences
  87. * @return void
  88. */
  89. protected function printVerbose(OutputInterface $output, string $entity, Difference | array $differences): void {
  90. $output->writeln($entity);
  91. if (!is_array($differences)) {
  92. $output->writeln($differences->getType()->value . " : " . $differences->getMessage());
  93. } else {
  94. foreach ($differences as $field => $difference) {
  95. $output->writeln(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
  96. }
  97. }
  98. $output->writeln("\n");
  99. }
  100. /**
  101. * @param OutputInterface $output
  102. * @param string $entity
  103. * @param Difference|array<Difference> $differences
  104. * @return void
  105. */
  106. protected function printCsv(OutputInterface $output, string $entity, Difference | array $differences): void {
  107. if (!is_array($differences)) {
  108. $output->writeln(implode(';', [$entity, '', $differences->getType()->value]));
  109. } else {
  110. foreach ($differences as $field => $difference) {
  111. $output->writeln(implode(';', [$entity, $field, $difference->getType()->value]));
  112. }
  113. }
  114. }
  115. }