UpdateSiteCommand.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 updates an existing organization's website
  16. * with the latest data from the Opentalent DB
  17. *
  18. * @package Opentalent\OtAdmin\Command
  19. */
  20. class UpdateSiteCommand extends Command
  21. {
  22. public function __construct(
  23. private readonly SiteController $siteController,
  24. private readonly ConnectionPool $connectionPool
  25. ) {
  26. parent::__construct();
  27. }
  28. /**
  29. * -- This method is expected by Typo3, do not rename ou remove --
  30. *
  31. * Allows to configure the command.
  32. * Allows to add a description, a help text, and / or define arguments.
  33. *
  34. */
  35. protected function configure(): void
  36. {
  37. $this
  38. ->setName("ot:site:update")
  39. ->setDescription("Update an organization website")
  40. ->setHelp("This CLI command update an existing organization's website
  41. with the latest data from the Opentalent DB")
  42. ->addOption(
  43. 'all',
  44. null,
  45. InputOption::VALUE_NONE,
  46. "Update all of the organization websites"
  47. )
  48. ->addArgument(
  49. 'organization-id',
  50. InputArgument::OPTIONAL,
  51. "The organization's id in the opentalent DB"
  52. )
  53. ->addOption(
  54. 'delete',
  55. null,
  56. InputOption::VALUE_NONE,
  57. "Performs a soft deletion of the websites when the organization does not exist anymore " .
  58. "in the Opentalent DB. (This only applies if the --all option is used)"
  59. );
  60. }
  61. /**
  62. * -- This method is expected by Typo3, do not rename ou remove --
  63. *
  64. * @param InputInterface $input
  65. * @param OutputInterface $output
  66. * @return int
  67. * @throws NoSuchOrganizationException
  68. * @throws \Doctrine\DBAL\ConnectionException
  69. * @throws \Doctrine\DBAL\DBALException
  70. * @throws \Opentalent\OtCore\Exception\InvalidWebsiteConfigurationException
  71. * @throws \Opentalent\OtCore\Exception\NoSuchRecordException
  72. * @throws \Opentalent\OtCore\Exception\NoSuchWebsiteException
  73. * @throws \TYPO3\CMS\Extbase\Object\Exception
  74. * @throws \Throwable
  75. */
  76. protected function execute(InputInterface $input, OutputInterface $output): int
  77. {
  78. $org_id = $input->getArgument('organization-id');
  79. $all = $input->getOption('all');
  80. $delete = $input->getOption('delete');
  81. if ($all && $org_id) {
  82. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  83. }
  84. if (!$all && !$org_id) {
  85. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  86. }
  87. if (!$all && $delete) {
  88. throw new \InvalidArgumentException("The delete option only applies when the --all option is passed");
  89. }
  90. $io = new SymfonyStyle($input, $output);
  91. if ($all) {
  92. $queryBuilder = $this->connectionPool->getQueryBuilderForTable('ot_websites');
  93. $sites = $queryBuilder
  94. ->select('organization_id')
  95. ->from('ot_websites')
  96. ->where($queryBuilder->expr()->eq('deleted', 0))
  97. ->andWhere($queryBuilder->expr()->gt('organization_id', 0))
  98. ->execute()
  99. ->fetchAll();
  100. $io->progressStart(count($sites));
  101. foreach ($sites as $site) {
  102. $org_id = $site['organization_id'];
  103. try {
  104. $this->siteController->updateSiteAction($org_id);
  105. } catch (NoSuchOrganizationException $e) {
  106. if ($delete) {
  107. $this->siteController->deleteSiteAction($org_id);
  108. } else {
  109. $io->error('Organization ' . $org_id . ' does not exist, consider deleting the website.');
  110. }
  111. } catch (\Throwable $e) {
  112. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  113. }
  114. $io->progressAdvance(1);
  115. }
  116. $io->progressFinish();
  117. $io->success(sprintf("The websites have all been updated"));
  118. } else {
  119. $rootUid = $this->siteController->updateSiteAction($org_id);
  120. $io->success(sprintf("The website with root uid " . $rootUid . " has been updated"));
  121. }
  122. return 0;
  123. }
  124. }