Completed
Push — master ( 8110a3...fe2a60 )
by Morris
18:07 queued 10s
created

Manager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
31
	/** @var IServerContainer */
32
	private $server;
33
34
	/** @var string[] holds all registered resource backends */
35
	private $backends = [];
36
37
	/** @var IBackend[] holds all backends that have been initialized already */
38
	private $initializedBackends = [];
39
40
	/**
41
	 * Manager constructor.
42
	 *
43
	 * @param IServerContainer $server
44
	 */
45
	public function __construct(IServerContainer $server) {
46
		$this->server = $server;
47
	}
48
49
	/**
50
	 * Registers a resource backend
51
	 *
52
	 * @param string $backendClass
53
	 * @return void
54
	 * @since 14.0.0
55
	 */
56
	public function registerBackend(string $backendClass) {
57
		$this->backends[$backendClass] = $backendClass;
58
	}
59
60
	/**
61
	 * Unregisters a resource backend
62
	 *
63
	 * @param string $backendClass
64
	 * @return void
65
	 * @since 14.0.0
66
	 */
67
	public function unregisterBackend(string $backendClass) {
68
		unset($this->backends[$backendClass], $this->initializedBackends[$backendClass]);
69
	}
70
71
	/**
72
	 * @return IBackend[]
73
	 * @throws \OCP\AppFramework\QueryException
74
	 * @since 14.0.0
75
	 */
76
	public function getBackends():array {
77
		foreach($this->backends as $backend) {
78
			if (isset($this->initializedBackends[$backend])) {
79
				continue;
80
			}
81
82
			$this->initializedBackends[$backend] = $this->server->query($backend);
83
		}
84
85
		return array_values($this->initializedBackends);
86
	}
87
88
	/**
89
	 * @param string $backendId
90
	 * @throws \OCP\AppFramework\QueryException
91
	 * @return IBackend|null
92
	 */
93
	public function getBackend($backendId) {
94
		$backends = $this->getBackends();
95
		foreach($backends as $backend) {
96
			if ($backend->getBackendIdentifier() === $backendId) {
97
				return $backend;
98
			}
99
		}
100
101
		return null;
102
	}
103
104
	/**
105
	 * removes all registered backend instances
106
	 * @return void
107
	 * @since 14.0.0
108
	 */
109
	public function clear() {
110
		$this->backends = [];
111
		$this->initializedBackends = [];
112
	}
113
}
114