You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.4 KiB

3 years ago
  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\ILogger;
  26. use OCP\IServerContainer;
  27. use OCP\UserStatus\IManager;
  28. use OCP\UserStatus\IProvider;
  29. use Psr\Container\ContainerExceptionInterface;
  30. class Manager implements IManager {
  31. /** @var IServerContainer */
  32. private $container;
  33. /** @var ILogger */
  34. private $logger;
  35. /** @var null */
  36. private $providerClass;
  37. /** @var IProvider */
  38. private $provider;
  39. /**
  40. * Manager constructor.
  41. *
  42. * @param IServerContainer $container
  43. * @param ILogger $logger
  44. */
  45. public function __construct(IServerContainer $container,
  46. ILogger $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. try {
  80. $provider = $this->container->get($this->providerClass);
  81. } catch (ContainerExceptionInterface $e) {
  82. $this->logger->logException($e, [
  83. 'message' => 'Could not load user-status provider dynamically: ' . $e->getMessage(),
  84. 'level' => ILogger::ERROR,
  85. ]);
  86. return;
  87. }
  88. $this->provider = $provider;
  89. }
  90. }