1
0

Broker.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  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. namespace OC\Talk;
  24. use OC\AppFramework\Bootstrap\Coordinator;
  25. use OCP\IServerContainer;
  26. use OCP\Talk\Exceptions\NoBackendException;
  27. use OCP\Talk\IBroker;
  28. use OCP\Talk\IConversation;
  29. use OCP\Talk\IConversationOptions;
  30. use OCP\Talk\ITalkBackend;
  31. use Psr\Log\LoggerInterface;
  32. use RuntimeException;
  33. use Throwable;
  34. class Broker implements IBroker {
  35. private Coordinator $coordinator;
  36. private IServerContainer $container;
  37. private LoggerInterface $logger;
  38. private ?bool $hasBackend = null;
  39. private ?ITalkBackend $backend = null;
  40. public function __construct(Coordinator $coordinator,
  41. IServerContainer $container,
  42. LoggerInterface $logger) {
  43. $this->coordinator = $coordinator;
  44. $this->container = $container;
  45. $this->logger = $logger;
  46. }
  47. public function hasBackend(): bool {
  48. if ($this->hasBackend !== null) {
  49. return $this->hasBackend;
  50. }
  51. $context = $this->coordinator->getRegistrationContext();
  52. if ($context === null) {
  53. // Backend requested too soon, e.g. from the bootstrap `register` method of an app
  54. throw new RuntimeException("Not all apps have been registered yet");
  55. }
  56. $backendRegistration = $context->getTalkBackendRegistration();
  57. if ($backendRegistration === null) {
  58. // Nothing to do. Remember and exit.
  59. return $this->hasBackend = false;
  60. }
  61. try {
  62. $this->backend = $this->container->get(
  63. $backendRegistration->getService()
  64. );
  65. // Remember and return
  66. return $this->hasBackend = true;
  67. } catch (Throwable $e) {
  68. $this->logger->error("Talk backend {class} could not be loaded: " . $e->getMessage(), [
  69. 'class' => $backendRegistration->getService(),
  70. 'exception' => $e,
  71. ]);
  72. // Temporary result. Maybe the next time the backend is requested it can be loaded.
  73. return false;
  74. }
  75. }
  76. public function newConversationOptions(): IConversationOptions {
  77. return ConversationOptions::default();
  78. }
  79. public function createConversation(string $name,
  80. array $moderators,
  81. IConversationOptions $options = null): IConversation {
  82. if (!$this->hasBackend()) {
  83. throw new NoBackendException("The Talk broker has no registered backend");
  84. }
  85. return $this->backend->createConversation(
  86. $name,
  87. $moderators,
  88. $options ?? ConversationOptions::default()
  89. );
  90. }
  91. }