Code Duplication    Length = 85-85 lines in 2 locations

lib/private/Calendar/Resource/Manager.php 1 location

@@ 29-113 (lines=85) @@
26
use OCP\Calendar\Resource\IBackend;
27
use OCP\IServerContainer;
28
29
class Manager implements \OCP\Calendar\Resource\IManager {
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

lib/private/Calendar/Room/Manager.php 1 location

@@ 29-113 (lines=85) @@
26
use OCP\Calendar\Room\IBackend;
27
use OCP\IServerContainer;
28
29
class Manager implements \OCP\Calendar\Room\IManager {
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