Manager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\UserStatus;
  8. use OCP\IServerContainer;
  9. use OCP\UserStatus\IManager;
  10. use OCP\UserStatus\IProvider;
  11. use Psr\Container\ContainerExceptionInterface;
  12. use Psr\Log\LoggerInterface;
  13. class Manager implements IManager {
  14. /** @var IServerContainer */
  15. private $container;
  16. /** @var LoggerInterface */
  17. private $logger;
  18. /** @var class-string */
  19. private $providerClass;
  20. /** @var IProvider */
  21. private $provider;
  22. /**
  23. * Manager constructor.
  24. *
  25. * @param IServerContainer $container
  26. * @param LoggerInterface $logger
  27. */
  28. public function __construct(IServerContainer $container,
  29. LoggerInterface $logger) {
  30. $this->container = $container;
  31. $this->logger = $logger;
  32. }
  33. /**
  34. * @inheritDoc
  35. */
  36. public function getUserStatuses(array $userIds): array {
  37. $this->setupProvider();
  38. if (!$this->provider) {
  39. return [];
  40. }
  41. return $this->provider->getUserStatuses($userIds);
  42. }
  43. /**
  44. * @param string $class
  45. * @since 20.0.0
  46. * @internal
  47. */
  48. public function registerProvider(string $class): void {
  49. $this->providerClass = $class;
  50. $this->provider = null;
  51. }
  52. /**
  53. * Lazily set up provider
  54. */
  55. private function setupProvider(): void {
  56. if ($this->provider !== null) {
  57. return;
  58. }
  59. if ($this->providerClass === null) {
  60. return;
  61. }
  62. /**
  63. * @psalm-suppress InvalidCatch
  64. */
  65. try {
  66. $provider = $this->container->get($this->providerClass);
  67. } catch (ContainerExceptionInterface $e) {
  68. $this->logger->error('Could not load user-status "' . $this->providerClass . '" provider dynamically: ' . $e->getMessage(), [
  69. 'exception' => $e,
  70. ]);
  71. return;
  72. }
  73. $this->provider = $provider;
  74. }
  75. public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false, ?string $customMessage = null): void {
  76. $this->setupProvider();
  77. if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
  78. return;
  79. }
  80. $this->provider->setUserStatus($userId, $messageId, $status, $createBackup, $customMessage);
  81. }
  82. public function revertUserStatus(string $userId, string $messageId, string $status): void {
  83. $this->setupProvider();
  84. if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
  85. return;
  86. }
  87. $this->provider->revertUserStatus($userId, $messageId, $status);
  88. }
  89. public function revertMultipleUserStatus(array $userIds, string $messageId, string $status): void {
  90. $this->setupProvider();
  91. if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
  92. return;
  93. }
  94. $this->provider->revertMultipleUserStatus($userIds, $messageId, $status);
  95. }
  96. }