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

Manager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A clear() 0 3 1
A registerBackend() 0 2 1
A getBackends() 0 12 3
A unregisterBackend() 0 2 1
A fetchBootstrapBackends() 0 13 4
A __construct() 0 4 1
A getBackend() 0 9 3
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\Resource;
29
30
use OC\AppFramework\Bootstrap\Coordinator;
31
use OCP\Calendar\Resource\IBackend;
32
use OCP\Calendar\Resource\IManager;
33
use OCP\IServerContainer;
34
35
class Manager implements IManager {
0 ignored issues
show
Deprecated Code introduced by
The interface OCP\Calendar\Resource\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 $backends = [];
47
48
	/** @var IBackend[] holds all backends that have been initialized already */
49
	private $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->getCalendarResourceBackendRegistrations() 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
			$this->initializedBackends[$backend] = $this->server->query($backend);
109
		}
110
111
		return array_values($this->initializedBackends);
112
	}
113
114
	/**
115
	 * @param string $backendId
116
	 * @throws \OCP\AppFramework\QueryException
117
	 * @return IBackend|null
118
	 */
119
	public function getBackend($backendId) {
120
		$backends = $this->getBackends();
121
		foreach ($backends as $backend) {
122
			if ($backend->getBackendIdentifier() === $backendId) {
123
				return $backend;
124
			}
125
		}
126
127
		return null;
128
	}
129
130
	/**
131
	 * removes all registered backend instances
132
	 * @return void
133
	 * @since 14.0.0
134
	 */
135
	public function clear() {
136
		$this->backends = [];
137
		$this->initializedBackends = [];
138
	}
139
}
140