UpdateConfigFileCommand.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 create or update the organization config file, and update the identifier field in ot_websites table
  16. * This is a light alternative to the update command, designed to be executed on a development env, just after
  17. * the databases cloning.
  18. *
  19. * @package Opentalent\OtAdmin\Command
  20. */
  21. class UpdateConfigFileCommand extends Command
  22. {
  23. /**
  24. * -- This method is expected by Typo3, do not rename ou remove --
  25. *
  26. * Allows to configure the command.
  27. * Allows to add a description, a help text, and / or define arguments.
  28. *
  29. */
  30. protected function configure()
  31. {
  32. $this
  33. ->setName("ot:site:gen-config-file")
  34. ->setDescription("Update an organization configuration file")
  35. ->setHelp("This CLI command creates or updates the organization config file")
  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. }
  48. /**
  49. * -- This method is expected by Typo3, do not rename ou remove --
  50. *
  51. * @param InputInterface $input
  52. * @param OutputInterface $output
  53. * @throws \Exception
  54. */
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. $org_id = $input->getArgument('organization-id');
  58. $all = $input->getOption('all');
  59. if ($all && $org_id) {
  60. throw new \InvalidArgumentException("You can not pass both an organization id and the --all option");
  61. }
  62. if (!$all && !$org_id) {
  63. throw new \InvalidArgumentException("You shall either pass an organization id or use the --all option");
  64. }
  65. $io = new SymfonyStyle($input, $output);
  66. $siteController = GeneralUtility::makeInstance(ObjectManager::class)->get(SiteController::class);
  67. if ($all) {
  68. $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
  69. $queryBuilder = $connectionPool->getQueryBuilderForTable('ot_websites');
  70. $sites = $queryBuilder
  71. ->select('organization_id')
  72. ->from('ot_websites')
  73. ->where($queryBuilder->expr()->eq('deleted', 0))
  74. ->andWhere($queryBuilder->expr()->gt('organization_id', 0))
  75. ->execute()
  76. ->fetchAll();
  77. $io->progressStart(count($sites));
  78. foreach ($sites as $site) {
  79. $org_id = $site['organization_id'];
  80. try {
  81. $siteController->updateConfigFileAction($org_id);
  82. } catch (\Throwable $e) {
  83. $io->error('Organization Id: ' . $org_id . ' - ' . $e->getMessage());
  84. }
  85. $io->progressAdvance(1);
  86. }
  87. $io->progressFinish();
  88. $io->success(sprintf("The websites config files have all been updated"));
  89. } else {
  90. $identifier = $siteController->updateConfigFileAction($org_id);
  91. $io->success(sprintf("The config file with identifier " . $identifier . " has been updated"));
  92. }
  93. }
  94. }