ClearObsoleteWebsitesSiteCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Opentalent\OtAdmin\Command;
  3. use Opentalent\OtAdmin\Controller\SiteController;
  4. use Opentalent\OtCore\Exception\NoSuchOrganizationException;
  5. use Symfony\Component\Console\Command\Command;
  6. use Symfony\Component\Console\Input\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. use TYPO3\CMS\Core\Database\ConnectionPool;
  12. use TYPO3\CMS\Core\Utility\GeneralUtility;
  13. use TYPO3\CMS\Extbase\Object\ObjectManager;
  14. /**
  15. * This CLI command loop over the ot_websites and delete the ones whom organization doesn't exist anymore
  16. *
  17. * @package Opentalent\OtAdmin\Command
  18. */
  19. class ClearObsoleteWebsitesSiteCommand extends Command
  20. {
  21. public function __construct(
  22. private readonly SiteController $siteController,
  23. private readonly ConnectionPool $connectionPool
  24. ) {
  25. parent::__construct();
  26. }
  27. /**
  28. * -- This method is expected by Typo3, do not rename ou remove --
  29. *
  30. * Allows to configure the command.
  31. * Allows to add a description, a help text, and / or define arguments.
  32. *
  33. */
  34. protected function configure(): void
  35. {
  36. $this
  37. ->setName("ot:clear-obsoletes-websites")
  38. ->setDescription("Delete obsolete websites")
  39. ->setHelp(" This CLI command loop over the ot_websites and delete
  40. the ones whom organization doesn't exist anymore")
  41. ->addOption(
  42. 'preview',
  43. 'p',
  44. InputOption::VALUE_NONE,
  45. "Only preview, perform no deletion"
  46. );
  47. }
  48. /**
  49. * -- This method is expected by Typo3, do not rename ou remove --
  50. *
  51. * @param InputInterface $input
  52. * @param OutputInterface $output
  53. * @return int
  54. * @throws NoSuchOrganizationException
  55. * @throws \Doctrine\DBAL\ConnectionException
  56. * @throws \Doctrine\DBAL\DBALException
  57. * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException
  58. * @throws \Opentalent\OtCore\Exception\NoSuchRecordException
  59. * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
  60. * @throws \TYPO3\CMS\Extbase\Object\Exception
  61. * @throws \Throwable
  62. */
  63. protected function execute(InputInterface $input, OutputInterface $output): int
  64. {
  65. $preview = $input->getOption('preview');
  66. $io = new SymfonyStyle($input, $output);
  67. $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
  68. $sites = $queryBuilder
  69. ->select('organization_id')
  70. ->from('ot_websites')
  71. ->where($queryBuilder->expr()->eq('deleted', 0))
  72. ->execute()
  73. ->fetchAll();
  74. $io->progressStart(count($sites));
  75. $n = 0;
  76. foreach ($sites as $site) {
  77. try {
  78. $this->siteController->fetchOrganization($site['organization_id']);
  79. } catch (NoSuchOrganizationException $e) {
  80. $n++;
  81. if ($preview) {
  82. $io->info('Site ' . $site['uid'] . ' to delete : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']');
  83. continue;
  84. }
  85. $this->siteController->deleteSiteAction($site['organization_id']);
  86. $io->info('Website ' . $site['uid'] . ' deleted : ' . $site['organization_id'] . ' [' . $site['organization_name'] . ']');
  87. }
  88. /** @noinspection DisconnectedForeachInstructionInspection */
  89. $io->progressAdvance();
  90. }
  91. $io->progressFinish();
  92. if (!$preview) {
  93. $io->success(sprintf($n . " websites have been successfully deleted"));
  94. } else {
  95. $io->success(sprintf("Preview ended -- " . $n . ' websites would be deleted'));
  96. }
  97. return Command::SUCCESS;
  98. }
  99. }