Passed
Push — master ( 2f2538...82f602 )
by Joas
14:31 queued 12s
created

Broker   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 68
c 1
b 0
f 0
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A newConversationOptions() 0 2 1
A createConversation() 0 11 2
A __construct() 0 6 1
A hasBackend() 0 31 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * @copyright 2022 Christoph Wurst <[email protected]>
7
 *
8
 * @author 2021 Christoph Wurst <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 */
25
26
namespace OC\Talk;
27
28
use OC\AppFramework\Bootstrap\Coordinator;
29
use OCP\IServerContainer;
30
use OCP\Talk\Exceptions\NoBackendException;
31
use OCP\Talk\IBroker;
32
use OCP\Talk\IConversation;
33
use OCP\Talk\IConversationOptions;
34
use OCP\Talk\ITalkBackend;
35
use Psr\Log\LoggerInterface;
36
use RuntimeException;
37
use Throwable;
38
39
class Broker implements IBroker {
40
	private Coordinator $coordinator;
41
42
	private IServerContainer $container;
43
44
	private LoggerInterface $logger;
45
46
	private ?bool $hasBackend = null;
47
48
	private ?ITalkBackend $backend = null;
49
50
	public function __construct(Coordinator $coordinator,
51
								IServerContainer $container,
52
								LoggerInterface $logger) {
53
		$this->coordinator = $coordinator;
54
		$this->container = $container;
55
		$this->logger = $logger;
56
	}
57
58
	public function hasBackend(): bool {
59
		if ($this->hasBackend !== null) {
60
			return $this->hasBackend;
61
		}
62
63
		$context = $this->coordinator->getRegistrationContext();
64
		if ($context === null) {
65
			// Backend requested too soon, e.g. from the bootstrap `register` method of an app
66
			throw new RuntimeException("Not all apps have been registered yet");
67
		}
68
		$backendRegistration = $context->getTalkBackendRegistration();
69
		if ($backendRegistration === null) {
70
			// Nothing to do. Remember and exit.
71
			return $this->hasBackend = false;
72
		}
73
74
		try {
75
			$this->backend = $this->container->get(
76
				$backendRegistration->getService()
77
			);
78
79
			// Remember and return
80
			return $this->hasBackend = true;
81
		} catch (Throwable $e) {
82
			$this->logger->error("Talk backend {class} could not be loaded: " . $e->getMessage(), [
83
				'class' => $backendRegistration->getService(),
84
				'exception' => $e,
85
			]);
86
87
			// Temporary result. Maybe the next time the backend is requested it can be loaded.
88
			return false;
89
		}
90
	}
91
92
	public function newConversationOptions(): IConversationOptions {
93
		return ConversationOptions::default();
94
	}
95
96
	public function createConversation(string $name,
97
									   array $moderators,
98
									   IConversationOptions $options = null): IConversation {
99
		if (!$this->hasBackend()) {
100
			throw new NoBackendException("The Talk broker has no registered backend");
101
		}
102
103
		return $this->backend->createConversation(
0 ignored issues
show
Bug introduced by
The method createConversation() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
		return $this->backend->/** @scrutinizer ignore-call */ createConversation(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
			$name,
105
			$moderators,
106
			$options ?? ConversationOptions::default()
107
		);
108
	}
109
}
110