DolibarrSyncCommand.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Commands;
  3. use App\Service\Dolibarr\DolibarrSyncService;
  4. use Symfony\Component\Console\Attribute\AsCommand;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Command\LockableTrait;
  7. use Symfony\Component\Console\Helper\ProgressBar;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Input\InputOption;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11. #[AsCommand(
  12. name: 'ot:dolibarr-sync',
  13. description: 'Push the latest data from the Opentalent DB to dolibarr'
  14. )]
  15. class DolibarrSyncCommand extends Command
  16. {
  17. use LockableTrait;
  18. /**
  19. * How many operations are shown each time the preview choice is made.
  20. */
  21. public const PREVIEW_CHUNK = 20;
  22. public function __construct(
  23. private DolibarrSyncService $dolibarrSyncService
  24. ) {
  25. parent::__construct();
  26. }
  27. protected function configure(): void
  28. {
  29. $this->addOption(
  30. 'preview',
  31. 'p',
  32. InputOption::VALUE_NONE,
  33. 'Only preview the sync operations instead of executing it'
  34. );
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output): int
  37. {
  38. if (!$this->lock()) {
  39. $output->writeln('The command is already running in another process.');
  40. return Command::SUCCESS;
  41. }
  42. $output->writeln('Start the synchronization');
  43. $t0 = microtime(true);
  44. $output->writeln('Scanning...');
  45. $progressBar = new ProgressBar($output, 0);
  46. $progressCallback = function ($i, $total) use ($progressBar) {
  47. if (!$progressBar->getMaxSteps() !== $total) {
  48. $progressBar->setMaxSteps($total);
  49. }
  50. $progressBar->setProgress($i);
  51. };
  52. $operations = $this->dolibarrSyncService->scan($progressCallback);
  53. $t1 = microtime(true);
  54. $output->writeln('Scan lasted '.($t1 - $t0).' sec.');
  55. $output->writeln(count($operations).' operations to be executed');
  56. if ($input->getOption('preview')) {
  57. $output->writeln('-- Preview --');
  58. foreach ($operations as $i => $iValue) {
  59. $output->writeln($i.'. '.$iValue->getLabel());
  60. foreach ($iValue->getChangeLog() as $message) {
  61. $output->writeln(' '.$message);
  62. }
  63. }
  64. } else {
  65. $t0 = microtime(true);
  66. $output->writeln('Executing...');
  67. $operations = $this->dolibarrSyncService->execute($operations, $progressCallback);
  68. $successes = count(array_filter($operations, function ($o) { return $o->getStatus() === $o::STATUS_DONE; }));
  69. $errors = count(array_filter($operations, function ($o) { return $o->getStatus() === $o::STATUS_ERROR; }));
  70. $output->writeln($successes.' operations successfully executed');
  71. $output->writeln($errors.' errors');
  72. $t1 = microtime(true);
  73. $output->writeln('Execution lasted '.($t1 - $t0).' sec.');
  74. }
  75. return Command::SUCCESS;
  76. }
  77. }