UpdateSiteCommand.php 4.9 KB

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