1
0

Manager.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\UserStatus;
  25. use OCP\IServerContainer;
  26. use OCP\UserStatus\IManager;
  27. use OCP\UserStatus\IProvider;
  28. use Psr\Container\ContainerExceptionInterface;
  29. use Psr\Log\LoggerInterface;
  30. class Manager implements IManager {
  31. /** @var IServerContainer */
  32. private $container;
  33. /** @var LoggerInterface */
  34. private $logger;
  35. /** @var class-string */
  36. private $providerClass;
  37. /** @var IProvider */
  38. private $provider;
  39. /**
  40. * Manager constructor.
  41. *
  42. * @param IServerContainer $container
  43. * @param LoggerInterface $logger
  44. */
  45. public function __construct(IServerContainer $container,
  46. LoggerInterface $logger) {
  47. $this->container = $container;
  48. $this->logger = $logger;
  49. }
  50. /**
  51. * @inheritDoc
  52. */
  53. public function getUserStatuses(array $userIds): array {
  54. $this->setupProvider();
  55. if (!$this->provider) {
  56. return [];
  57. }
  58. return $this->provider->getUserStatuses($userIds);
  59. }
  60. /**
  61. * @param string $class
  62. * @since 20.0.0
  63. * @internal
  64. */
  65. public function registerProvider(string $class): void {
  66. $this->providerClass = $class;
  67. $this->provider = null;
  68. }
  69. /**
  70. * Lazily set up provider
  71. */
  72. private function setupProvider(): void {
  73. if ($this->provider !== null) {
  74. return;
  75. }
  76. if ($this->providerClass === null) {
  77. return;
  78. }
  79. /**
  80. * @psalm-suppress InvalidCatch
  81. */
  82. try {
  83. $provider = $this->container->get($this->providerClass);
  84. } catch (ContainerExceptionInterface $e) {
  85. $this->logger->error('Could not load user-status "' . $this->providerClass . '" provider dynamically: ' . $e->getMessage(), [
  86. 'exception' => $e,
  87. ]);
  88. return;
  89. }
  90. $this->provider = $provider;
  91. }
  92. public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false, ?string $customMessage = null): void {
  93. $this->setupProvider();
  94. if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
  95. return;
  96. }
  97. $this->provider->setUserStatus($userId, $messageId, $status, $createBackup, $customMessage);
  98. }
  99. public function revertUserStatus(string $userId, string $messageId, string $status): void {
  100. $this->setupProvider();
  101. if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
  102. return;
  103. }
  104. $this->provider->revertUserStatus($userId, $messageId, $status);
  105. }
  106. public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void {
  107. $this->setupProvider();
  108. if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
  109. return;
  110. }
  111. $this->provider->revertMultipleUserStatus($userIds, $messageId, $status);
  112. }
  113. }