EnabledCommand.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Core\Command\TaskProcessing;
  7. use OC\Core\Command\Base;
  8. use OCP\IConfig;
  9. use OCP\TaskProcessing\IManager;
  10. use Symfony\Component\Console\Input\InputArgument;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. class EnabledCommand extends Base {
  14. public function __construct(
  15. protected IManager $taskProcessingManager,
  16. private IConfig $config,
  17. ) {
  18. parent::__construct();
  19. }
  20. protected function configure() {
  21. $this
  22. ->setName('taskprocessing:task-type:set-enabled')
  23. ->setDescription('Enable or disable a task type')
  24. ->addArgument(
  25. 'task-type-id',
  26. InputArgument::REQUIRED,
  27. 'ID of the task type to configure'
  28. )
  29. ->addArgument(
  30. 'enabled',
  31. InputArgument::REQUIRED,
  32. 'status of the task type availability. Set 1 to enable and 0 to disable.'
  33. );
  34. parent::configure();
  35. }
  36. protected function execute(InputInterface $input, OutputInterface $output): int {
  37. $enabled = (bool)$input->getArgument('enabled');
  38. $taskType = $input->getArgument('task-type-id');
  39. $json = $this->config->getAppValue('core', 'ai.taskprocessing_type_preferences');
  40. try {
  41. if ($json === '') {
  42. $taskTypeSettings = [];
  43. } else {
  44. $taskTypeSettings = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
  45. }
  46. $taskTypeSettings[$taskType] = $enabled;
  47. $this->config->setAppValue('core', 'ai.taskprocessing_type_preferences', json_encode($taskTypeSettings));
  48. $this->writeArrayInOutputFormat($input, $output, $taskTypeSettings);
  49. return 0;
  50. } catch (\JsonException $e) {
  51. throw new \JsonException('Error in TaskType DB entry');
  52. }
  53. }
  54. }