Broker.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Talk;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OCP\IServerContainer;
  10. use OCP\Talk\Exceptions\NoBackendException;
  11. use OCP\Talk\IBroker;
  12. use OCP\Talk\IConversation;
  13. use OCP\Talk\IConversationOptions;
  14. use OCP\Talk\ITalkBackend;
  15. use Psr\Log\LoggerInterface;
  16. use RuntimeException;
  17. use Throwable;
  18. class Broker implements IBroker {
  19. private Coordinator $coordinator;
  20. private IServerContainer $container;
  21. private LoggerInterface $logger;
  22. private ?bool $hasBackend = null;
  23. private ?ITalkBackend $backend = null;
  24. public function __construct(Coordinator $coordinator,
  25. IServerContainer $container,
  26. LoggerInterface $logger) {
  27. $this->coordinator = $coordinator;
  28. $this->container = $container;
  29. $this->logger = $logger;
  30. }
  31. public function hasBackend(): bool {
  32. if ($this->hasBackend !== null) {
  33. return $this->hasBackend;
  34. }
  35. $context = $this->coordinator->getRegistrationContext();
  36. if ($context === null) {
  37. // Backend requested too soon, e.g. from the bootstrap `register` method of an app
  38. throw new RuntimeException("Not all apps have been registered yet");
  39. }
  40. $backendRegistration = $context->getTalkBackendRegistration();
  41. if ($backendRegistration === null) {
  42. // Nothing to do. Remember and exit.
  43. return $this->hasBackend = false;
  44. }
  45. try {
  46. $this->backend = $this->container->get(
  47. $backendRegistration->getService()
  48. );
  49. // Remember and return
  50. return $this->hasBackend = true;
  51. } catch (Throwable $e) {
  52. $this->logger->error("Talk backend {class} could not be loaded: " . $e->getMessage(), [
  53. 'class' => $backendRegistration->getService(),
  54. 'exception' => $e,
  55. ]);
  56. // Temporary result. Maybe the next time the backend is requested it can be loaded.
  57. return false;
  58. }
  59. }
  60. public function newConversationOptions(): IConversationOptions {
  61. return ConversationOptions::default();
  62. }
  63. public function createConversation(string $name,
  64. array $moderators,
  65. ?IConversationOptions $options = null): IConversation {
  66. if (!$this->hasBackend()) {
  67. throw new NoBackendException("The Talk broker has no registered backend");
  68. }
  69. return $this->backend->createConversation(
  70. $name,
  71. $moderators,
  72. $options ?? ConversationOptions::default()
  73. );
  74. }
  75. public function deleteConversation(string $id): void {
  76. if (!$this->hasBackend()) {
  77. throw new NoBackendException("The Talk broker has no registered backend");
  78. }
  79. $this->backend->deleteConversation($id);
  80. }
  81. }