Passed
Push — master ( f4c719...cd7a21 )
by Christoph
18:01 queued 11s
created

Manager::fetchBootstrapBackends()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2018, Georg Ehrke <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
9
 * @author Georg Ehrke <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
namespace OC\Calendar\Room;
29
30
use OC\AppFramework\Bootstrap\Coordinator;
31
use OCP\Calendar\Room\IBackend;
32
use OCP\Calendar\Room\IManager;
33
use OCP\IServerContainer;
34
35
class Manager implements IManager {
0 ignored issues
show
Deprecated Code introduced by
The interface OCP\Calendar\Room\IManager has been deprecated: 24.0.0 ( Ignorable by Annotation )

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

35
class Manager implements /** @scrutinizer ignore-deprecated */ IManager {

This interface has been deprecated. The supplier of the interface has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.

Loading history...
36
	private Coordinator $bootstrapCoordinator;
37
38
	private IServerContainer $server;
39
40
	private bool $bootstrapBackendsLoaded = false;
41
42
	/**
43
	 * @var string[] holds all registered resource backends
44
	 * @psalm-var class-string<IBackend>[]
45
	 */
46
	private array $backends = [];
47
48
	/** @var IBackend[] holds all backends that have been initialized already */
49
	private array $initializedBackends = [];
50
51
	public function __construct(Coordinator $bootstrapCoordinator,
52
								IServerContainer $server) {
53
		$this->bootstrapCoordinator = $bootstrapCoordinator;
54
		$this->server = $server;
55
	}
56
57
	/**
58
	 * Registers a resource backend
59
	 *
60
	 * @param string $backendClass
61
	 * @return void
62
	 * @since 14.0.0
63
	 */
64
	public function registerBackend(string $backendClass) {
65
		$this->backends[$backendClass] = $backendClass;
66
	}
67
68
	/**
69
	 * Unregisters a resource backend
70
	 *
71
	 * @param string $backendClass
72
	 * @return void
73
	 * @since 14.0.0
74
	 */
75
	public function unregisterBackend(string $backendClass) {
76
		unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
77
	}
78
79
	private function fetchBootstrapBackends(): void {
80
		if ($this->bootstrapBackendsLoaded) {
81
			return;
82
		}
83
84
		$context = $this->bootstrapCoordinator->getRegistrationContext();
85
		if ($context === null) {
86
			// Too soon
87
			return;
88
		}
89
90
		foreach ($context->getCalendarRoomBackendRegistrations() as $registration) {
91
			$this->backends[] = $registration->getService();
92
		}
93
	}
94
95
	/**
96
	 * @return IBackend[]
97
	 * @throws \OCP\AppFramework\QueryException
98
	 * @since 14.0.0
99
	 */
100
	public function getBackends():array {
101
		$this->fetchBootstrapBackends();
102
103
		foreach ($this->backends as $backend) {
104
			if (isset($this->initializedBackends[$backend])) {
105
				continue;
106
			}
107
108
			/**
109
			 * @todo fetch from the app container
110
			 *
111
			 * The backend might have services injected that can't be build from the
112
			 * server container.
113
			 */
114
			$this->initializedBackends[$backend] = $this->server->query($backend);
115
		}
116
117
		return array_values($this->initializedBackends);
118
	}
119
120
	/**
121
	 * @param string $backendId
122
	 * @throws \OCP\AppFramework\QueryException
123
	 * @return IBackend|null
124
	 */
125
	public function getBackend($backendId) {
126
		$backends = $this->getBackends();
127
		foreach ($backends as $backend) {
128
			if ($backend->getBackendIdentifier() === $backendId) {
129
				return $backend;
130
			}
131
		}
132
133
		return null;
134
	}
135
136
	/**
137
	 * removes all registered backend instances
138
	 * @return void
139
	 * @since 14.0.0
140
	 */
141
	public function clear() {
142
		$this->backends = [];
143
		$this->initializedBackends = [];
144
	}
145
}
146