BackendManager::getBackends()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2014 Tobia De Koninck <hey--at--ledfan.be>
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later.
6
 * See the COPYING-README file.
7
 */
8
9
namespace OCA\Chat;
10
11
class BackendManager implements IBackendManager {
12
13
	/**
14
	 * @var array $backends
15
	 */
16
	private static $backends;
17
18
	public static function registerBackend(IBackend $backend){
19
		if (!isset(self::$backends[$backend->getId()])) {
20
			self::$backends[$backend->getId()] = $backend;
21
		}
22
	}
23
	public static function unRegisterBackend(IBackend $backend){
24
		unset(self::$backends[$backend->getId()]);
25
	}
26
27
	public function getEnabledBackends(){
28
		$enabledBackends = array();
29
		foreach (self::$backends as $backend) {
30
			if ($backend->isEnabled()) {
31
				$enabledBackends[$backend->getId()] = $backend;
32
			}
33
		}
34
		return $enabledBackends;
35
	}
36
37
	public function getBackend($id){
38
		return self::$backends[$id];
39
	}
40
41
	public function getBackendByProtocol($protocol){
42
		foreach (self::$backends as $backend) {
43
			if ($backend->hasProtocol($protocol)) {
44
				return $backend;
45
			}
46
		}
47
	}
48
49
	public function getBackends(){
50
		return self::$backends;
51
	}
52
53
	public function enableBackend($backend){
54
		$backends = $this->getBackends();
55
		if(array_key_exists($backend,$backends)){
56
			\OCP\Config::setAppValue('chat', 'backend_' . $backend . '_enabled', true);
57
		} else {
58
			throw new BackendNotFoundException('Backend not found', 404);
59
		}
60
	}
61
62
	public function disableBackend($backend){
63
		$backends = $this->getBackends();
64
		if(array_key_exists($backend,$backends)){
65
			\OCP\Config::setAppValue('chat', 'backend_' . $backend . '_enabled', false);
66
			return true;
67
		} else {
68
			throw new BackendNotFoundException('Backend not found', 404);
69
		}
70
	}
71
}