DeclarativeSettingsOption.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Settings;
  8. use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
  9. use OCP\Common\Exception\NotFoundException;
  10. use OCP\IUser;
  11. use OCP\IUserSession;
  12. use OCP\Server;
  13. use OCP\Settings\DeclarativeSettingsForm;
  14. use OCP\Settings\IDeclarativeManager;
  15. use Psr\Container\ContainerExceptionInterface;
  16. use Psr\Container\NotFoundExceptionInterface;
  17. class DeclarativeSettingsOption {
  18. private ?IUser $user = null;
  19. private ?IDeclarativeManager $manager = null;
  20. public function __construct(
  21. public string $appName,
  22. public DeclarativeSettingsForm $form,
  23. public string $id,
  24. ) {
  25. }
  26. /**
  27. * @throws ContainerExceptionInterface
  28. * @throws NotFoundExceptionInterface
  29. * @throws NotFoundException
  30. */
  31. private function getUser(): IUser {
  32. if ($this->user === null) {
  33. $userSession = Server::get(IUserSession::class);
  34. $this->user = $userSession->getUser();
  35. if ($this->user === null) {
  36. throw new NotFoundException('User not found');
  37. }
  38. }
  39. return $this->user;
  40. }
  41. /**
  42. * @throws ContainerExceptionInterface
  43. * @throws NotFoundExceptionInterface
  44. */
  45. private function getDeclarativeManager(): IDeclarativeManager {
  46. if ($this->manager === null) {
  47. $this->manager = Server::get(IDeclarativeManager::class);
  48. }
  49. $this->manager->loadSchemas();
  50. return $this->manager;
  51. }
  52. /**
  53. * @throws NotAdminException
  54. * @throws NotFoundExceptionInterface
  55. * @throws NotFoundException
  56. * @throws ContainerExceptionInterface
  57. */
  58. public function setValue(mixed $value): void {
  59. $this->getDeclarativeManager()->setValue(
  60. $this->getUser(),
  61. $this->appName,
  62. $this->form->getId(),
  63. $this->id,
  64. $value,
  65. );
  66. }
  67. /**
  68. * @throws NotAdminException
  69. * @throws NotFoundExceptionInterface
  70. * @throws NotFoundException
  71. * @throws ContainerExceptionInterface
  72. */
  73. public function getValue(): mixed {
  74. return $this->getDeclarativeManager()->getValue(
  75. $this->getUser(),
  76. $this->appName,
  77. $this->form->getId(),
  78. $this->id,
  79. );
  80. }
  81. /**
  82. * @throws NotAdminException
  83. * @throws NotFoundExceptionInterface
  84. * @throws NotFoundException
  85. * @throws ContainerExceptionInterface
  86. */
  87. public function deleteValue(): void {
  88. $this->getDeclarativeManager()->deleteValue(
  89. $this->getUser(),
  90. $this->appName,
  91. $this->form->getId(),
  92. $this->id,
  93. );
  94. }
  95. }