SchemaValidation.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Service\Cron\Job;
  3. use App\Service\Cron\BaseCronJob;
  4. use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
  5. use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
  6. use JetBrains\PhpStorm\Pure;
  7. /**
  8. * Valide le schéma Doctrine en le comparant à la V1.
  9. *
  10. * >>> ot:cron run schema-validation --preview
  11. * >>> ot:cron run schema-validation
  12. */
  13. class SchemaValidation extends BaseCronJob
  14. {
  15. public const VALIDATION_FILTER = DiffTypeEnum::MISSING_RELATION;
  16. #[Pure]
  17. public function __construct(
  18. private readonly SchemaValidationService $schemaValidationService,
  19. ) {
  20. parent::__construct();
  21. }
  22. /**
  23. * @return array<string>
  24. */
  25. protected function getDiffAsCsv(): array
  26. {
  27. $diff = $this->schemaValidationService->validateSchema(self::VALIDATION_FILTER);
  28. return $this->schemaValidationService->formatToCsv($diff);
  29. }
  30. /**
  31. * Validate the doctrine schema (without reporting).
  32. */
  33. public function preview(): void
  34. {
  35. $csv = $this->getDiffAsCsv();
  36. if (empty($csv)) {
  37. $this->ui->print('No difference found');
  38. } else {
  39. foreach ($csv as $line) {
  40. $this->ui->print($line);
  41. }
  42. $this->ui->print('');
  43. $this->ui->print('> '.count($csv).' differences found');
  44. }
  45. }
  46. /**
  47. * Validate the doctrine schema and report.
  48. */
  49. public function execute(): void
  50. {
  51. $csv = $this->getDiffAsCsv();
  52. if (empty($csv)) {
  53. $this->logger->info('No difference found');
  54. } else {
  55. foreach ($csv as $line) {
  56. $this->logger->warning($line);
  57. }
  58. $this->logger->warning(count($csv).' differences found');
  59. }
  60. }
  61. }